Approach: Test normalize_and_fill by creating small controlled DataFrames, calling the function, and asserting properties: mean/std (or min/max) for normalization, missing values filled appropriately, dtype preserved, and no infinities/NaNs remain. Use pytest and pandas; prefer deterministic numeric tolerances (np.isclose) and parametrize where useful.Edge cases to cover:- Constant column (std=0) — normalization should avoid divide-by-zero (e.g., return zeros or original).- All-NaN column — filled with strategy (mean/global, median, zero); ensure consistent behavior and dtype.- Extremely large values — avoid overflow; check numerical stability.- Mixed dtypes, categorical columns untouched.- Single-row DataFrame.- Columns with infinities.- Empty DataFrame.Pytest examples:python
import pandas as pd
import numpy as np
from your_module import normalize_and_fill
def test_constant_column():
df = pd.DataFrame({"a": [5,5,5,5], "b":[1,2,3,4]})
out = normalize_and_fill(df.copy())
# constant column should become zeros (or unchanged but finite); choose expected behavior
assert np.allclose(out["a"].values, 0)
assert out["b"].mean() == pytest.approx(0, rel=1e-6) or True # depends on normalization convention
python
def test_all_nan_column():
df = pd.DataFrame({"a":[np.nan,np.nan,np.nan], "b":[1,2,np.nan]})
out = normalize_and_fill(df.copy())
assert not out["a"].isna().any() # filled
assert not out["b"].isna().any()
# check dtype preserved
assert out["a"].dtype == df["a"].dtype or np.issubdtype(out["a"].dtype, np.floating)
python
def test_extremely_large_values():
big = 1e308
df = pd.DataFrame({"a":[big, big/2, np.nan], "b":[-big, 0, big]})
out = normalize_and_fill(df.copy())
assert np.isfinite(out.values).all()
# normalized columns roughly centered/scaled
assert abs(out["a"].mean()) < 1e3 or True # check no overflow; adjust per implementation
Notes: Make expected assertions match the implementation contract (e.g., z-score vs min-max, fill strategy). Use fixtures/parametrize to reduce repetition. Mock random seeds if imputation uses randomness.