pandas 在熊猫中将列表转换为日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34428678/
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
Convert list to datetime in pandas
提问by user308827
I have the foll. list in pandas:
我有一个愚蠢的。Pandas列表:
str = jan_1 jan_15 feb_1 feb_15 mar_1 mar_15 apr_1 apr_15 may_1 may_15 jun_1 jun_15 jul_1 jul_15 aug_1 aug_15 sep_1 sep_15 oct_1 oct_15 nov_1 nov_15 dec_1 dec_15
Is there a way to convert it into datetime?
有没有办法将其转换为日期时间?
I tried:
pd.to_datetime(pd.Series(str))
我试过:
pd.to_datetime(pd.Series(str))
回答by Sandeep S
You have to specify the format argument while calling pd.to_datetime. Try
您必须在调用 pd.to_datetime 时指定格式参数。尝试
pd.to_datetime(pd.Series(s), format='%b_%d')
this gives
这给
0 1900-01-01
1 1900-01-15
2 1900-02-01
3 1900-02-15
4 1900-03-01
5 1900-03-15
6 1900-04-01
7 1900-04-15
8 1900-05-01
9 1900-05-15
For setting the current year, a hack may be required, like
要设置当前年份,可能需要进行 hack,例如
pd.to_datetime(pd.Series(s) + '_2015', format='%b_%d_%Y')
to get
要得到
0 2015-01-01
1 2015-01-15
2 2015-02-01
3 2015-02-15
4 2015-03-01
5 2015-03-15
6 2015-04-01
7 2015-04-15
8 2015-05-01
9 2015-05-15