Count the missing values in each column, find the rows and columns with a gap with isna().any(axis=…), drop only the rows that miss the value you need with dropna(subset=…), and see why a zero is no fill for a price.
After this lesson, you can find the gaps in a price table and keep the rows that have the value you need.
How do I count the missing values?
.isna() marks each missing cell with True, and .sum() down each column counts them, because True counts as 1. In a long panel of eight made-up rows, the close and the volume each miss two values, a share of 0.25.
np.nan marks a missing number. The panel has one row per stock and date, as in the lesson on long and wide panels, with closes in kronor and volumes in shares. A column with gaps prints its whole numbers as decimals, because pandas stores them as floats.
The following box builds the panel and counts its gaps.
date ticker close volume
0 2026-03-02 AAA 101.2 12000.0
1 2026-03-03 AAA NaN 11500.0
2 2026-03-04 AAA 102.8 NaN
3 2026-03-02 BBB 55.1 8000.0
4 2026-03-03 BBB 55.6 8200.0
5 2026-03-04 BBB NaN 7900.0
6 2026-03-02 CCC 230.0 NaN
7 2026-03-03 CCC 231.5 15100.0
date 0
ticker 0
close 2
volume 2
dtype: int64 date 0.00
ticker 0.00
close 0.25
volume 0.25
dtype: float64
CCC has no row for 4 March, and .isna() cannot see a row that does not exist, as the lesson on missing returns shows.
Which rows have a gap, and which do I keep?
.any(axis=1) asks across each row whether any cell is missing, so prices[prices.isna().any(axis=1)] keeps the rows with a gap, here rows 1, 2, 5 and 6. .dropna(subset=["close"]) drops only the rows without a close and keeps the six rows that have one, while .dropna() alone would drop every row with any gap and keep four.
.any(axis=0) asks down each column instead.
The following box prints the rows with a gap and counts the rows each version of dropna keeps.
date ticker close volume
1 2026-03-03 AAA NaN 11500.0
2 2026-03-04 AAA 102.8 NaN
5 2026-03-04 BBB NaN 7900.0
6 2026-03-02 CCC 230.0 NaN
4 6
What happens if I fill a missing close with zero?
A zero close makes the return of that day −100% and the next return a division by zero, which pandas shows as inf. Left missing, the returns that need the gap stay missing, which is the honest answer.
The following box computes AAA’s returns with the gap filled by zero and with the gap kept.