Missing data, data types, and duplicates

3.1.1

Identify invalid values, choose nullable dtypes, and resolve duplicates using the correct row grain.

On this page

Detect and convert missing values

Use isna() and notna() to inspect missing values. to_numeric(errors="coerce") converts invalid text into missing values. Keep original values or flags when the conversion needs an audit trail.

import pandas as pd

raw = pd.Series(["2", "ERROR", "4"], dtype="string")
quantity = pd.to_numeric(raw, errors="coerce").astype("Int64")
print(quantity.isna().tolist())
print(quantity.sum())
Example output
[False, True, False]
6

Text and nullable dtypes in pandas 3.x

Since pandas 3.0, text values are inferred as the dedicated str dtype instead of object. Inferred str typically represents missing values with NaN, while explicit string uses pd.NA. Use Int64 with a capital I for nullable pandas integers when missing values are allowed.

Resolve exact and key duplicates

duplicated() flags repeated rows after the first by default. Pass subset to inspect key duplicates. A repeated ID can be valid in a line-item table, so verify the intended grain before dropping rows.

import pandas as pd

frame = pd.DataFrame({"id": [1, 2, 2], "total": [2, 5, 5]})
print(frame.duplicated().sum())
print(len(frame.drop_duplicates()))
Example output
1
2

Official references