Pandas Inconsistent Date-time Format
I started using pandas library about a fortnight back. Learning the new features. I would appreciate help on the following problem. I have a column with dates in mixed format. Thes
Solution 1:
The real problem is that there are ambiguous dates in your dataset (do you parse it as mm/dd/yyyy or dd/mm/yyyy if it could be either?? (I've been here, and we decided just to pick what the majority seemed to be; essentially the dataset was compromised... and we had to treat it as such).
If it's a Series then hitting it with pd.to_datetime seems to work:
In [11]: s = pd.Series(['6/5/2016', '7/5/2016', '7/5/2016', '7/5/2016', '9/5/2016', '9/5/2016', '9/5/2016', '9/5/2016', '5/13/2016', '5/14/2016', '5/14/2016'])
In [12]: pd.to_datetime(s)
Out[12]:
0 2016-06-05
1 2016-07-05
2 2016-07-05
3 2016-07-05
4 2016-09-05
5 2016-09-05
6 2016-09-05
7 2016-09-05
8 2016-05-13
9 2016-05-14
10 2016-05-14
Name: 0, dtype: datetime64[ns]
Note: If you had a consistent format you can pass it in explicitly:
In [13]: pd.to_datetime(s, format="%m/%d/%Y")
Out[13]:
0 2016-06-05
1 2016-07-05
2 2016-07-05
3 2016-07-05
4 2016-09-05
5 2016-09-05
6 2016-09-05
7 2016-09-05
8 2016-05-13
9 2016-05-14
10 2016-05-14
Name: 0, dtype: datetime64[ns]
Post a Comment for "Pandas Inconsistent Date-time Format"