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

[Data] Fix Discretizers transforming ignored cols #31404

Merged
merged 1 commit into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions python/ray/data/preprocessors/discretizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class _AbstractKBinsDiscretizer(Preprocessor):

def _transform_pandas(self, df: pd.DataFrame):
def bin_values(s: pd.Series) -> pd.Series:
if s.name not in self.columns:
return s
labels = self.dtypes.get(s.name) if self.dtypes else False
ordered = True
if labels:
Expand Down
14 changes: 10 additions & 4 deletions python/ray/data/tests/preprocessors/test_discretizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ def test_uniform_kbins_discretizer(
"""Tests basic UniformKBinsDiscretizer functionality."""

col_a = [0.2, 1.4, 2.5, 6.2, 9.7, 2.1]
col_b = [0.2, 1.4, 2.5, 6.2, 9.7, 2.1]
in_df = pd.DataFrame.from_dict({"A": col_a, "B": col_b})
col_b = col_a.copy()
col_c = col_a.copy()
in_df = pd.DataFrame.from_dict({"A": col_a, "B": col_b, "C": col_c})
ds = ray.data.from_pandas(in_df).repartition(2)

discretizer = UniformKBinsDiscretizer(
Expand Down Expand Up @@ -74,6 +75,8 @@ def test_uniform_kbins_discretizer(
include_lowest=include_lowest,
)
)
# Check that the remaining column was not modified
assert out_df["C"].equals(in_df["C"])


@pytest.mark.parametrize(
Expand All @@ -98,8 +101,9 @@ def test_custom_kbins_discretizer(
"""Tests basic CustomKBinsDiscretizer functionality."""

col_a = [0.2, 1.4, 2.5, 6.2, 9.7, 2.1]
col_b = [0.2, 1.4, 2.5, 6.2, 9.7, 2.1]
in_df = pd.DataFrame.from_dict({"A": col_a, "B": col_b})
col_b = col_a.copy()
col_c = col_a.copy()
in_df = pd.DataFrame.from_dict({"A": col_a, "B": col_b, "C": col_c})
ds = ray.data.from_pandas(in_df).repartition(2)

discretizer = CustomKBinsDiscretizer(
Expand Down Expand Up @@ -147,6 +151,8 @@ def test_custom_kbins_discretizer(
include_lowest=include_lowest,
)
)
# Check that the remaining column was not modified
assert out_df["C"].equals(in_df["C"])


if __name__ == "__main__":
Expand Down