I have data in a csv file with dates stored as strings in a standard UK format - %d/%m/%Y
- meaning they look like:
12/01/2012
30/01/2012
The examples above represent 12 January 2012 and 30 January 2012.
When I import this data with pandas version 0.11.0 I applied the following transformation:
import pandas as pd
...
cpts.Date = cpts.Date.apply(pd.to_datetime)
but it converted dates inconsistently. To use my existing example, 12/01/2012 would convert as a datetime object representing 1 December 2012 but 30/01/2012 converts as 30 January 2012, which is what I want.
After looking at this question I tried:
cpts.Date = cpts.Date.apply(pd.to_datetime, format='%d/%m/%Y')
but the results are exactly the same. The source code suggests I'm doing things right so I'm at a loss. Does anyone know what I'm doing wrong?
read_csv
? Because then you can do it directly while reading in.read_csv
. Could you tell what function does the date conversion, and does it handle my formatting issues?cpts[['Date']].apply(pd.to_datetime, ...)
as a workaround (due to the double [ it will return a dataframe with one column). But note that it should also work on a Series (it is a bug it doesn't), and that the easier way is just to callpd.to_datetime(..)
on the column directly as @AndyHayden pointed out in his answer or to do the conversion inread_csv
.