简体   繁体   中英

Extract date from string Pandas data frame

I have this column Date_X in pandas dataframe which is a object. I am not able to use pandas to_datetime function here. I want to convert this column to a range of 1 to 365, using which I can carry on my analysis.

29JAN14:21:16:00
01FEB14:00:11:00
30JAN14:15:11:00
01FEB14:00:07:00
29JAN14:19:31:00
30JAN14:23:52:00
29JAN14:19:18:00
30JAN14:16:46:00
30JAN14:21:39:00
31JAN14:17:40:00
01FEB14:00:16:00

You can use pandas.to_datetime() by providing the format explicitly using the format keyword argument. Example -

import pandas as pd
pd.to_datetime(dataframe['column'],format='%d%b%y:%H:%M:%S')

The explanation for the format -

%d - 2 digit date
%b - 2 letter month abbreviation
%y - 2 digit year
%H - 2 digit hour (24-hour format)
%M - Minutes
%S - Seconds

Demo -

In [46]: pd.to_datetime('29JAN14:21:16:00') #Not working
Out[46]: '29JAN14:21:16:00'

In [48]: pd.to_datetime('29JAN14:21:16:00',format='%d%b%y:%H:%M:%S') #Working
Out[48]: Timestamp('2014-01-29 21:16:00')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM