简体   繁体   中英

Convert Pandas Column to DateTime With Rare Date Format

I have one column on my dataframe that follows this date format:

17 MAY2016

I've tried to follow this reference: http://strftime.org/ and pandas.to_datetime reference: http://pandas.pydata.org/pandas-docs/version/0.20/generated/pandas.to_datetime.html

My code is as follows: df1 =df1.apply(pandas.to_datetime, errors='ignore', format='%d %b%Y')

I also tried: format='%d/%b%Y' format='%d /%b%Y' and still doesn't work. The date column type is still and object. Any ideas? Thanks in advance

You can use to_datetime only:

df = pd.DataFrame({'date':['17 MAY2016']})

df['date'] = pd.to_datetime(df['date'])
print (df)
        date
0 2016-05-17

If want format parameter:

df['date'] = pd.to_datetime(df['date'], format='%d %b%Y')
print (df)
        date
0 2016-05-17

If some non date values add errors='coerce' for convert them to NaT :

df['date'] = pd.to_datetime(df['date'], errors='coerce')

EDIT:

For check use dtypes :

print (df.dtypes)
date    datetime64[ns]
dtype: object

您无需使用.applyto_datetime函数可以在熊猫Series对象上本地运行。

df1['date column'] = pd.to_datetime(df1['date column'], errors='ignore') 

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