I have the following code:
time = calendar.iat[i, j]
time = dt.datetime.strptime(str(time), '%Y-%m-%d %H:%M:%S%z') # converts the time to a datetime object
time = time.replace(tzinfo=from_zone) # notes the current timezone of the object
time = time.astimezone(to_zone) # converts the object to the desired timezone
calendar.iat[i, j] = time # commits the change to the dataframe
where
from_zone = tz.gettz('UTC')
to_zone = tz.gettz('America/New_York')
Calendar in this case is a Pandas Dataframe that contains UTC + 0 datetimes. I need to convert these datetimes to UTC -4/-5 (see an example below as to what is happening).
The way I am doing this is by looping over the data-frame and replacing these one by one (not the most efficient method, but I still need to learn how to implement vectorization properly). This being said, if I execute this line of code:
calendar.iat[i, j] = time # commits the change to the dataframe
then I get the following warning:
FutureWarning: Setitem-like behavior with mismatched timezones is deprecated and
will change in a future version. Instead of raising (or for Index, Series, and
DataFrame methods, coercing to object dtype),the value being set (or passed as a
fill_value, or inserted) will be cast to the existing DatetimeArray/DatetimeIndex
/Series/DataFrame column's timezone. To retain the old behavior, explicitly cast
to object dtype before the operation.
calendar.iat[i, j] = time # commits the change to the dataframe
Having googled this specific alert, I did not come up with anything worthwhile. Can someone explain to me what this alert means and how would I remove it? Is Pandas suggesting here another way of changing the time zones of the dates? The code works fine in general and the dates are converted properly to their required timezone.
The imports I am using for this code are:
import pandas as pd
import datetime as dt
from datetime import time
from dateutil import tz