1

I am trying to parse dates together from the following sample set of data


No,year,month,day,hour,pm2.5,DEWP,TEMP,PRES,cbwd,Iws,Is,Ir
1,2010,1,1,0,NA,-21,-11,1021,NW,1.79,0,0
2,2010,1,1,1,NA,-21,-12,1020,NW,4.92,0,0
3,2010,1,1,2,NA,-21,-11,1019,NW,6.71,0,0
4,2010,1,1,3,NA,-21,-14,1019,NW,9.84,0,0

My code is as follows:

dateparser = lambda x: pd.datetime.strptime(x, "%Y %m %d %H")`
dataset = pd.read_csv("raw.csv", parse_dates=['year', 'month', 'day', 'hour'], index_col = 0,date_parser=mydateparser)

It's throwing this error:

ValueError: Missing column provided to 'parse_dates': 'day, hour, month, year'

Can someone help me understand why I am getting this error

1 Answer 1

1

Try it passing as dict or list of list

dataset = pd.read_csv("raw.csv", parse_dates={'date':['year', 'month', 'day',
'hour']}, index_col = 1, date_parser=dateparser)

Or

dataset = pd.read_csv("raw.csv", parse_dates=[['year', 'month', 'day',
'hour']], index_col = 1, date_parser=dateparser)

PS: Was not able to reproduce the same error, but the proposed solution should work fine.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your response passing as a list of list works fine but for whatever reason I had to reference the columns by their index number and no the col name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.