Skip to content

Commit

Permalink
fix(python): Add drop_first parameter to Series.to_dummies (pola-…
Browse files Browse the repository at this point in the history
  • Loading branch information
mcrumiller authored Mar 8, 2024
1 parent cf747f9 commit 9cc04a2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
22 changes: 19 additions & 3 deletions py-polars/polars/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2108,14 +2108,18 @@ def quantile(
"""
return self._s.quantile(quantile, interpolation)

def to_dummies(self, separator: str = "_") -> DataFrame:
def to_dummies(
self, *, separator: str = "_", drop_first: bool = False
) -> DataFrame:
"""
Get dummy/indicator variables.
Parameters
----------
separator
Separator/delimiter used when generating column names.
drop_first
Remove the first category from the variable being encoded.
Examples
--------
Expand All @@ -2131,8 +2135,20 @@ def to_dummies(self, separator: str = "_") -> DataFrame:
│ 0 ┆ 1 ┆ 0 │
│ 0 ┆ 0 ┆ 1 │
└─────┴─────┴─────┘
"""
return wrap_df(self._s.to_dummies(separator))
>>> s.to_dummies(drop_first=True)
shape: (3, 2)
┌─────┬─────┐
│ a_2 ┆ a_3 │
│ --- ┆ --- │
│ u8 ┆ u8 │
╞═════╪═════╡
│ 0 ┆ 0 │
│ 1 ┆ 0 │
│ 0 ┆ 1 │
└─────┴─────┘
"""
return wrap_df(self._s.to_dummies(separator, drop_first))

@overload
def cut(
Expand Down
10 changes: 10 additions & 0 deletions py-polars/tests/unit/series/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,16 @@ def test_to_dummies() -> None:
assert_frame_equal(result, expected)


def test_to_dummies_drop_first() -> None:
s = pl.Series("a", [1, 2, 3])
result = s.to_dummies(drop_first=True)
expected = pl.DataFrame(
{"a_2": [0, 1, 0], "a_3": [0, 0, 1]},
schema={"a_2": pl.UInt8, "a_3": pl.UInt8},
)
assert_frame_equal(result, expected)


def test_chunk_lengths() -> None:
s = pl.Series("a", [1, 2, 2, 3])
# this is a Series with one chunk, of length 4
Expand Down

0 comments on commit 9cc04a2

Please sign in to comment.