python - Specifying date format when converting with pandas.to_datetime -


i have data in csv file dates stored strings in standard uk format - %d/%m/%y - meaning like:

12/01/2012 30/01/2012 

the examples above represent 12 january 2012 , 30 january 2012.

when import data pandas version 0.11.0 applied following transformation:

import pandas pd ... cpts.date = cpts.date.apply(pd.to_datetime) 

but converted dates inconsistently. use existing example, 12/01/2012 convert datetime object representing 1 december 2012 30/01/2012 converts 30 january 2012, want.

after looking @ this question tried:

cpts.date = cpts.date.apply(pd.to_datetime, format='%d/%m/%y') 

but results same. source code suggests i'm doing things right i'm @ loss. know i'm doing wrong?

you can use parse_dates option read_csv conversion directly while reading data.
trick here use dayfirst=true indicate dates start day , not month. see here more information: http://pandas.pydata.org/pandas-docs/dev/generated/pandas.io.parsers.read_csv.html

when dates have index:

>>> import pandas pd >>> stringio import stringio >>> s = stringio("""date,value ... 12/01/2012,1 ... 12/01/2012,2 ... 30/01/2012,3""") >>>  >>> pd.read_csv(s, index_col=0, parse_dates=true, dayfirst=true)             value date              2012-01-12      1 2012-01-12      2 2012-01-30      3 

or when dates in column:

>>> s = stringio("""date ... 12/01/2012 ... 12/01/2012 ... 30/01/2012""") >>>  >>> pd.read_csv(s, parse_dates=[0], dayfirst=true)                  date 0 2012-01-12 00:00:00 1 2012-01-12 00:00:00 2 2012-01-30 00:00:00 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -