pandas 如何在熊猫中将对象转换为日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42693410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:08:46  来源:igfitidea点击:

How to convert object to date in pandas

pandas

提问by Neil

I have pandas column like following

我有如下的Pandas专栏

   January 2014
   February 2014

I want to convert it to following format 201401 201402 I am doing following

我想将其转换为以下格式 201401 201402 我正在执行以下操作

   df.date = pd.to_datetime(df.date,format= '%Y%B') 

But,it gives me an error.

但是,它给了我一个错误。

回答by EdChum

You shouldn't need the format string, it just works:

你不应该需要格式字符串,它只是有效:

In [207]:
pd.to_datetime('January 2014')

Out[207]:
Timestamp('2014-01-01 00:00:00')

besides your format string is incorrect, it should be '%B %Y':

除了您的格式字符串不正确之外,它应该是'%B %Y'

In [209]:
pd.to_datetime('January 2014', format='%B %Y')

Out[209]:
Timestamp('2014-01-01 00:00:00')