Sub topic 9
Empty bins: NaN, zero, and min_count
This chapter assumes you can build a DataFrame, set a DatetimeIndex, and call resample() with a frequency string such as "MS". It covers what pandas returns for a bin that contains no rows, and how to control that result.
Why empty bins appear
resample() builds a regular grid of bins from the first timestamp to the last. If your data has a gap longer than one bin, the grid still includes the bins inside the gap. Those bins hold no rows.
Each aggregation has its own rule for an empty bin:
| Aggregation | Result for an empty bin |
|---|---|
| `sum()` | 0 |
| `count()` | 0 |
| `mean()`, `min()`, `max()`, `median()` | NaN |
| `first()`, `last()` | NaN |
| `ohlc()` | NaN in all four columns |
The rule follows the math. The sum of nothing is 0, but the mean of nothing is undefined.
Zero versus NaN with sum()
The data below has no rows in February.
import pandas as pd
df = pd.DataFrame(
{
"date": pd.to_datetime(["2024-01-05", "2024-01-20", "2024-03-02"]),
"sales": [100, 150, 80],
}
)
monthly = df.resample("MS", on="date")["sales"].sum()
print(monthly)
sparse = df.resample("MS", on="date")["sales"].sum(min_count=1)
print(sparse)The first result shows February as 0. The second shows February as NaN, and the dtype changes from integer to float because integers cannot hold NaN.
Both results are defensible, and the right one depends on what a missing month means:
- If no rows means no sales happened, 0 is correct.
- If no rows means the data was not collected, 0 is misleading. A month with a recording outage would look like a month with no business.
What min_count does
min_count is the minimum number of non-NaN values a bin needs before pandas computes the result. If a bin has fewer, the result is NaN.
min_count=0is the default forsum()andprod(). An empty bin gives 0 forsum()and 1 forprod().min_count=1turns an empty bin into NaN.min_count=3also turns a bin with only one or two valid values into NaN. This is useful when a monthly total built from two days of data would be misleading.
Note that min_count counts non-NaN values. A bin that has rows whose values are all NaN also falls below the threshold.
Aggregations that already give NaN
For mean() you do not need an option, because empty bins are already NaN.
import pandas as pd
df = pd.DataFrame(
{
"date": pd.to_datetime(["2024-01-05", "2024-01-20", "2024-03-02"]),
"sales": [100, 150, 80],
}
)
print(df.resample("MS", on="date")["sales"].mean())
print(df.resample("MS", on="date")["sales"].count())The mean for February is NaN, and the count is 0. Comparing the two is a quick way to find which bins were empty.
Choosing what to do with the NaN
Once empty bins are NaN, you decide how to handle them:
- Keep the NaN, so that gaps stay visible in plots and later calculations.
- Replace them with
fillna(0)when zero is the real meaning. - Drop them with
dropna()when you only want bins that contain data. - Fill them from neighbors with
ffill()orinterpolate()on the result.
Exercise
A sensor logs readings on 2024-05-01 and 2024-05-03 only. You run s.resample("D").sum(). What does 2024-05-02 contain, and how do you change the call so that it contains NaN?
Show answer
It contains 0, because the sum of an empty bin is 0 by default. Use s.resample("D").sum(min_count=1) so a bin with no valid values returns NaN. The result is a float series.