pandas “系列”对象没有属性“to_datetime”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54318395/
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-14 06:17:27  来源:igfitidea点击:

'Series' object has no attribute 'to_datetime'

pythonpandasdataframe

提问by wen

I am trying to convert one column str type to datetime type. But when I write the code:

我正在尝试将一列 str 类型转换为日期时间类型。但是当我写代码时:

df.timeStamp = df.timeStamp.to_datetime

it just tell me

它只是告诉我

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

But when I try

但是当我尝试

pd.to_datetime(df.timeStamp)

it works.

有用。

I am new to python and hope someone could explain why it happens.

我是 python 的新手,希望有人能解释它为什么会发生。

I appreciate your time!

我很感激你的时间!

回答by U10-Forward

Because to_datetimeis only a valid attribute to pandasmodule, that's all.

因为to_datetime只是pandas模块的有效属性,仅此而已。

So that's why:

所以这就是为什么:

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

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

(see highlighted part)

(见高亮部分)

So of course, to_datetimecan't be used that way.

所以当然to_datetime不能这样使用。

回答by Espanta

I am kind of late, but still useful for future readers.

我有点晚了,但对未来的读者仍然有用。

Below code converts a column of type objectin a pandas df to type timestamp

下面的代码将Pandas df中类型为object的列转换为类型时间戳

df.timeStamp = pd.to_datetime(df.timeStamp)