Skip to content

Commit

Permalink
change error to warning for fill col with None or nan
Browse files Browse the repository at this point in the history
  • Loading branch information
jitingxu1 committed Sep 17, 2024
1 parent 87cf462 commit 5e78804
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
10 changes: 8 additions & 2 deletions ibis_ml/steps/_impute.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@

if TYPE_CHECKING:
from collections.abc import Iterable
import warnings

_DOCS_PAGE_NAME = "imputation"


def _fillna(col, val):
if val is None or col.type().is_floating() and math.isnan(val):
raise ValueError(f"Cannot fill column {col.get_name()!r} with `None` or `NaN`")
if val is None or (col.type().is_numeric() and math.isnan(val)):
warnings.warn(
"Imputation requires at least one non-missing value in "
f"column {col.get_name()!r}",
UserWarning,
stacklevel=2,
)
if col.type().is_floating():
return (col.isnull() | col.isnan()).ifelse(val, col) # noqa: PD003
else:
Expand Down
15 changes: 11 additions & 4 deletions tests/test_impute.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import math

import ibis
import numpy as np
import pandas as pd
Expand Down Expand Up @@ -50,10 +52,15 @@ def test_fillna(train_table):
expected = pd.DataFrame({"floating_col": [0]})
tm.assert_frame_equal(result.execute(), expected, check_dtype=False)

# test _fillna with None
step = ml.FillNA("floating_col", None)

@pytest.mark.parametrize("val", [None, math.nan])
def test_fillna_with_none(train_table, val):
step = ml.FillNA("floating_col", val)
step.fit_table(train_table, ml.core.Metadata())
with pytest.raises(
ValueError, match="Cannot fill column 'floating_col' with `None` or `NaN`"
test_table = ibis.memtable({"floating_col": [1.0, None]})
with pytest.warns(
UserWarning,
match="Imputation requires at least one non-missing value in "
"column 'floating_col'",
):
step.transform_table(test_table)

0 comments on commit 5e78804

Please sign in to comment.