1

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

User 6683331
  • 692
  • 1
  • 13
  • 31

2 Answers2

2

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

You don't need to use .apply, the to_datetime function natively works on pandas Series objects.

df1['date column'] = pd.to_datetime(df1['date column'], errors='ignore') 
James
  • 32,991
  • 4
  • 47
  • 70