keynesiancross keynesiancross -4 years ago 279
Python Question

Pandas Dataframe asFreq changing datatype of index

I'm having an issue when using asfreq to resample a dataframe. My dataframe, df, has an index of type Datetime.Date(). After using df.asfreq('d','pad'), my dataframe index has been changed to type pandas.tslib.Timestamp. I've tried the following to change it back but I'm having no luck...

df = df.set_index(df.index.to_datetime())
df.index = df.index.to_datetime()
df.index = pd.to_datetime(df.index)

Any thoughts?

Answer Source

use pd.to_datetime

df.index = pd.to_datetime(df.index)

This is the canonical approach to creating datetime indices. If you want your index indices to all be of type datetime.datetime then you can do this following.

df.index = pd.Index([i.to_datetime() for i in df.index],, dtype=object)

I just don't know why you'd want to.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download