Follow on question to: Python pandas for reading in file with date
I am not able to parse the date on the dataframe below. The code is as follows:
df = pandas.read_csv(file_name, skiprows = 2, index_col='datetime',
parse_dates={'datetime': [0,1,2]}, delim_whitespace=True,
date_parser=lambda x: pandas.datetime.strptime(x, '%Y %m %d'))
OTH-000.opc
XKN1= 0.500000E-01
Y M D PRCP VWC1
2006 1 1 0.0 0.17608E+00
2006 1 2 6.0 0.21377E+00
2006 1 3 0.1 0.22291E+00
2006 1 4 3.0 0.23460E+00
2006 1 5 6.7 0.26076E+00
I get an error saying: lambda () takes exactly 1 argument (3 given)
Based on @EdChum's comment below, if I use this code:
df = pandas.read_csv(file_name, skiprows = 2, index_col='datetime', parse_dates={'datetime': [0,1,2]}, delim_whitespace=True))
df.index results in an object and not a datetime series
df.index
Index([u'2006 1 1',u'2006 1 2'....,u'nan nan nan'],dtype='object')
Finally the file is available here:
df = pandas.read_csv(file_name, skiprows = 2, index_col='datetime', parse_dates={'datetime': [0,1,2]}, delim_whitespace=True))as this works for me, it seems the pandas parser is man/woman enough to handle your date formatdf.index = pd.to_datetime(df.index)this shouldn't be necessary but it should work for you