I am trying to convert the timezone of a pandas Series. I am using the pytz package to do so, however I am getting a value that is off by a few minutes. The code I am currently using can be found in the answer here: Converting Items from Pandas Series to Date Time
Other answers suggest using the localize()
function to make this work, however this does not work in my code. I tried using the normalize()
function to solve this issue but I am getting an error using a pandas series.
Error message: AttributeError: 'Series' object has no attribute 'tzinfo'
Sample input:
dfNY = pd.DataFrame({'TimeSeries': [13:00, nan, 06:00, 'Morning', 'Afternoon', nan, nan, 01:30]})
Desired output:
dfLondon = pd.DataFrame({'TimeSeries': [18:00, nan, 11:00, 'Morning', 'Afternoon', nan, nan, 06:30]})
Current code:
import pandas as pd
from pytz import timezone
dfNY = pd.DataFrame({'TimeSeries': [13:00, nan, 06:00, 'Morning', 'Afternoon', nan, nan, 01:30]})
tzDestination = "Europe/London"
dtTimeSeries = pd.to_datetime(dfNY.TimeSeries, errors='coerce', format='%H:%M').dt.tz_localize(tzOrigin)
tzChange = timezone(tzDestination)
convertedTime = tzChange.normalize(dtTimeSeries).dt.strftime('%H:%M')
dyNY = convertedTime.copy()
dfNY = timeSeries.where(~convertedTime.ne('NaT'), convertedTime)
Thanks for any help