【问题标题】:Use pandas asfreq/resample to get the end of the period使用 pandas asfreq/resample 获取期末
【发布时间】:2015-04-14 15:39:40
【问题描述】:

我试图找出每一天的平均值,并将该值作为一年中每个小时的输入。问题是最后一天只包含第一个小时。

rng = pd.date_range('2011-01-01', '2011-12-31')
ts = pd.Series(np.random.randn(len(rng)), index=rng)
days = ts.resample('D')
day_mean_in_hours = asfreq('H', method='ffill')
day_mean_in_hours.tail(5)
2011-12-30 20:00:00   -0.606819
2011-12-30 21:00:00   -0.606819
2011-12-30 22:00:00   -0.606819
2011-12-30 23:00:00   -0.606819
2011-12-31 00:00:00   -2.086733
Freq: H, dtype: float64

有没有一种很好的方法可以将频率更改为小时并仍然获得最后一天的完整时间?

【问题讨论】:

    标签: pandas


    【解决方案1】:

    您可以使用涵盖最后一整天的每小时 DatetimeIndex 来reindex 框架。

    hourly_rng = pd.date_range('2011-01-01', '2012-01-01', freq='1H', closed='left')
    day_mean_in_hours = day_mean_in_hours.reindex(hourly_rng, method='ffill')
    

    另一个例子见Resample a time series with the index of another time series

    【讨论】:

      猜你喜欢
      • 2018-08-29
      • 1970-01-01
      • 2019-02-21
      • 1970-01-01
      • 2018-10-11
      • 1970-01-01
      • 2021-08-16
      • 2020-06-18
      • 2021-07-11
      相关资源
      最近更新 更多