Skip to content

Commit

Permalink
chore: flatten subset in unique one level up
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Dec 28, 2024
1 parent 74e8f95 commit 94ac3cf
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 8 deletions.
5 changes: 1 addition & 4 deletions narwhals/_arrow/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ def is_unique(self: Self) -> ArrowSeries:

def unique(
self: Self,
subset: str | list[str] | None,
subset: list[str] | None,
*,
keep: Literal["any", "first", "last", "none"],
maintain_order: bool,
Expand All @@ -653,9 +653,6 @@ def unique(
import pyarrow.compute as pc

df = self._native_frame

if isinstance(subset, str):
subset = [subset]
subset = subset or self.columns

if keep in {"any", "first", "last"}:
Expand Down
3 changes: 1 addition & 2 deletions narwhals/_dask/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,13 @@ def head(self: Self, n: int) -> Self:

def unique(
self: Self,
subset: str | list[str] | None,
subset: list[str] | None,
*,
keep: Literal["any", "first", "last", "none"] = "any",
maintain_order: bool = False,
) -> Self:
# The param `maintain_order` is only here for compatibility with the Polars API
# and has no effect on the output.
subset = flatten(subset) if subset else None
native_frame = self._native_frame
if keep == "none":
subset = subset or self.columns
Expand Down
3 changes: 1 addition & 2 deletions narwhals/_pandas_like/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,15 +684,14 @@ def tail(self, n: int) -> Self:

def unique(
self: Self,
subset: str | list[str] | None,
subset: list[str] | None,
*,
keep: Literal["any", "first", "last", "none"] = "any",
maintain_order: bool = False,
) -> Self:
# The param `maintain_order` is only here for compatibility with the Polars API
# and has no effect on the output.
mapped_keep = {"none": False, "any": "first"}.get(keep, keep)
subset = flatten(subset) if subset else None
return self._from_native_frame(
self._native_frame.drop_duplicates(subset=subset, keep=mapped_keep)
)
Expand Down
2 changes: 2 additions & 0 deletions narwhals/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ def unique(
keep: Literal["any", "first", "last", "none"] = "any",
maintain_order: bool = False,
) -> Self:
if isinstance(subset, str):
subset = [subset]
return self._from_compliant_dataframe(
self._compliant_frame.unique(
subset=subset, keep=keep, maintain_order=maintain_order
Expand Down

0 comments on commit 94ac3cf

Please sign in to comment.