Skip to content

Commit

Permalink
Update value_counts
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego committed Aug 26, 2023
1 parent fef9a84 commit 0855d34
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 66 deletions.
8 changes: 4 additions & 4 deletions crates/polars-ops/src/series/ops/various.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ use crate::series::ops::SeriesSealed;
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) -> PolarsResult<DataFrame> {
fn value_counts(&self, sort: bool, parallel: bool) -> PolarsResult<DataFrame> {
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 groups = s.group_tuples(parallel, sort)?;
let values = unsafe { s.agg_first(&groups) };
let counts = groups.group_lengths("counts");
let cols = vec![values, counts.into_series()];
let df = DataFrame::new_no_checks(cols);
if sorted {
let df = DataFrame::new(cols)?;
if sort {
df.sort(["counts"], true, false)
} else {
Ok(df)
Expand Down
6 changes: 3 additions & 3 deletions crates/polars-plan/src/dsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1691,11 +1691,11 @@ impl Expr {

#[cfg(feature = "dtype-struct")]
/// Count all unique values and create a struct mapping value to count
/// Note that it is better to turn multithreaded off in the aggregation context
pub fn value_counts(self, multithreaded: bool, sorted: bool) -> Self {
/// Note that it is better to turn parallel off in the aggregation context
pub fn value_counts(self, sort: bool, parallel: bool) -> Self {
self.apply(
move |s| {
s.value_counts(multithreaded, sorted)
s.value_counts(sort, parallel)
.map(|df| Some(df.into_struct(s.name()).into_series()))
},
GetOutput::map_field(|fld| {
Expand Down
70 changes: 42 additions & 28 deletions py-polars/polars/expr/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -8269,48 +8269,62 @@ def extend_constant(self, value: PythonLiteral | None, n: int) -> Self:

return self._from_pyexpr(self._pyexpr.extend_constant(value, n))

def value_counts(self, *, multithreaded: bool = False, sort: bool = False) -> Self:
@deprecate_renamed_parameter("multithreaded", "parallel", version="0.19.0")
def value_counts(self, *, sort: bool = False, parallel: bool = False) -> Self:
"""
Count all unique values and create a struct mapping value to count.
Count the occurrences of unique values.
Parameters
----------
multithreaded:
Better to turn this off in the aggregation context, as it can lead to
contention.
sort:
Ensure the output is sorted from most values to least.
sort
Sort the output by count in descending order.
If set to ``False`` (default), the order of the output is random.
parallel
Execute the computation in parallel.
.. note::
This option should likely not be enabled in a group by context,
as the computation is already parallelized per group.
Returns
-------
Expr
Expression of data type :class:`Struct`.
Expression of data type :class:`Struct` with mapping of unique values to
their count.
Examples
--------
>>> df = pl.DataFrame(
... {
... "id": ["a", "b", "b", "c", "c", "c"],
... }
... )
>>> df.select(
... [
... pl.col("id").value_counts(sort=True),
... ]
... {"color": ["red", "blue", "red", "green", "blue", "blue"]}
... )
>>> df.select(pl.col("color").value_counts()) # doctest: +IGNORE_RESULT
shape: (3, 1)
┌───────────┐
│ id │
│ --- │
│ struct[2] │
╞═══════════╡
│ {"c",3} │
│ {"b",2} │
│ {"a",1} │
└───────────┘
"""
return self._from_pyexpr(self._pyexpr.value_counts(multithreaded, sort))
┌─────────────┐
│ color │
│ --- │
│ struct[2] │
╞═════════════╡
│ {"red",2} │
│ {"green",1} │
│ {"blue",3} │
└─────────────┘
Sort the output by count.
>>> df.select(pl.col("color").value_counts(sort=True))
shape: (3, 1)
┌─────────────┐
│ color │
│ --- │
│ struct[2] │
╞═════════════╡
│ {"blue",3} │
│ {"red",2} │
│ {"green",1} │
└─────────────┘
"""
return self._from_pyexpr(self._pyexpr.value_counts(sort, parallel))

def unique_counts(self) -> Self:
"""
Expand Down
2 changes: 1 addition & 1 deletion py-polars/polars/expr/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ def eval(self, expr: Expr, *, parallel: bool = False) -> Expr:
Run all expression parallel. Don't activate this blindly.
Parallelism is worth it if there is enough work to do per thread.
This likely should not be use in the group by context, because we already
This likely should not be used in the group by context, because we already
parallel execution per group
Examples
Expand Down
63 changes: 46 additions & 17 deletions py-polars/polars/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2290,32 +2290,61 @@ def hist(
bins = Series(bins, dtype=Float64)._s
return wrap_df(self._s.hist(bins, bin_count))

def value_counts(self, *, sort: bool = False) -> DataFrame:
def value_counts(self, *, sort: bool = False, parallel: bool = False) -> DataFrame:
"""
Count the unique values in a Series.
Count the occurrences of unique values.
Parameters
----------
sort
Ensure the output is sorted from most values to least.
Sort the output by count in descending order.
If set to ``False`` (default), the order of the output is random.
parallel
Execute the computation in parallel.
.. note::
This option should likely not be enabled in a group by context,
as the computation is already parallelized per group.
Returns
-------
DataFrame
Mapping of unique values to their count.
Examples
--------
>>> s = pl.Series("a", [1, 2, 2, 3])
>>> s.value_counts().sort(by="a")
>>> s = pl.Series("color", ["red", "blue", "red", "green", "blue", "blue"])
>>> s.value_counts() # doctest: +IGNORE_RESULT
shape: (3, 2)
┌─────┬────────┐
│ a ┆ counts │
│ --- ┆ --- │
│ i64 ┆ u32 │
╞═════╪════════╡
│ 1 ┆ 1 │
│ 2 ┆ 2 │
│ 3 ┆ 1 │
└─────┴────────┘
"""
return wrap_df(self._s.value_counts(sort))
┌───────┬────────┐
│ color ┆ counts │
│ --- ┆ --- │
│ str ┆ u32 │
╞═══════╪════════╡
│ red ┆ 2 │
│ green ┆ 1 │
│ blue ┆ 3 │
└───────┴────────┘
Sort the output by count.
shape: (3, 2)
┌───────┬────────┐
│ color ┆ counts │
│ --- ┆ --- │
│ str ┆ u32 │
╞═══════╪════════╡
│ blue ┆ 3 │
│ red ┆ 2 │
│ green ┆ 1 │
└───────┴────────┘
"""
return (
self.to_frame()
.select(F.col(self.name).value_counts(sort=sort, parallel=parallel))
.unnest(self.name)
)

def unique_counts(self) -> Series:
"""
Expand Down
7 changes: 2 additions & 5 deletions py-polars/src/expr/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,8 @@ impl PyExpr {
fn count(&self) -> Self {
self.clone().inner.count().into()
}
fn value_counts(&self, multithreaded: bool, sorted: bool) -> Self {
self.inner
.clone()
.value_counts(multithreaded, sorted)
.into()
fn value_counts(&self, sort: bool, parallel: bool) -> Self {
self.inner.clone().value_counts(sort, parallel).into()
}
fn unique_counts(&self) -> Self {
self.inner.clone().unique_counts().into()
Expand Down
8 changes: 0 additions & 8 deletions py-polars/src/series/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,6 @@ impl PySeries {
self.series.sort(descending).into()
}

fn value_counts(&self, sorted: bool) -> PyResult<PyDataFrame> {
let df = self
.series
.value_counts(true, sorted)
.map_err(PyPolarsErr::from)?;
Ok(df.into())
}

fn take_with_series(&self, indices: &PySeries) -> PyResult<Self> {
let idx = indices.series.idx().map_err(PyPolarsErr::from)?;
let take = self.series.take(idx).map_err(PyPolarsErr::from)?;
Expand Down

0 comments on commit 0855d34

Please sign in to comment.