Changing datetime format

timeseries
pandas
python

#1

I am constantly getting a ValueError that date time format doesn’t match.
I want all the date values to change into the format of YYYY-MM-DD.
As there are some values in the format of DD/MM/YYYY.

And after running the code I get such values -
0 2016-01-01
1 2016-02-01
2 2016-03-01
3 2016-04-01
4 2016-05-01
5 2016-06-01
6 2016-07-01
7 2016-08-01
8 2016-09-01
9 2016-10-01
10 2016-11-01
11 2016-12-01
12 2016-01-13
13 2016-01-14
14 2016-01-15
15 2016-01-16

I have tried using pd.to_datetime and dt.strftime


#2

Hello Ashish,

The primary issue here is non-consistent date format in your date column. I would suggest to use a wrapper around pandas pd.to_datetime class.

def custom_parse_date(date_val):
return pd.to_datetime(date_val)

and apply it to your series.

Below is a simulation for same:

image

Hope this helps :slight_smile:


#3

Thanks for your help.
It worked.