如何将 Pandas 数据框中的字符串转换为“日期"数据类型? [英] How do I convert strings in a Pandas data frame to a 'date' data type?

查看:79
本文介绍了如何将 Pandas 数据框中的字符串转换为“日期"数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Pandas 数据框,其中一列包含格式为 YYYY-MM-DD

I have a Pandas data frame, one of the column contains date strings in the format YYYY-MM-DD

例如'2013-10-28'

此时列的dtypeobject.

如何将列值转换为 Pandas 日期格式?

How do I convert the column values to Pandas date format?

推荐答案

使用 类型

In [31]: df
Out[31]: 
   a        time
0  1  2013-01-01
1  2  2013-01-02
2  3  2013-01-03

In [32]: df['time'] = df['time'].astype('datetime64[ns]')

In [33]: df
Out[33]: 
   a                time
0  1 2013-01-01 00:00:00
1  2 2013-01-02 00:00:00
2  3 2013-01-03 00:00:00

这篇关于如何将 Pandas 数据框中的字符串转换为“日期"数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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