Skip to content Skip to sidebar Skip to footer

Convert List To Datetime In Pandas

I have the foll. list in 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

Solution 1:

You have to specify the format argument while calling pd.to_datetime. Try

pd.to_datetime(pd.Series(s), format='%b_%d')

this gives

01900-01-0111900-01-1521900-02-0131900-02-1541900-03-0151900-03-1561900-04-0171900-04-1581900-05-0191900-05-15

For setting the current year, a hack may be required, like

pd.to_datetime(pd.Series(s) + '_2015', format='%b_%d_%Y')

to get

02015-01-0112015-01-1522015-02-0132015-02-1542015-03-0152015-03-1562015-04-0172015-04-1582015-05-0192015-05-15

Post a Comment for "Convert List To Datetime In Pandas"