问题
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'
回答1:
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.
回答2:
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()
来源:https://stackoverflow.com/questions/16910795/pandas-to-datetime-error