Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(steps): add unit test and fix null cols for impute #157

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ibis_ml/steps/_impute.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import math
from typing import TYPE_CHECKING, Any

import ibis.expr.types as ir
Expand All @@ -14,6 +15,8 @@


def _fillna(col, val):
if val is None or col.type().is_floating() and math.isnan(val):
jitingxu1 marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError(f"Cannot fill column {col.get_name()!r} with `None` or `NaN`")
if col.type().is_floating():
return (col.isnull() | col.isnan()).ifelse(val, col) # noqa: PD003
else:
Expand Down
59 changes: 59 additions & 0 deletions tests/test_impute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import ibis
import numpy as np
import pandas as pd
import pandas.testing as tm
import pytest

import ibis_ml as ml


@pytest.fixture()
def train_table():
return ibis.memtable(
{
"floating_col": [0.0, 0.0, 3.0, None, np.nan],
"int_col": [0, 0, 3, None, None],
"string_col": ["a", "a", "c", None, None],
"null_col": [None] * 5,
}
)


@pytest.mark.parametrize(
("mode", "col_name", "expected"),
[
("mean", "floating_col", 1.0),
("median", "floating_col", 0.0),
("mode", "floating_col", 0.0),
("mean", "int_col", 1),
("median", "int_col", 0),
("mode", "int_col", 0),
("mode", "string_col", "a"),
],
)
def test_impute(train_table, mode, col_name, expected):
mode_class = getattr(ml, f"Impute{mode.capitalize()}")
step = mode_class(col_name)
test_table = ibis.memtable({col_name: [None]})
step.fit_table(train_table, ml.core.Metadata())
result = step.transform_table(test_table)
expected = pd.DataFrame({col_name: [expected]})
tm.assert_frame_equal(result.execute(), expected, check_dtype=False)


def test_fillna(train_table):
step = ml.FillNA("floating_col", 0)
step.fit_table(train_table, ml.core.Metadata())
assert step.is_fitted()
test_table = ibis.memtable({"floating_col": [None]})
result = step.transform_table(test_table)
expected = pd.DataFrame({"floating_col": [0]})
tm.assert_frame_equal(result.execute(), expected, check_dtype=False)

# test _fillna with None
jitingxu1 marked this conversation as resolved.
Show resolved Hide resolved
step = ml.FillNA("floating_col", None)
step.fit_table(train_table, ml.core.Metadata())
with pytest.raises(
ValueError, match="Cannot fill column 'floating_col' with `None` or `NaN`"
):
step.transform_table(test_table)
Loading