我在將一個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'))
請幫我轉換時區!謝謝!