简体   繁体   中英

Converting dataframe to datetime in pandas

I have a dataframe called 'times', the head of which looks like this:

year    month   day hour    minute  second
0   2015    02  03  01  12  04
1   2015    02  03  01  12  07
2   2015    02  03  01  12  11
3   2015    02  03  01  12  13
4   2015    02  03  01  12  17

When I try to put all of these together into a single datetime series like this:

timeData = pd.to_datetime(times)

it throws this error:

TypeError: arg must be a string, datetime, list, tuple, 1-d array, or Series

Why does it throw this error, and how can I fix it?

Your solution works for me as well.

But you can also try:

times.apply(lambda x: pd.datetime(*x), axis=1)

Or:

times.T.apply(lambda x: pd.datetime(*x))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM