pandas 将具有年份整数的列转换为日期时间 [英] Pandas convert column with year integer to datetime

查看:59
本文介绍了 pandas 将具有年份整数的列转换为日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将列(datatype:int64)转换为 datetime 时遇到了一些问题.

I am having some problem converting column (datatype:int64) into datetime working with Pandas.

原始数据:

Year
2015
2014
...
2010

所需结果:

Year
2015-01-01
2014-01-01
...
2010-01-01

我当前的结果:

Year
1970-01-01 00:00:00.000002015
1970-01-01 00:00:00.000002014
...
1970-01-01 00:00:00.000002010

我尝试过:

data.Year = pd.to_datetime(data.Year)
data.Year = pd.to_datetime(data.Year, format='%Y-%m-%d')

谢谢

推荐答案

使用format='%Y'

In [225]: pd.to_datetime(df.Year, format='%Y')
Out[225]:
0   2015-01-01
1   2014-01-01
2   2010-01-01
Name: Year, dtype: datetime64[ns]


详细信息


Details

In [226]: df
Out[226]:
   Year
0  2015
1  2014
2  2010

这篇关于 pandas 将具有年份整数的列转换为日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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