AttributeError:"str"对象没有属性"to_datetime" [英] AttributeError: 'str' object has no attribute 'to_datetime'

查看:896
本文介绍了AttributeError:"str"对象没有属性"to_datetime"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码将Excel数据表(一个表)读入DataFrame并将日期"列(例如值20150508)转换为日期时间,

I have a code that reads an excel data sheet (a table) into a DataFrame and convert a 'date' column (with values e.g. 20150508) into date time,

df['date'] = df['date'].astype(str)
dates = df['date'].to_datetime() // error occurs

我出错了,

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

如果我删除了该行,

df['date'] = df['date'].astype(str)

另一行可以正常运行,我想知道是什么问题.

the other line can run fine, I am wondering what is the problem.

推荐答案

没有 to_datetime 仅用于Index对象的Series方法,它是您想要的顶级方法:

There is no to_datetime method for Series only for Index objects it's the top-level method you want:

dates = pd.to_datetime(df['date'])

这篇关于AttributeError:"str"对象没有属性"to_datetime"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆