我在将一个UTC转换为python中的新时区时遇到了一些麻烦。从UTC到新时区的时区转换
的数据原本是一个字符串,带来并成功转化为UTC这样:
df['order delivery time'] = pd.to_datetime(df['order delivery time'])
接下来,我尝试写这样的功能:
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))
然后应用:
df['order delivery time ADJUSTED'] = df.apply(lambda row: change_tz(row['order delivery time'], row['region']), axis=1)
我收到此错误:
条AttributeError: ("'US/Central' object has no attribute 'upper'", u'occurred at index 0')
我也试过线,如:
if region == 'sf':
return datetime.fromtimestamp(time, tz='US/Pacific')
和:
if region == 'sf':
return tz.fromutc(datetime.utcfromtimestamp(time).replace(tzinfo='US/Pacific'))
请帮我转换时区!谢谢!