Ravindra BagaleCourses & study guides मराठी Track your progress

23. Common pandas Mistakes and Fixes

23.2 Wrong dtypes and Silent NaNs

Why. Amount column looks numeric on screen (1,299) but dtype is object. Mean fails or skips. errors="coerce" quietly turns junk into NaN — good if you count NaNs, bad if you ignore them.

What.

  • object columns often hold messy text numbers.
  • pd.to_numeric(..., errors="coerce") → bad strings become NaN.
  • Date strings without dayfirst=True can swap day/month (05-10-2026 vs 10-05-2026).

How.

Type hygiene

  1. df.dtypes and df.isna().sum() right after load.
  2. Strip Indian commas before to_numeric.
  3. Print how many values became NaN after coerce.
  4. Parse dates with dayfirst=True for dd-mm-yyyy.
  5. Never compute GMV until amount is numeric.

Worked example A — Comma + N/A.

import numpy as np
import pandas as pd

raw = pd.DataFrame({
    "order_id": ["BLK-10", "BLK-11", "AMN-12", "BLK-13"],
    "city": ["Pune", "Solapur", "Nagpur", "Nashik"],
    "amount": ["64", "1,299", "N/A", "210"],
    "customer": ["Shraddha Bagale", "Salman", "Rani", "Shahrukh"],
})
before_na = raw["amount"].isna().sum()
amt = (
    raw["amount"].astype(str)
      .str.replace(",", "", regex=False)
      .replace({"N/A": np.nan, "nan": np.nan, "": np.nan})
)
raw["amount"] = pd.to_numeric(amt, errors="coerce")
print(raw.dtypes)
print("NaNs introduced", raw["amount"].isna().sum() - before_na)
print(raw)

What you should see. One NaN for AMN-12. GMV must decide: drop, fill, or exclude with an explicit filter — and log the count.

Worked example B — Silent mean on object (trap).

messy = pd.Series(["100", "200", "x"])
# pandas may error or coerce depending on version/path — always clean first
clean = pd.to_numeric(messy, errors="coerce")
print(clean.tolist(), "mean skipping NaN", clean.mean())

Worked example C — Date swap risk.

dates = pd.Series(["05-10-2026", "13-10-2026"])  # 5 Oct, 13 Oct
print(pd.to_datetime(dates, dayfirst=True))
# Without dayfirst, 05-10 may become 10 May in some settings

Zoya’s Diwali window filter breaks if 05-10 becomes May.

Practice task

Clean a column ["90", "1,150", "—", "2,499"] to floats, count NaNs, and print sum of non-null amounts.