Skip to content

Commit

Permalink
Add drop_first parameter to Series.to_dummies
Browse files Browse the repository at this point in the history
  • Loading branch information
mcrumiller committed Mar 4, 2024
1 parent baacf3d commit 6ec7ca5
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 @@ -2109,14 +2109,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 @@ -2132,8 +2136,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 6ec7ca5

Please sign in to comment.