Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas datetime giving wrong output

I am working with a pandas dataframe with date column. I have converted the dtype of this column from object to datetime using pandas pd.to_datetime:

Input:

0     30-11-2019
1     31-12-2019
2     31-12-2019
3     31-12-2019
4     31-12-2019
5     21-01-2020
6     27-01-2020
7     01-02-2020
8     01-02-2020
9     03-02-2020
10    15-02-2020
11    12-03-2020
12    13-03-2020
13    31-03-2020
14    31-03-2020
15    04-04-2020
16    04-04-2020
17    04-04-2020
ta['transaction_date'] = pd.to_datetime(ta['transaction_date'])

Output:

0    2019-11-30
1    2019-12-31
2    2019-12-31
3    2019-12-31
4    2019-12-31
5    2020-01-21
6    2020-01-27
7    2020-01-02
8    2020-01-02
9    2020-03-02
10   2020-02-15
11   2020-12-03
12   2020-03-13
13   2020-03-31
14   2020-03-31
15   2020-04-04
16   2020-04-04
17   2020-04-04

As you can see that the 11th output after converting it into datetime is wrong month is swapped with day.This is affecting my further analysis. How can I sort this out.

like image 493
Shyam Avatar asked Oct 24 '25 09:10

Shyam


1 Answers

Use dayfirst=True parameter or specify format, because pandas by default matching months first, if possible:

a['transaction_date'] = pd.to_datetime(ta['transaction_date'], dayfirst=True)

Or:

a['transaction_date'] = pd.to_datetime(ta['transaction_date'], format='%d-%m-%Y')
like image 112
jezrael Avatar answered Oct 26 '25 00:10

jezrael