0

I have data of Jan-91 (mmm-YY) format. I am trying to convert is using pandas date_time function with code - data['Month'] = pd.to_datetime(data['Month'], format = '%ddd-%YY') However getting the error as time data 'Jan-91' does not match format '%ddd-%YY' (match)

time data 'Jan-91' does not match format '%ddd-%YY' (match)

I am trying to expecting the output should be DD-MM_YYYY format.

Yogesh
  • 1
  • 1
  • Does this answer your question? [Convert string "Jun 1 2005 1:33PM" into datetime](https://stackoverflow.com/questions/466345/convert-string-jun-1-2005-133pm-into-datetime) – Sri Harsha Kappala May 23 '23 at 04:58

2 Answers2

1

your datetime format is a little off. Instead of %ddd-%YY it should read "%b-%y". i.e. the code snippet should read:

data['Month'] = pd.to_datetime(data['Month'], format = '%b-%y')

If you look at the documentation for pandas.to_datetime, under format, it says that it uses strftime syntax to get the date format. Under strftime and strptime format codes, you will see the correct format codes are %b for a 3 letter month abbreviation, and %y for a zero-padded year without a century.

Then, to get the date into your desired format, you will need a second line to reassign the Month column:

data['Month'] = data['Month'].dt.strftime(date_format = '%d-%m_%Y')
Naum Raskind
  • 174
  • 4
0

According the Pandas documents, the format argument is only for parsing not for the destination format. So to get your desired output, you have to first parse into the default format, and then use the `` to produce your expected format:

data['Month'] = pd.to_datetime(data['Month'])
data['Month'] = data['Month'].dt.strftime('%d-%m_%Y')

HRezaei
  • 31
  • 4