Pandas - ValueError: Error parsing datetime string "17-Jan-23" at position 3


Keywords:python 


Question: 

I have the following code where I am reading date column:

data = pd.DataFrame(array, columns=names)

data[['D_DATE']] = data[['D_DATE']].astype('datetime64')

But this is giving me error:

ValueError: Error parsing datetime string "17-Jan-23" at position 3

Can someone help how can I resolve this.


1 Answer: 

Try this:

data['D_DATE'] = pd.to_datetime(data['D_DATE'])

Indexing a single column with double brackets (df[['D_DATE']]) returns a DataFrame with one column named 'D_DATE'. Indexing with a single set of brackets (df['D_DATE']) returns a Series named 'D_DATE'. To create a new column in a DataFrame using the form df[new_col], use single brackets.