【发布时间】:2017-01-05 13:36:12
【问题描述】:
我有一个处理 Excel 文件的脚本。发送它的部门有一个生成它的系统,我的脚本停止工作。
我突然收到以下代码行的错误Can only use .str accessor with string values, which use np.object_ dtype in pandas:
df['DATE'] = df['Date'].str.replace(r'[^a-zA-Z0-9\._/-]', '')
我检查了旧系统文件中日期列的类型 (dtype: object) 与新系统文件 (dtype: datetime64[ns])。
如何将日期格式更改为我的脚本可以理解的格式?
我看到了this answer,但我对日期格式的了解并不那么细致。
【问题讨论】:
-
您可以使用
df['Date'].dt.strftime('%Y-%m-%d')将日期转换回字符串。 -
成功了,谢谢。
标签: python-2.7 datetime pandas datetime64