Skip to content

Commit

Permalink
fix[rust]: maintain logical type in value counts (#4460)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Aug 17, 2022
1 parent b2f70bb commit 15e8036
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
3 changes: 1 addition & 2 deletions polars/polars-ops/src/series/ops/various.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ pub trait SeriesMethods: SeriesSealed {
/// Create a [`DataFrame`] with the unique `values` of this [`Series`] and a column `"counts"`
/// with dtype [`IdxType`]
fn value_counts(&self, multithreaded: bool, sorted: bool) -> Result<DataFrame> {
let s = self.as_series().to_physical_repr();
let s = s.as_ref();
let s = self.as_series();
// we need to sort here as well in case of `maintain_order` because duplicates behavior is undefined
let groups = s.group_tuples(multithreaded, sorted);
let values = unsafe { s.agg_first(&groups) };
Expand Down
10 changes: 10 additions & 0 deletions py-polars/tests/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ def test_value_counts_expr() -> None:
]


def test_value_counts_logical_type() -> None:
# test logical type
df = pl.DataFrame({"a": ["b", "c"]}).with_column(
pl.col("a").cast(pl.Categorical).alias("ac")
)
out = df.select([pl.all().value_counts()])
assert out["ac"].struct.field("ac").dtype == pl.Categorical
assert out["a"].struct.field("a").dtype == pl.Utf8


def test_nested_struct() -> None:
df = pl.DataFrame({"d": [1, 2, 3], "e": ["foo", "bar", "biz"]})
# Nest the datafame
Expand Down

0 comments on commit 15e8036

Please sign in to comment.