3

I am trying to find the mean for each day and put that value as an input for every hour in a year. The problem is that the last day only contains the first hour.

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

Is there a nice way to change the frequency to hour and still get the full last day?

qc11
  • 31
  • 3

1 Answers1

0

You could reindex the frame using an hourly DatetimeIndex that covers the last full day.

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')

See Resample a time series with the index of another time series for another example.

Tttt1228
  • 409
  • 5
  • 11