【问题标题】:Pandas. How to resample with last day of moth?熊猫。如何在一个月的最后一天重新采样?
【发布时间】:2020-08-18 02:11:21
【问题描述】:

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

>> 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 结尾。我想让重新采样的 y2017-04-30 结尾。我该怎么做?

【问题讨论】:

  • 2017-04-03 到 moth 结束的值是多少? 29.043835?

标签: python pandas time-series


【解决方案1】:

添加一个包含最后一天 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 2019-01-16
    • 2020-10-19
    • 1970-01-01
    • 2014-08-09
    • 2022-06-10
    • 2021-06-01
    相关资源
    最近更新 更多