pandas :将数据框的列转换为日期时间 [英] Pandas: convert column of dataframe to datetime

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

问题描述

我有df

ID month
0       0001ee12f919a1b570658024bb59d118    2014-02 
1       0001ee12f919a1b570658024bb59d118    2014-03  
2       0001ee12f919a1b570658024bb59d118    2014-04  
3       0001ee12f919a1b570658024bb59d118    2014-05  
4       0001ee12f919a1b570658024bb59d118    2014-06  
5       0001ee12f919a1b570658024bb59d118    2014-07

,我尝试将 year_month 设置为日期时间.我使用 df1 ['month'] = pd.to_datetime(df1.month)但返回 ValueError:未知的字符串格式我该如何解决?

and I try to turn year_month to datetime. I use df1['month'] = pd.to_datetime(df1.month) but it return ValueError: Unknown string format how can I fix that?

推荐答案

您需要将格式字符串'%Y-%m'传递为

You need to pass a format string '%Y-%m' as to_datetime can't deduce the format from your string:

In [42]:
df['date'] = pd.to_datetime(df['month'], format='%Y-%m')
df

Out[42]:
                                 ID    month       date
0  0001ee12f919a1b570658024bb59d118  2014-02 2014-02-01
1  0001ee12f919a1b570658024bb59d118  2014-03 2014-03-01
2  0001ee12f919a1b570658024bb59d118  2014-04 2014-04-01
3  0001ee12f919a1b570658024bb59d118  2014-05 2014-05-01
4  0001ee12f919a1b570658024bb59d118  2014-06 2014-06-01
5  0001ee12f919a1b570658024bb59d118  2014-07 2014-07-01

In [43]:
df.info()           

<class 'pandas.core.frame.DataFrame'>
Int64Index: 6 entries, 0 to 5
Data columns (total 3 columns):
ID       6 non-null object
month    6 non-null object
date     6 non-null datetime64[ns]
dtypes: datetime64[ns](1), object(2)
memory usage: 192.0+ bytes

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

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