Skip to content

Commit

Permalink
refactor(python): Deprecate pl.get_dummies (#7055)
Browse files Browse the repository at this point in the history
  • Loading branch information
zundertj authored Feb 25, 2023
1 parent d039b1a commit 1525209
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
8 changes: 8 additions & 0 deletions py-polars/polars/internals/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def get_dummies(
"""
Convert categorical variables into dummy/indicator variables.
.. deprecated:: 0.16.8
`pl.get_dummies(df)` has been deprecated; use `df.to_dummies()`
Parameters
----------
df
Expand Down Expand Up @@ -76,6 +79,11 @@ def get_dummies(
└───────┴───────┴───────┴───────┴───────┴───────┘
"""
warnings.warn(
"`pl.get_dummies(df)` has been deprecated; use `df.to_dummies()`",
category=DeprecationWarning,
stacklevel=2,
)
return df.to_dummies(columns=columns, separator=separator)


Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/datatypes/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_read_csv_categorical() -> None:
def test_cat_to_dummies() -> None:
df = pl.DataFrame({"foo": [1, 2, 3, 4], "bar": ["a", "b", "a", "c"]})
df = df.with_columns(pl.col("bar").cast(pl.Categorical))
assert pl.get_dummies(df).to_dict(False) == {
assert df.to_dummies().to_dict(False) == {
"foo_1": [1, 0, 0, 0],
"foo_2": [0, 1, 0, 0],
"foo_3": [0, 0, 1, 0],
Expand Down
12 changes: 9 additions & 3 deletions py-polars/tests/unit/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,9 +800,9 @@ def test_arg_where() -> None:
assert_series_equal(pl.arg_where(s, eager=True).cast(int), pl.Series([0, 2]))


def test_get_dummies() -> None:
def test_to_dummies2() -> None:
df = pl.DataFrame({"a": [1, 2, 3]})
res = pl.get_dummies(df)
res = df.to_dummies()
expected = pl.DataFrame(
{"a_1": [1, 0, 0], "a_2": [0, 1, 0], "a_3": [0, 0, 1]}
).with_columns(pl.all().cast(pl.UInt8))
Expand All @@ -820,10 +820,16 @@ def test_get_dummies() -> None:
},
schema={"i": pl.Int32, "category|cat": pl.UInt8, "category|dog": pl.UInt8},
)
result = pl.get_dummies(df, columns=["category"], separator="|")
result = df.to_dummies(columns=["category"], separator="|")
assert_frame_equal(result, expected)


def test_get_dummies_function_deprecated() -> None:
df = pl.DataFrame({"a": [1, 2, 3]})
with pytest.deprecated_call():
pl.get_dummies(df)


def test_to_pandas(df: pl.DataFrame) -> None:
# pyarrow cannot deal with unsigned dictionary integer yet.
# pyarrow cannot convert a time64 w/ non-zero nanoseconds
Expand Down

0 comments on commit 1525209

Please sign in to comment.