您必须设置DatetimeIndex
数据帧,因此我将您的代码修改为:
import pandas as pd df = pd.read_csv("data.csv", index_col="Date") df.index = pd.DatetimeIndex(df.index) df = df.reindex(pd.date_range("2011-01-01", "2011-10-31"), fill_value="NaN") df.to_csv('test.csv')
这应该工作.
编辑:添加样本测试输出:
... 2011-01-24,NaN,NaN 2011-01-25,NaN,NaN 2011-01-26,NaN,NaN 2011-01-27,NaN,NaN 2011-01-28,NaN,NaN 2011-01-29,NaN,NaN 2011-01-30,NaN,NaN 2011-01-31,4.75,5.81 2011-02-01,NaN,NaN 2011-02-02,NaN,NaN 2011-02-03,NaN,NaN 2011-02-04,NaN,NaN 2011-02-05,NaN,NaN 2011-02-06,NaN,NaN ...