我有一个关于将tseries.period.Periodindex转换为日期时间的问题.
colors country time_month 2010-09 xxx xxx 2010-10 xxx xxx 2010-11 xxx xxx ...
time_month是索引.
type(df.index)
回报
class 'pandas.tseries.period.Periodindex'
当我尝试使用df进行VAR分析时(http://statsmodels.sourceforge.net/devel/vector_ar.html#vector-autoregressions-tsa-vector-ar),
VAR(mdata)
收益:
Given a pandas object and the index does not contain dates
显然,Period不被认为是日期时间.现在,我的问题是如何将索引(time_month)转换为VAR分析可以使用的日期时间?
df.index = pandas.DatetimeIndex(df.index)
回报
cannot convert Int64Index->DatetimeIndex
谢谢您帮忙!
解决方法:
您可以使用Periodindex的to_timestamp方法:
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
在旧版本的Pandas中,方法是to_datetime
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。