微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – AttributeError:’DataFrame’对象没有属性’to_datetime’

我想将我的pandas数据帧的’Time’列中的所有项目从UTC转换为Eastern时间.但是,根据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()打印行[‘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'])

解决方法:

to_datetime是在pandas中定义的函数,而不是DataFrame上的方法.尝试:

tweets_df['Time'] = pd.to_datetime(tweets_df['Time'])

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐