熊猫如何在蛾的最后一天重新采样?

时间:2020-05-03 17:32:47

标签: python pandas time-series

我想将一个时间序列的每月样本重新采样到每天。所以,我有:

>> y

2017-01-01    15.034583
2017-02-01    16.984745
2017-03-01    21.982249
2017-04-03    29.043835
dtype: float64


>> y.resample('D').pad()

2017-01-01    15.034583
2017-01-02    15.034583
2017-01-03    15.034583
                ...    
2017-04-01    21.982249
2017-04-02    21.982249
2017-04-03    29.043835
Freq: D, Length: 93, dtype: float64

y 和重新采样的 y 均以 2017-04-03 结尾。我想重新采样的 y 2017-04-30 结尾。我该怎么办?

1 个答案:

答案 0 :(得分:2)

在最后一天用MonthEnd添加新列,并按天重新采样

from pandas.tseries.offsets import MonthEnd

y_last = y.iloc[-1:].copy()
y_last.index = y_last.index + MonthEnd(0)
y = pd.concat([y, y_last])

y.resample('1D').pad()

#               value
# 2017-01-01    15.034583
# 2017-01-02    15.034583
# 2017-01-03    15.034583
# ...   ...
# 2017-04-28    29.043835
# 2017-04-29    29.043835
# 2017-04-30    29.043835