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

refactor(python): Deprecate pl.get_dummies #7055

Merged
merged 1 commit into from
Feb 25, 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
9 changes: 9 additions & 0 deletions py-polars/polars/internals/functions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import contextlib
import warnings
from datetime import date, datetime, timedelta
from typing import TYPE_CHECKING, Iterable, Sequence, overload

Expand Down Expand Up @@ -44,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 @@ -75,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