2
votes

I have a pandas dataframe and I want to convert the time column to datetime format.

Time

30/May/2013 06:00:41 -0600

import pandas as pd
df.index = pd.to_datetime(df.pop('Time'))

But it always gives the following error.What is the problem with the code? :(

AttributeError                            Traceback (most recent call last)
<ipython-input-124-9219cf10d027> in <module>()
----> 1 df.index = pd.to_datetime(df.pop('Time'))

AttributeError: 'module' object has no attribute 'to_datetime'
2
So, I find to_datetime method in pd.DatetimeIndex class. I don't use pandas before, but can't you get pd.DatetimeIndex object from df.pop('Time')? Or, maybe, convert it?awesoon
Which version of pandas are you using?Andy Hayden

2 Answers

0
votes

The to_datetime function was introduced in 0.8.0, so you'll have to upgrade your pandas to use it.
Ideally to the latest stable version.

-1
votes

Use set_index to set the time column as the index, and then convert the Index to DateTimeIndex:

df = df.set_index('Time')

df.index = df.index.to_datetime()