Python Pandas:字符串到日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45018172/
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
Python Pandas: string to datetime
提问by Tuutsrednas
I have a Pandas dataframe with a datetime column in string format. The format is like this:
我有一个带有字符串格式的日期时间列的 Pandas 数据框。格式是这样的:
06 Feb 2014 12:09:42:000
I need to convert this to datetime. Right now I have:
我需要将其转换为日期时间。现在我有:
df['date'] = pd.to_datetime(df['STARTDATE'],format='')
My issue is, I do not know what to put in the format argument to parse the string correctly. Can this be done, or is there a better function to use?
我的问题是,我不知道在格式参数中放什么来正确解析字符串。可以这样做,还是有更好的功能可以使用?
回答by jezrael
You can check http://strftime.org/and use:
您可以查看http://strftime.org/并使用:
df['date'] = pd.to_datetime(df['STARTDATE'],format='%d %b %Y %H:%M:%S:%f')
Sample:
样本:
df = pd.DataFrame({'STARTDATE':['06 Feb 2014 12:09:42:000','06 Mar 2014 12:09:42:000']})
print (df)
STARTDATE
0 06 Feb 2014 12:09:42:000
1 06 Mar 2014 12:09:42:000
df['date'] = pd.to_datetime(df['STARTDATE'],format='%d %b %Y %H:%M:%S:%f')
print (df)
STARTDATE date
0 06 Feb 2014 12:09:42:000 2014-02-06 12:09:42
1 06 Mar 2014 12:09:42:000 2014-03-06 12:09:42