我想将pandas数据框中“Time”列中的所有项目从UTC转换为东部时间。然而,按照this stackoverflow帖子中的答案,有些关键字在pandas 0.20.3中是未知的。总体而言,我应该如何完成这个任务?
tweets_df = pd.read_csv('valid_tweets.csv')
tweets_df['Time'] = tweets_df.to_datetime(tweets_df['Time'])
tweets_df.set_index('Time', drop=False, inplace=True)
错误是:
tweets_df['Time'] = tweets_df.to_datetime(tweets_df['Time'])
File "/scratch/sjn/anaconda/lib/python3.6/site-packages/pandas/core/generic.py", line 3081, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'to_datetime'
时间列中的项目看起来像这样:
2016-10-20 03:43:11+00:00
更新:使用中
tweets_df['Time'] = pd.to_datetime(tweets_df['Time'])
tweets_df.set_index('Time', drop=False, inplace=True)
tweets_df.index = tweets_df.index.tz_localize('UTC').tz_convert('US/Eastern')
没有进行时间转换。有什么问题可以修复的吗?
更新2:因此,以下代码不会进行就地转换,这意味着当我使用iterrows()
打印row['Time']
时,它会显示原始值。您知道如何进行原地转换吗?
tweets_df['Time'] = pd.to_datetime(tweets_df['Time'])
for index, row in tweets_df.iterrows():
row['Time'].tz_localize('UTC').tz_convert('US/Eastern')
for index, row in tweets_df.iterrows():
print(row['Time'])
pandas.to_datetime
不是 DataFrame 的一个方法。” - Alex