【发布时间】:2020-10-20 17:39:47
【问题描述】:
对于某些感兴趣的日期,我有一个对应于月底的时间序列:
Date
31-01-2005 0.0
28-02-2006 0.0
30-06-2020 0.0
Name: Whatever, dtype: float64
我想在每个数据点之前使用两个月的样本来扩展此数据框的索引,从而生成以下数据框:
Date
30-11-2004 NaN
31-12-2004 NaN
31-01-2005 0.0
31-12-2005 NaN
31-01-2006 NaN
28-02-2006 0.0
30-04-2020 NaN
31-05-2020 NaN
30-06-2020 0.0
Name: Whatever, dtype: float64
我该怎么做?请注意,我只对结果索引感兴趣。
我天真的尝试是这样做的:
df.index.apply(lambda x: [x - pd.DateOffset(months=2), x - pd.DateOffset(months=1), x])
但是 index 没有 apply 功能。
【问题讨论】:
标签: python pandas dataframe indexing time-series