pandas.to_datetime 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16910795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:52:23  来源:igfitidea点击:

pandas.to_datetime ERROR

pythonpandasipython-notebook

提问by Nilani Algiriyage

I have a pandas dataframe and I want to convert the time column to datetime format.

我有一个Pandas数据框,我想将时间列转换为日期时间格式。

Time

时间

30/May/2013 06:00:41 -0600

2013 年 5 月 30 日 06:00:41 -0600

import pandas as pd
df.index = pd.to_datetime(df.pop('Time'))

But it always gives the following error.What is the problem with the code? :(

但是老是报如下错误,代码有什么问题吗?:(

AttributeError                            Traceback (most recent call last)
<ipython-input-124-9219cf10d027> in <module>()
----> 1 df.index = pd.to_datetime(df.pop('Time'))

AttributeError: 'module' object has no attribute 'to_datetime'

回答by waitingkuo

Use set_index to set the time column as the index, and then convert the Indexto DateTimeIndex:

使用 set_index 将时间列设置为索引,然后将其转换IndexDateTimeIndex

df = df.set_index('Time')

df.index = df.index.to_datetime()

回答by Andy Hayden

The to_datetimefunction was introduced in 0.8.0, so you'll have to upgrade your pandas to use it.
Ideally to the latest stable version.

to_datetime功能是在 0.8.0引入的,因此您必须升级您的Pandas才能使用它。
最好是最新的稳定版本