I'm having a bunch of trouble converting UTC to a new time zone in python.
The data is originally a string brought in and successfully to converted to UTC as such:
df['order delivery time'] = pd.to_datetime(df['order delivery time'])
Next I attempt to write a function like this:
eastern = pytz.timezone('US/Eastern')
central = pytz.timezone('US/Central')
pacific = pytz.timezone('US/Pacific')
def change_tz(time, region):
if region == 'sf':
return time.astimezone(pytz.timezone(pacific))
elif region == 'chi':
return time.astimezone(pytz.timezone(central))
elif region == 'nyc':
return time.astimezone(pytz.timezone(eastern))
Then apply:
df['order delivery time ADJUSTED'] = df.apply(lambda row: change_tz(row['order delivery time'], row['region']), axis=1)
I get this error:
AttributeError: ("'US/Central' object has no attribute 'upper'", u'occurred at index 0')
I've also tried lines like:
if region == 'sf':
return datetime.fromtimestamp(time, tz='US/Pacific')
And:
if region == 'sf':
return tz.fromutc(datetime.utcfromtimestamp(time).replace(tzinfo='US/Pacific'))
Please help me convert the time zone! Thanks!