我有一个数据框:
Seasonal
Date
2014-12 -1.089744
2015-01 -0.283654
2015-02 0.158974
2015-03 0.461538
我在DataFrame中使用了pd.to_period,因此其索引已转换为Pandas周期类型(类型为'pandas._period.Period')。
现在,我想将该索引转换为字符串。 我正在尝试应用以下内容:
df.index=df.index.astype(str)
然而那并不起作用...
ValueError: Cannot cast PeriodIndex to dtype |S0
从那时起我的代码就被冻结了。
救命啊。
df.index.strftime('%Y-%m')
对你有用吗? - Sergey Bushmanov