Sub topic 6
Upsampling: creating a finer time grid
This chapter assumes you can build a DatetimeIndex, read frequency strings such as "D" and "W-MON", and have seen downsampling with resample().
What upsampling does
Upsampling converts data to a finer frequency than it was recorded at, for example weekly values to daily rows. resample() builds a new, regular time grid. Each new timestamp that has no original observation gets a missing value (NaN).
Upsampling cannot invent data. It only adds rows. Choosing the values for those rows is a separate step, covered in the next chapter.
Creating the grid with asfreq()
After resample(), call asfreq() to get the new grid with no filling applied.
import pandas as pd
idx = pd.date_range("2024-01-01", periods=3, freq="W-MON")
s = pd.Series([10.0, 20.0, 30.0], index=idx)
daily = s.resample("D").asfreq()
print(daily)The result has 15 rows, from 2024-01-01 to 2024-01-15. Only 2024-01-01, 2024-01-08 and 2024-01-15 hold values. The other twelve rows are NaN.
Two details about the grid:
- The grid starts at the first timestamp and ends at the last one. It does not extend past the last original observation.
- Original values keep their exact timestamps only if they fall on the new grid. If they do not, they are dropped.
import pandas as pd
idx = pd.to_datetime(["2024-01-01 00:00", "2024-01-01 07:30"])
s = pd.Series([1.0, 2.0], index=idx)
print(s.resample("h").asfreq())The value at 07:30 does not land on an hourly timestamp, so asfreq() drops it. The output has eight hourly rows, 00:00 to 07:00, and only 00:00 holds a value (1.0). The 07:30 value is lost. To keep it, use an aggregation such as last() or mean(), which places each value in its bin.
Dtype changes
NaN is a float. An integer series becomes float64 once new rows appear.
import pandas as pd
idx = pd.date_range("2024-01-01", periods=2, freq="2D")
s = pd.Series([1, 2], index=idx)
print(s.dtype)
print(s.resample("D").asfreq().dtype)This prints int64 and then float64. If you need integers afterward, fill the gaps first, then convert with astype().
Aggregations on empty bins
Methods like sum() also work on an upsampled grid, but they treat empty bins differently from asfreq().
import pandas as pd
idx = pd.date_range("2024-01-01", periods=2, freq="2D")
s = pd.Series([5.0, 7.0], index=idx)
print(s.resample("D").sum())The empty day 2024-01-02 becomes 0.0, not NaN. For upsampling, asfreq() is usually the right choice, because a sum would show zeros that look like real measurements. The chapter on empty bins covers min_count.
Common errors
- Expecting filled values.
resample("D")alone returns aResamplerobject. You must call a method on it. - Passing a frequency coarser than the data and calling it upsampling. That is downsampling and needs an aggregation.
- Forgetting that the grid stops at the last timestamp. Add a final row yourself if you need the last period fully covered.
Exercise
A series has int64 values recorded every 3 days, from 2024-03-01 to 2024-03-10. You call s.resample("D").asfreq(). How many rows result, and what dtype does the series have?
Show answer
Observations fall on March 1, 4, 7 and 10, so the daily grid spans March 1 to March 10 and has 10 rows. Six rows are NaN, so the dtype changes from int64 to float64.