Skip to content

Commit

Permalink
chore(rust): move unique_counts to ops (#11963)
Browse files Browse the repository at this point in the history
  • Loading branch information
reswqa authored Oct 24, 2023
1 parent 7c83592 commit 3fbb34b
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 54 deletions.
1 change: 0 additions & 1 deletion crates/polars-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ docs-selection = [
"abs",
"dataframe_arithmetic",
"product",
"unique_counts",
"describe",
"chunked_ids",
"partition_by",
Expand Down
1 change: 0 additions & 1 deletion crates/polars-core/src/series/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ mod downcast;
mod extend;
mod null;
mod to_list;
mod unique;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

Expand Down
49 changes: 0 additions & 49 deletions crates/polars-core/src/series/ops/unique.rs

This file was deleted.

1 change: 1 addition & 0 deletions crates/polars-ops/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ round_series = []
is_first_distinct = []
is_last_distinct = []
is_unique = []
unique_counts = []
approx_unique = []
fused = []
cutqcut = ["dtype-categorical", "dtype-struct"]
Expand Down
4 changes: 4 additions & 0 deletions crates/polars-ops/src/series/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ mod round;
mod search_sorted;
#[cfg(feature = "to_dummies")]
mod to_dummies;
#[cfg(feature = "unique_counts")]
mod unique;
mod various;

pub use approx_algo::*;
Expand Down Expand Up @@ -93,6 +95,8 @@ pub use round::*;
pub use search_sorted::*;
#[cfg(feature = "to_dummies")]
pub use to_dummies::*;
#[cfg(feature = "unique_counts")]
pub use unique::*;
pub use various::*;

pub trait SeriesSealed {
Expand Down
42 changes: 42 additions & 0 deletions crates/polars-ops/src/series/ops/unique.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::hash::Hash;

use polars_core::hashing::_HASHMAP_INIT_SIZE;
use polars_core::prelude::*;
use polars_core::utils::NoNull;

fn unique_counts_helper<I, J>(items: I) -> IdxCa
where
I: Iterator<Item = J>,
J: Hash + Eq,
{
let mut map = PlIndexMap::with_capacity_and_hasher(_HASHMAP_INIT_SIZE, Default::default());
for item in items {
map.entry(item)
.and_modify(|cnt| {
*cnt += 1;
})
.or_insert(1 as IdxSize);
}
let out: NoNull<IdxCa> = map.into_values().collect();
out.into_inner()
}

/// Returns a count of the unique values in the order of appearance.
pub fn unique_counts(s: &Series) -> PolarsResult<Series> {
if s.dtype().to_physical().is_numeric() {
if s.bit_repr_is_large() {
let ca = s.bit_repr_large();
Ok(unique_counts_helper(ca.into_iter()).into_series())
} else {
let ca = s.bit_repr_small();
Ok(unique_counts_helper(ca.into_iter()).into_series())
}
} else {
match s.dtype() {
DataType::Utf8 => Ok(unique_counts_helper(s.utf8().unwrap().into_iter()).into_series()),
dt => {
polars_bail!(opq = unique_counts, dt)
},
}
}
}
2 changes: 1 addition & 1 deletion crates/polars-plan/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ random = ["polars-core/random"]
dynamic_group_by = ["polars-core/dynamic_group_by"]
ewma = ["polars-ops/ewma"]
dot_diagram = []
unique_counts = ["polars-core/unique_counts"]
unique_counts = ["polars-ops/unique_counts"]
log = ["polars-ops/log"]
chunked_ids = ["polars-core/chunked_ids"]
list_to_struct = ["polars-ops/list_to_struct"]
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-plan/src/dsl/function_expr/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub(super) fn value_counts(s: &Series, sort: bool, parallel: bool) -> PolarsResu

#[cfg(feature = "unique_counts")]
pub(super) fn unique_counts(s: &Series) -> PolarsResult<Series> {
Ok(s.unique_counts().into_series())
polars_ops::prelude::unique_counts(s)
}

pub(super) fn backward_fill(s: &Series, limit: FillNullLimit) -> PolarsResult<Series> {
Expand Down
2 changes: 1 addition & 1 deletion crates/polars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ ewma = ["polars-ops/ewma", "polars-lazy?/ewma"]
dot_diagram = ["polars-lazy?/dot_diagram"]
dataframe_arithmetic = ["polars-core/dataframe_arithmetic"]
product = ["polars-core/product"]
unique_counts = ["polars-core/unique_counts", "polars-lazy?/unique_counts"]
unique_counts = ["polars-ops/unique_counts", "polars-lazy?/unique_counts"]
log = ["polars-ops/log", "polars-lazy?/log"]
partition_by = ["polars-core/partition_by"]
semi_anti_join = ["polars-lazy?/semi_anti_join", "polars-ops/semi_anti_join", "polars-sql?/semi_anti_join"]
Expand Down

0 comments on commit 3fbb34b

Please sign in to comment.