我有一个数据框:
Seasonal
Date
2014-12 -1.089744
2015-01 -0.283654
2015-02 0.158974
2015-03 0.461538
我在 DataFrame 中使用了 pd.to_period,所以它的索引变成了 Pandas period 类型(类型’pandas._period.Period’)。
现在,我想将该索引转换为字符串。我正在尝试应用以下内容:
df.index=df.index.astype(str)
然而这不起作用…
ValueError: Cannot cast PeriodIndex to dtype |S0
从那时起我的代码就被冻结了。
紧急求救
原文由 aabujamra 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以使用
to_series
然后转换为string
: