Metadata-Version: 2.5
Name: nacheck
Version: 0.1.0
Summary: Find every missing value in a pandas DataFrame, including hidden ones like -999 and "N/A", see where and why, and get the right fix for each column.
Project-URL: Homepage, https://github.com/AnmolAdhikari/nacheck
Project-URL: Issues, https://github.com/AnmolAdhikari/nacheck/issues
Project-URL: Changelog, https://github.com/AnmolAdhikari/nacheck/blob/main/CHANGELOG.md
Author-email: Anmol Adhikari <anmoladhikari03@gmail.com>
License-Expression: MIT
License-File: LICENSE
Keywords: data-cleaning,data-quality,data-science,eda,imputation,missing-data,missing-values,nan,pandas,sentinel-values,statistics
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Education
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Education
Classifier: Topic :: Scientific/Engineering :: Information Analysis
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: numpy>=1.23
Requires-Dist: pandas>=2.0
Provides-Extra: dev
Requires-Dist: build; extra == 'dev'
Requires-Dist: matplotlib>=3.7; extra == 'dev'
Requires-Dist: pytest>=7; extra == 'dev'
Requires-Dist: ruff; extra == 'dev'
Requires-Dist: twine; extra == 'dev'
Provides-Extra: plot
Requires-Dist: matplotlib>=3.7; extra == 'plot'
Description-Content-Type: text/markdown

# nacheck

[![PyPI](https://img.shields.io/pypi/v/nacheck.svg)](https://pypi.org/project/nacheck/)
[![CI](https://github.com/AnmolAdhikari/nacheck/actions/workflows/ci.yml/badge.svg)](https://github.com/AnmolAdhikari/nacheck/actions/workflows/ci.yml)
[![Python](https://img.shields.io/pypi/pyversions/nacheck.svg)](https://pypi.org/project/nacheck/)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)

**Find every missing value in a pandas DataFrame (including the hidden ones), see where and why they are
missing, and get the right fix for each column.**

`df.isna()` only sees real `NaN`. Real data hides its gaps as `-999`, `9999`, `"N/A"`, `"unknown"`,
`"?"` or a blank string, and those silently wreck your averages and models. `nacheck` finds them all,
tells you **where** they are, **why** they are missing, and **what to do** about each column, then fixes it
in one line.

```bash
pip install nacheck
```

## Quick start

```python
import nacheck

df = nacheck.make_messy()                 # a realistic messy survey (or use your own DataFrame)
report = nacheck.scan(df, target="passed")
print(report)
```

Real output:

```text
nacheck: 1,000 rows x 11 columns · 1,526 missing values (14%), of which 278 were hidden
[HIDDEN] gender: 'unknown' x37, ' ' x25
[HIDDEN] income: -999 x61 -> -999 pulls the mean from 35.6 to -32.3
[HIDDEN] distance_km: 9999 x40 -> 9999 pulls the mean from 2.82 to 405.1
[HIDDEN] exam_score: 'N/A' x32, 'absent' x41 (numbers stored as text)
[HIDDEN] email: 'N/A' x42

age: 37 missing (3.7%) in rows 38, 41, 71, 161, 205, ...
    fix:  fill with the mean  (roughly symmetric and the gaps look random)
gender: 68 missing (6.8%) in rows 13, 17, 38, 51, 55, ...
    fix:  fill with 'Missing'  (too many gaps to give them all the most common value)
income: 132 missing (13%) in rows 3, 11, 13, 21, 36, ...
    why:  missing 40% of the time when region = Mountain vs 6.7% otherwise
    note: rows missing income have a lower passed (0.31 vs 0.45): the gap itself is informative
    fix:  fill with the median by 'region' + add 'income_was_missing'  (region explains 35% of its variation, so one overall value would be wrong; the flag keeps the information that it was missing)
distance_km: 46 missing (4.6%) in rows 33, 38, 43, 48, 53, ...
    fix:  fill with the median by 'region'  (region explains 25% of its variation, so one overall value would be wrong)
exam_score: 79 missing (7.9%) in rows 3, 6, 26, 36, 38, ...
    fix:  fill with the median by 'region'  (region explains 16% of its variation, so one overall value would be wrong)
phone: 125 missing (12%) in rows 2, 4, 8, 16, 30, ...
    fix:  fill with 'Missing'  (free text or unique values (names, phones, emails) cannot be guessed, so mark them)
email: 125 missing (12%) in rows 2, 4, 8, 16, 30, ...
    fix:  fill with 'Missing'  (free text or unique values (names, phones, emails) cannot be guessed, so mark them)
comments: 906 missing (91%) in rows 0, 1, 2, 3, 4, ...
    fix:  drop the column  (91% missing is too much to fill reliably)
passed: 8 missing (0.8%) in rows 205, 348, 455, 525, 733, ...
    fix:  drop the rows  (never fill the target you want to predict)
[TOGETHER] phone and email are missing in the same rows (100% overlap): probably one skipped section or failed source
[ROWS] 7 rows are mostly empty (>= 50% missing): 38, 205, 226, 501, 545, ... -> drop them

Apply all fixes:  clean = report.fix()
```

Then fix everything in one line:

```python
clean = report.fix()
print("\n".join(report.fix_log))
```

```text
revealed 278 hidden missing values as NaN
dropped column(s): comments
dropped 7 mostly-empty row(s)
dropped 7 row(s) missing passed
age: filled 30 with the mean 16.051
gender: filled 61 with the value 'Missing'
income: filled 125 with the median by region (Hill 31, Mountain 18, Terai 32.4, Valley 51.6)
distance_km: filled 39 with the median by region (Hill 1.8, Mountain 4.4, Terai 1.7, Valley 1.85)
exam_score: filled 73 with the median by region (Hill 53.5, Mountain 45, Terai 54, Valley 61)
phone: filled 118 with the value 'Missing'
email: filled 118 with the value 'Missing'
added income_was_missing (125 flagged)
result: 986 rows x 11 columns, 0 missing values left
```

## What it does

| | |
|---|---|
| **Finds hidden missing values** | `-999`, `9999`, `-1` (only in non-negative columns), `"N/A"`, `"unknown"`, `"?"`, `"#N/A"`, blank strings and 30+ more, plus numbers stored as text because of an `"N/A"`. Numeric codes only count when they sit far outside the real data, so a genuine score of 99 is never flagged. |
| **Shows the damage** | "-999 pulls the mean from 35.6 to -32.3" |
| **Where** | count, percentage and the rows for every column; `report.where("income")` gives all of them |
| **Why** | whether the gaps look random or depend on another column ("missing 40% of the time when region = Mountain vs 6.7% otherwise"), and whether they predict your target |
| **What to do** | one recommendation per column, with the reason: drop the column, drop the rows, mean, median, most common value, a `"Missing"` category, **median by group**, interpolate or carry forward (time series), plus a `_was_missing` flag when the gap itself carries information |
| **Patterns** | columns that go missing together (a skipped survey section) and rows that are mostly empty |
| **Fix** | `report.fix()` applies everything and logs each change; override any column with `report.fix({"age": "median"})` |
| **No leakage** | fill values are learned once: `report.fixer.transform(test_df)` applies the training medians to new data |

![Missing-value map](https://raw.githubusercontent.com/AnmolAdhikari/nacheck/main/docs/missing_map.png)

`report.plot()` draws the map above: dark cells are real `NaN`, orange cells are hidden codes.

## How it chooses a fix

| Situation | Recommendation |
|---|---|
| it is your `target` | drop those rows (never fill what you want to predict) |
| more than 60% missing | drop the column |
| time-series data (`time_col=` or a `DatetimeIndex`) | interpolate numbers, carry other values forward |
| numeric, under 1% missing and random | drop the rows |
| numeric, another column explains at least 10% of its variation | median **by that group** |
| numeric, skewed | median |
| numeric, symmetric | mean |
| categorical, under 5% missing and random | most common value |
| categorical, more gaps or gaps that mean something | a `"Missing"` category |
| names, phones, emails, free text | a `"Missing"` marker |
| missingness depends on other columns or predicts the target | also add `<column>_was_missing` |

"Depends on another column" uses a chi-square test (categories) or Welch's test (numbers) with Bonferroni
correction, so random gaps are not mistaken for patterns: on 20 datasets with purely random gaps it raised no
false alarms, and this is checked in the test suite.

## More

```python
report.to_frame()                 # one row per column: where, pattern, fix, reason
report.plan                       # the recommended plan as a dict, edit and pass to fix()
nacheck.reveal(df)                # just turn hidden codes into real NaN
df.nacheck.scan(target="y")       # DataFrame accessor
nacheck.scan(df, text_sentinels=["n.k."], numeric_sentinels=[-7], ignore=["None"])

# train / test without leakage
report = nacheck.scan(train, target="y")
train_clean = report.fix()
test_clean = report.fixer.transform(test, drop_rows=False)
```

## Also by the author

* [gapnet](https://github.com/AnmolAdhikari/gapnet): an interpretable neural network that finds how effects differ between groups
* [simpsonscan](https://github.com/AnmolAdhikari/simpsonscan): find Simpson's paradoxes in your data
* [chartaudit](https://github.com/AnmolAdhikari/chartaudit): catch misleading charts
* [pandas-mergeguard](https://github.com/AnmolAdhikari/mergeguard): predict and audit pandas merges

MIT licensed. Contributions welcome.
