导出到Excel时,Pandas不保留频率格式

时间:2017-10-03 18:58:57

标签: python pandas export-to-excel datetime-format resampling

我有一个月度数据框,在重新采样到年度数据之后,我使用Pandas to_period('M')以每月格式显示索引。这很好。当我导出到excel时,它在那里是日期时间格式。 导出为ex​​cel时如何保留格式。

Jupyter Notebook中的数据示例:

2014  1463  146.416667  1110.877414  197.230546  199.230546

excel中的数据样本:

2014-01-01 00:00:00 1463    146.416667  1110.877414 197.230546  199.230546

1 个答案:

答案 0 :(得分:0)

@Arthur Gouveia:感谢您的回复!

我尝试将数据类型更改为字符串并正常工作。但任何更好的解决方案?

if type(TNA_BB2_a.index)==pd.tseries.period.PeriodIndex:
    TNA_BB2_a.index = TNA_BB2_a.index.strftime('%Y')
if type(tna_n_m_BB.index)==pd.tseries.period.PeriodIndex:
    tna_n_m_BB.index = tna_n_m_BB.index.strftime('%Y-%m')