I have a Pandas dataFrame, with a column of Date:
ID Amount raw-Date ZIP transaction-ID Date flag
749 145552 $100.00 1/15/2018 27614-7901 1342-P0192-F43 1/15/2018 1.0
1307 145552 $100.00 3/15/2018 27614-7901 1342-P0192-F43 3/15/2018 1.0
1672 145552 $100.00 2/15/2018 27614-7901 1342-P0192-F43 2/15/2018 1.0
3508 145552 $100.00 4/15/2018 27614-7901 1342-P0192-F43 4/15/2018 1.0
4144 145552 $250.00 4/24/2018 27614-7901 1234-O8910-B32 4/24/2018 1.0
4145 145552 $100.00 4/24/2018 27614-7901 1234-O8910-B32 4/24/2018 1.0
4787 145552 $100.00 5/15/2018 27614-7901 1342-P0192-F43 5/15/2018 1.0
8350 145552 $212.44 12/21/2018 27614-7901 1342-P0192-F43 12/21/2018 1.0
When I sort them by the Date column, i.e., using data.sort_values('Date'), I get:
ID Amount raw-Date ZIP transaction-ID Date flag
749 145552 $100.00 1/15/2018 27614-7901 1342-P0192-F43 1/15/2018 1.0
8350 145552 $212.44 12/21/2018 27614-7901 1342-P0192-F43 12/21/2018 1.0
1672 145552 $100.00 2/15/2018 27614-7901 1342-P0192-F43 2/15/2018 1.0
1307 145552 $100.00 3/15/2018 27614-7901 1342-P0192-F43 3/15/2018 1.0
3508 145552 $100.00 4/15/2018 27614-7901 1342-P0192-F43 4/15/2018 1.0
4144 145552 $250.00 4/24/2018 27614-7901 1234-O8910-B32 4/24/2018 1.0
4145 145552 $100.00 4/24/2018 27614-7901 1234-O8910-B32 4/24/2018 1.0
4787 145552 $100.00 5/15/2018 27614-7901 1342-P0192-F43 5/15/2018 1.0
in which apparently it sorts dates as a string. I tried pd.to_datetime(data['Date']) and again got same sorted result:
ID Amount raw-Date ZIP Appeal ID Date flag
749 145552 $100.00 1/15/2018 27614-7901 1342-P0192-F43 2018-01-15 1.0
8350 145552 $212.44 12/21/2018 27614-7901 1342-P0192-F43 2018-12-21 1.0
1672 145552 $100.00 2/15/2018 27614-7901 1342-P0192-F43 2018-02-15 1.0
1307 145552 $100.00 3/15/2018 27614-7901 1342-P0192-F43 2018-03-15 1.0
3508 145552 $100.00 4/15/2018 27614-7901 1342-P0192-F43 2018-04-15 1.0
4144 145552 $250.00 4/24/2018 27614-7901 1234-O8910-B32 2018-04-24 1.0
4145 145552 $100.00 4/24/2018 27614-7901 1234-O8910-B32 2018-04-24 1.0
4787 145552 $100.00 5/15/2018 27614-7901 1342-P0192-F43 2018-05-15 1.0
I appreciate any help.
data['Date']=pd.to_datetime(data['Date'])didn't work? You have to reassign back to data['Date']. Checkdata.info()to see that the dtype of Date column is after pd.to_datetime.df = df.sort_values('Date')?