use num_traits::Bounded;
#[cfg(feature = "dtype-struct")]
use polars_core::prelude::sort::arg_sort_multiple::_get_rows_encoded_ca;
use polars_core::prelude::*;
use polars_core::series::IsSorted;
use polars_core::with_match_physical_numeric_polars_type;
use polars_utils::total_ord::TotalOrd;
use crate::series::ops::SeriesSealed;
pub trait SeriesMethods: SeriesSealed {
fn value_counts(&self, sort: bool, parallel: bool, name: String) -> PolarsResult<DataFrame> {
let s = self.as_series();
polars_ensure!(
s.name() != name,
Duplicate: "using `value_counts` on a column/series named '{}' would lead to duplicate column names; change `name` to fix", name,
);
let groups = s.group_tuples(parallel, sort)?;
let values = unsafe { s.agg_first(&groups) };
let counts = groups.group_count().with_name(name.as_str());
let cols = vec![values, counts.into_series()];
let df = unsafe { DataFrame::new_no_checks(cols) };
if sort {
df.sort(
[name],
SortMultipleOptions::default()
.with_order_descending(true)
.with_multithreaded(parallel),
)
} else {
Ok(df)
}
}
#[cfg(feature = "hash")]
fn hash(&self, build_hasher: ahash::RandomState) -> UInt64Chunked {
let s = self.as_series().to_physical_repr();
match s.dtype() {
DataType::List(_) => {
let mut ca = s.list().unwrap().clone();
crate::chunked_array::hash::hash(&mut ca, build_hasher)
},
_ => {
let mut h = vec![];
s.0.vec_hash(build_hasher, &mut h).unwrap();
UInt64Chunked::from_vec(s.name(), h)
},
}
}
fn ensure_sorted_arg(&self, operation: &str) -> PolarsResult<()> {
polars_ensure!(self.is_sorted(Default::default())?, InvalidOperation: "argument in operation '{}' is not sorted, please sort the 'expr/series/column' first", operation);
Ok(())
}
fn is_sorted(&self, options: SortOptions) -> PolarsResult<bool> {
let s = self.as_series();
let null_count = s.null_count();
if (options.descending
&& (options.nulls_last || null_count == 0)
&& matches!(s.is_sorted_flag(), IsSorted::Descending))
|| (!options.descending
&& (!options.nulls_last || null_count == 0)
&& matches!(s.is_sorted_flag(), IsSorted::Ascending))
{
return Ok(true);
}
#[cfg(feature = "dtype-struct")]
if matches!(s.dtype(), DataType::Struct(_)) {
let encoded = _get_rows_encoded_ca(
"",
&[s.clone()],
&[options.descending],
&[options.nulls_last],
)?;
return encoded.into_series().is_sorted(options);
}
let s_len = s.len();
if null_count == s_len {
return Ok(true);
}
if null_count > 0 {
if options.nulls_last {
if s.slice((s_len - null_count) as i64, null_count)
.null_count()
!= null_count
{
return Ok(false);
}
} else if s.slice(0, null_count).null_count() != null_count {
return Ok(false);
}
}
if s.dtype().is_numeric() {
with_match_physical_numeric_polars_type!(s.dtype(), |$T| {
let ca: &ChunkedArray<$T> = s.as_ref().as_ref().as_ref();
return Ok(is_sorted_ca_num::<$T>(ca, options))
})
}
let cmp_len = s_len - null_count - 1; let offset = !options.nulls_last as i64 * null_count as i64;
let (s1, s2) = (s.slice(offset, cmp_len), s.slice(offset + 1, cmp_len));
let cmp_op = if options.descending {
Series::gt_eq
} else {
Series::lt_eq
};
Ok(cmp_op(&s1, &s2)?.all())
}
}
fn check_cmp<T: NumericNative, Cmp: Fn(&T, &T) -> bool>(
vals: &[T],
f: Cmp,
previous: &mut T,
) -> bool {
let mut sorted = true;
for c in vals.chunks(1024) {
for v in c {
sorted &= f(previous, v);
*previous = *v;
}
if !sorted {
return false;
}
}
sorted
}
fn is_sorted_ca_num<T: PolarsNumericType>(ca: &ChunkedArray<T>, options: SortOptions) -> bool {
if let Ok(vals) = ca.cont_slice() {
let mut previous = vals[0];
return if options.descending {
check_cmp(vals, |prev, c| prev.tot_ge(c), &mut previous)
} else {
check_cmp(vals, |prev, c| prev.tot_le(c), &mut previous)
};
};
if ca.null_count() == 0 {
let mut previous = if options.descending {
T::Native::max_value()
} else {
T::Native::min_value()
};
for arr in ca.downcast_iter() {
let vals = arr.values();
let sorted = if options.descending {
check_cmp(vals, |prev, c| prev.tot_ge(c), &mut previous)
} else {
check_cmp(vals, |prev, c| prev.tot_le(c), &mut previous)
};
if !sorted {
return false;
}
}
return true;
};
let null_count = ca.null_count();
if options.nulls_last {
let ca = ca.slice(0, ca.len() - null_count);
is_sorted_ca_num(&ca, options)
} else {
let ca = ca.slice(null_count as i64, ca.len() - null_count);
is_sorted_ca_num(&ca, options)
}
}
impl SeriesMethods for Series {}