How do I find missing values in a pandas table?

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.

import numpy as np
import pandas as pd

prices = pd.DataFrame({
    "date": pd.to_datetime(["2026-03-02", "2026-03-03", "2026-03-04"] * 2 + ["2026-03-02", "2026-03-03"]),
    "ticker": ["AAA"] * 3 + ["BBB"] * 3 + ["CCC"] * 2,
    "close": [101.2, np.nan, 102.8, 55.1, 55.6, np.nan, 230.0, 231.5],
    "volume": [12000, 11500, np.nan, 8000, 8200, 7900, np.nan, 15100],
})
print(prices)
print(prices.isna().sum(), prices.isna().mean())
        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.

print(prices[prices.isna().any(axis=1)])
print(len(prices.dropna()), len(prices.dropna(subset=["close"])))
        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.

aaa = prices.loc[prices["ticker"] == "AAA", "close"]
print(pd.DataFrame({"zero": aaa.fillna(0).pct_change(fill_method=None), "gap": aaa.pct_change(fill_method=None)}))
   zero  gap
0   NaN  NaN
1  -1.0  NaN
2   inf  NaN

Dropping 3 March would turn the change from 2 to 4 March into a false one-day return.

Your turn

Print the tickers of the rows without a close, each once. You should see ['AAA', 'BBB'].

.unique() keeps each ticker once, in order of appearance.

print(prices.loc[prices["close"].isna(), "ticker"].unique().tolist())
['AAA', 'BBB']