Ravindra Bagale · Data Scienceसर्व coursesया course चे lessonsशोधाEnglish

Data Science · मराठी आवृत्ती

23.2 चुकीचे dtypes आणि silent NaNs

By Ravindra Bagale

या course चे सर्व chapters (py + ch)

हे का शिकायचं?

amount object असेल तर mean चुकीचं किंवा error. to_numeric errors="coerce" शांतपणे NaN करते.

हे काय आहे?

dtypes तपासा. pd.to_numeric, astype, बद्दल जाणून घ्या. Coerce नंतरisna().sum() पाहा.

कसं करायचं?

Step 1. df.dtypes पाहा.

Step 2. Numeric column to_numeric.

Step 3. NaN count print करा.

Step 4. Unexpected NaN असल्यास raw value पाहा.

उदाहरणं: copy करा आणि चालवा

उदाहरण 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.

उदाहरण 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())

उदाहरण 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.