python - Converting PeriodIndex to DateTimeIndex? -
i have question regarding converting tseries.period.periodindex datetime.
i have dataframe looks this:
colors country time_month 2010-09 xxx xxx 2010-10 xxx xxx 2010-11 xxx xxx ...
time_month index.
type(df.index)
returns
class 'pandas.tseries.period.periodindex'
when try use df var analysis (http://statsmodels.sourceforge.net/devel/vector_ar.html#vector-autoregressions-tsa-vector-ar),
var(mdata)
returns:
given pandas object , index not contain dates
so apparently, period not recognized datetime. now, question how convert index (time_month) datetime var analysis can work with?
df.index = pandas.datetimeindex(df.index)
returns
cannot convert int64index->datetimeindex
thank help!
you can use to_timestamp
method of periodindex this:
in [25]: pidx = pd.period_range('2012-01-01', periods=10) in [26]: pidx out[26]: <class 'pandas.tseries.period.periodindex'> [2012-01-01, ..., 2012-01-10] length: 10, freq: d in [27]: pidx.to_timestamp() out[27]: <class 'pandas.tseries.index.datetimeindex'> [2012-01-01, ..., 2012-01-10] length: 10, freq: d, timezone: none
in older versions of pandas method to_datetime
Comments
Post a Comment