# Checking Your Data with assert

assert stops the code with an AssertionError and your message whenever a condition is False, so checks on ranges with between and all, on repeated timestamps and on missing values, gathered in one check_readings function, catch a cleaning step that went wrong every time the cleaning runs.

Journeyman · Python · Updated November 19, 2026

By Gregor Dzierzon

## After this lesson you can

- Check a condition with assert and a message that says what failed, using all() and between on a column
- Write a check function that tests ranges, repeated rows and missing values, and run it after every clean

## Key takeaways

- assert, read "make sure that", does nothing when its condition is True and stops with an AssertionError when it's False.
- The message after the comma says what failed, so the error explains itself.
- all() is True only when every value is True, and any() when at least one is.
- between() says False for a missing value, so check a range on the values that are there, with dropna() first.
- A check function holds every check and runs after every clean, so a step that goes wrong stops the work instead of slipping into a report.

You cleaned a table once and the numbers looked right. Then June's readings arrive, or you change one step, and nobody is going to reread 13,000 rows to see whether the cleaning still works. Pilots don't trust their memory either: before every flight they go down the same checklist, and one "no" keeps the plane at the gate. This lesson writes that checklist for a table, so it runs every time you clean, for a class project, a lab or a job.

The example is the Ridgeview School weather station's `data/raw/readings.csv`: a reading every 30 minutes for a school year, with `timestamp`, `temp_f`, `humidity_pct`, `rain_in`, `wind_mph` and `battery_v`. The station writes -999 when its sensor drops out, turned into `NaN` here as *Values That Can't Be Right: Sentinels and Outliers* shows.

## assert: make sure that

**`assert`**, read "make sure that", is Python's checklist line. It takes a condition and a message. When the condition is `True`, nothing happens and the code carries on:

```python
import numpy as np
import pandas as pd

readings = pd.read_csv("data/raw/readings.csv").replace(-999, np.nan)
assert len(readings) > 0, "the file has no readings"
print("rows:", len(readings))
```

```text
rows: 13344
```

When it's `False`, the code stops with an **`AssertionError`** that carries your message:

```python
assert readings["humidity_pct"].max() <= 100, "humidity_pct has a reading above 100"
```

```text
AssertionError: humidity_pct has a reading above 100
```

That's the last line of the error, and the message says exactly what's wrong. Write each message for the person who reads it months from now: the column, and what the rule was.

## A whole column: between and all

`between(0, 100)` answers `True` or `False` for every humidity. An assert needs one answer, and two methods give it: **`all()`** is `True` only when every value is `True`, and **`any()`** when at least one is.

```python
in_range = readings["humidity_pct"].between(0, 100)
print("every humidity in range:", in_range.all())
print("any humidity in range:", in_range.any())
```

```text
every humidity in range: False
any humidity in range: True
```

Blank the 65 humidities above 100, as the cleaning does, and check again:

```python
humidity = readings["humidity_pct"]
readings["humidity_pct"] = humidity.mask(humidity > 100)
print("all in range:", readings["humidity_pct"].between(0, 100).all())
print("all there in range:", readings["humidity_pct"].dropna().between(0, 100).all())
```

```text
all in range: False
all there in range: True
```

Still `False`, because `between` says `False` for a missing value, and the sentinels left 61 of them. A range check is about the values that are there, so `dropna()` comes first.

## Repeated rows and missing values

Two more things go wrong in a station's file. After an outage, it sent some readings twice. And a sensor that drops out for a week leaves a hole too big to average over. The science department's rule: no more than 5% of the temperatures missing.

```python
repeats = readings["timestamp"].duplicated().sum()
missing = readings["temp_f"].isna().sum()
print("timestamps sent twice:", repeats)
print("temperatures missing:", missing)
```

```text
timestamps sent twice: 48
temperatures missing: 61
```

A message can include the count, with an f-string: the `f` before the quotes lets `{repeats}` put the number in.

```python
assert repeats == 0, f"{repeats} timestamps appear more than once"
```

```text
AssertionError: 48 timestamps appear more than once
```

## A check function, run after every clean

Put every check in one function, and call it whenever the cleaning runs:

```python
def check_readings(df):
    temps = df["temp_f"].dropna()
    assert temps.between(-30, 120).all(), "temp_f has a reading outside -30 to 120"
    humidity = df["humidity_pct"].dropna()
    assert humidity.between(0, 100).all(), "humidity_pct has a reading outside 0 to 100"
    repeats = df["timestamp"].duplicated().sum()
    assert repeats == 0, f"{repeats} timestamps appear more than once"
    missing = df["temp_f"].isna().sum()
    assert missing <= len(df) * 0.05, f"{missing} temperatures are missing, more than 5%"

check_readings(readings)
```

```text
AssertionError: temp_f has a reading outside -30 to 120
```

The six sun-on-the-sensor readings are still in `temp_f`. The function stops at the first check that fails, so you fix that step and run it again:

```python
temp = readings["temp_f"]
readings["temp_f"] = temp.mask(temp > 120)
readings = readings.drop_duplicates(subset=["timestamp"])
check_readings(readings)
print("all checks passed:", len(readings), "rows")
```

```text
all checks passed: 13296 rows
```

A check that passes prints nothing, so the last line says so itself. From now on, a cleaning step that breaks stops right here, with a message, instead of turning up as a strange number in somebody's report.

## Check yourself

2 questions to try on the lesson page.

## So what

`assert` turns "I think the data's right" into a check that runs every time: gather the range, duplicate and missing-value checks in one function, call it after every clean, and a broken step stops the work with a message that says what failed.

## Key terms

- [assert](https://www.ask-codey.com/glossary/assert)
- [AssertionError](https://www.ask-codey.com/glossary/assertionerror)

## Related

- [Values That Can't Be Right: Sentinels and Outliers](https://www.ask-codey.com/lessons/pandas-sentinels-and-outliers)
- [A Cleaning Function, Step by Step](https://www.ask-codey.com/lessons/pandas-cleaning-function)

Source: https://www.ask-codey.com/lessons/pandas-checking-data-with-assert
