Sub topic 11
resample vs asfreq vs rolling
This chapter assumes you can already use resample() with a DatetimeIndex and frequency strings. The three methods here all work on time-indexed data, but they answer different questions. Picking the wrong one gives results that look plausible and are wrong.
What each method does
resample(rule)groups rows into time bins, then reduces each bin with an aggregation such assum()ormean(). Every row in the data contributes to some bin.asfreq(freq)builds a new index at the given frequency and looks up the value at each timestamp. It does not aggregate. Rows that fall between the new timestamps are dropped.rolling(window)slides a window over the rows and computes a statistic at each position. The output has the same index as the input, so the frequency does not change.
import pandas as pd
idx = pd.date_range("2024-01-01", periods=10, freq="D")
s = pd.Series(range(10), index=idx, dtype="float")
print(s.resample("W").sum())
print(s.asfreq("W"))
print(s.rolling(3).mean())The weekly sum gives 21.0 for 2024-01-07 and 24.0 for 2024-01-14. asfreq("W") returns one row, 2024-01-07 with the value 6.0. That is the value stored at that exact timestamp, and the other six days of the week are ignored. The rolling mean returns all ten rows. The first two are NaN because a window of 3 needs three observations.
Choosing between them
| Task | Method | Output index |
|---|---|---|
| Weekly total of daily sales | `resample("W").sum()` | One row per bin |
| Value on each Sunday, no aggregation | `asfreq("W")` | One row per timestamp |
| Insert missing days into a daily series | `asfreq("D")` | Regular grid, NaN where absent |
| 7-day moving average | `rolling(7).mean()` | Same as input |
Where asfreq and resample differ
asfreq is a lookup, so it returns NaN whenever the exact timestamp is missing. resample(...).last() takes the last observation inside the bin, so it still returns a value.
import pandas as pd
idx = pd.to_datetime(["2024-01-05", "2024-01-06", "2024-01-14"])
s = pd.Series([1.0, 2.0, 3.0], index=idx)
print(s.asfreq("W"))
print(s.resample("W").last())Here no observation falls on 2024-01-07, which is a Sunday. asfreq("W") returns NaN for it, while resample("W").last() returns 2.0, the value from 2024-01-06. If the data is a state sampled at irregular times, such as a price, last() is usually what you want. Use asfreq only when the values already sit on the target grid.
asfreq also accepts method="ffill" or fill_value=0. This fills only the rows it inserts. It does not fill NaN values that were already in the data.
Rolling windows are not bins
With an integer window, rolling(7) means 7 rows. If days are missing, those rows cover more than a week. Give it a time-based window such as rolling("7D") to cover a fixed span regardless of row count. This needs a sorted DatetimeIndex.
Exercise
A daily series is missing Saturday and Sunday for one week. You want one value per week ending Sunday, equal to the last known reading in that week. Should you use asfreq("W") or resample("W").last(), and why?
Show answer
Use resample("W").last(). asfreq("W") looks up the exact Sunday timestamp, which is missing, so it returns NaN. last() returns the last observation inside the bin, which is Friday's reading.