From c7b85997aa6e25746104a2f56ced177a37b2f132 Mon Sep 17 00:00:00 2001 From: J van Zundert Date: Mon, 20 Feb 2023 21:11:46 +0000 Subject: [PATCH] refactor(rust, python): Rename kwarg reverse to descending (#6914) --- .../src/kernels/sort_partition.rs | 10 +- polars/polars-core/src/chunked_array/mod.rs | 18 +- .../chunked_array/ops/aggregate/quantile.rs | 12 +- .../polars-core/src/chunked_array/ops/mod.rs | 4 +- .../src/chunked_array/ops/sort/arg_sort.rs | 20 +-- .../ops/sort/arg_sort_multiple.rs | 23 ++- .../src/chunked_array/ops/sort/categorical.rs | 20 +-- .../src/chunked_array/ops/sort/mod.rs | 154 +++++++++--------- .../src/chunked_array/ops/unique/rank.rs | 10 +- .../src/frame/groupby/into_groups.rs | 7 +- .../src/frame/hash_join/sort_merge.rs | 6 +- polars/polars-core/src/frame/mod.rs | 30 ++-- polars/polars-core/src/functions.rs | 10 +- .../src/series/implementations/binary.rs | 12 +- .../src/series/implementations/boolean.rs | 8 +- .../src/series/implementations/categorical.rs | 8 +- .../src/series/implementations/dates_time.rs | 11 +- .../src/series/implementations/datetime.rs | 12 +- .../src/series/implementations/decimal.rs | 4 +- .../src/series/implementations/duration.rs | 12 +- .../src/series/implementations/floats.rs | 11 +- .../src/series/implementations/mod.rs | 11 +- .../src/series/implementations/utf8.rs | 12 +- polars/polars-core/src/series/mod.rs | 4 +- polars/polars-core/src/series/series_trait.rs | 2 +- .../src/executors/sinks/sort/ooc.rs | 10 +- .../src/executors/sinks/sort/sink.rs | 18 +- .../src/executors/sinks/sort/source.rs | 10 +- .../polars-pipe/src/pipeline/convert.rs | 2 +- .../polars-lazy/polars-plan/src/dsl/expr.rs | 2 +- .../polars-plan/src/dsl/function_expr/mod.rs | 6 +- .../polars-plan/src/dsl/functions.rs | 6 +- polars/polars-lazy/polars-plan/src/dsl/mod.rs | 14 +- .../polars-plan/src/logical_plan/aexpr/mod.rs | 2 +- .../polars-plan/src/logical_plan/builder.rs | 4 +- .../src/logical_plan/conversion.rs | 16 +- .../polars-plan/src/logical_plan/format.rs | 8 +- .../logical_plan/optimizer/simplify_expr.rs | 8 +- .../polars-plan/src/logical_plan/options.rs | 2 +- polars/polars-lazy/src/frame/mod.rs | 10 +- .../src/physical_plan/executors/sort.rs | 2 +- .../src/physical_plan/expressions/sortby.rs | 38 +++-- .../src/physical_plan/planner/expr.rs | 8 +- polars/polars-ops/src/chunked_array/top_k.rs | 4 +- .../src/series/ops/search_sorted.rs | 31 ++-- polars/polars-sql/src/context.rs | 8 +- polars/src/docs/eager.rs | 4 +- polars/src/docs/lazy.rs | 4 +- polars/tests/it/core/groupby.rs | 4 +- py-polars/polars/internals/dataframe/frame.py | 11 +- py-polars/polars/internals/expr/expr.py | 69 ++++---- py-polars/polars/internals/expr/list.py | 9 +- py-polars/polars/internals/functions.py | 17 +- py-polars/polars/internals/lazy_functions.py | 22 +-- py-polars/polars/internals/lazyframe/frame.py | 17 +- py-polars/polars/internals/series/list.py | 8 +- py-polars/polars/internals/series/series.py | 74 ++++++--- py-polars/src/lazy/dataframe.rs | 8 +- py-polars/src/lazy/dsl.rs | 24 +-- py-polars/src/lib.rs | 4 +- py-polars/src/series.rs | 18 +- .../tests/benchmark/run_h2oai_benchmark.py | 2 +- py-polars/tests/unit/namespaces/test_list.py | 2 +- py-polars/tests/unit/operations/test_sort.py | 28 ++-- py-polars/tests/unit/test_df.py | 18 +- py-polars/tests/unit/test_errors.py | 2 +- py-polars/tests/unit/test_functions.py | 4 +- py-polars/tests/unit/test_lazy.py | 18 +- py-polars/tests/unit/test_queries.py | 8 +- py-polars/tests/unit/test_series.py | 10 +- py-polars/tests/unit/test_streaming.py | 6 +- 71 files changed, 553 insertions(+), 478 deletions(-) diff --git a/polars/polars-arrow/src/kernels/sort_partition.rs b/polars/polars-arrow/src/kernels/sort_partition.rs index 76ec5d856fd0..2afe105c21f1 100644 --- a/polars/polars-arrow/src/kernels/sort_partition.rs +++ b/polars/polars-arrow/src/kernels/sort_partition.rs @@ -5,13 +5,13 @@ use arrow::types::NativeType; use crate::index::IdxSize; /// Find partition indexes such that every partition contains unique groups. -fn find_partition_points(values: &[T], n: usize, reverse: bool) -> Vec +fn find_partition_points(values: &[T], n: usize, descending: bool) -> Vec where T: Debug + NativeType + PartialOrd, { let len = values.len(); if n > len { - return find_partition_points(values, len / 2, reverse); + return find_partition_points(values, len / 2, descending); } if n < 2 { return vec![]; @@ -31,7 +31,7 @@ where let part = &values[start_idx..end_idx]; let latest_val = values[end_idx]; - let idx = if reverse { + let idx = if descending { part.partition_point(|v| *v > latest_val) } else { part.partition_point(|v| *v < latest_val) @@ -46,11 +46,11 @@ where partition_points } -pub fn create_clean_partitions(values: &[T], n: usize, reverse: bool) -> Vec<&[T]> +pub fn create_clean_partitions(values: &[T], n: usize, descending: bool) -> Vec<&[T]> where T: Debug + NativeType + PartialOrd, { - let part_idx = find_partition_points(values, n, reverse); + let part_idx = find_partition_points(values, n, descending); let mut out = Vec::with_capacity(n + 1); let mut start_idx = 0_usize; diff --git a/polars/polars-core/src/chunked_array/mod.rs b/polars/polars-core/src/chunked_array/mod.rs index 002763da9665..f4ad04f74bb2 100644 --- a/polars/polars-core/src/chunked_array/mod.rs +++ b/polars/polars-core/src/chunked_array/mod.rs @@ -158,11 +158,11 @@ bitflags! { }} impl ChunkedArray { - pub(crate) fn is_sorted_flag(&self) -> bool { + pub(crate) fn is_sorted_ascending_flag(&self) -> bool { self.bit_settings.contains(Settings::SORTED_ASC) } - pub(crate) fn is_sorted_reverse_flag(&self) -> bool { + pub(crate) fn is_sorted_descending_flag(&self) -> bool { self.bit_settings.contains(Settings::SORTED_DSC) } @@ -171,9 +171,9 @@ impl ChunkedArray { } pub fn is_sorted_flag2(&self) -> IsSorted { - if self.is_sorted_flag() { + if self.is_sorted_ascending_flag() { IsSorted::Ascending - } else if self.is_sorted_reverse_flag() { + } else if self.is_sorted_descending_flag() { IsSorted::Descending } else { IsSorted::Not @@ -188,15 +188,15 @@ impl ChunkedArray { .remove(Settings::SORTED_ASC | Settings::SORTED_DSC); } IsSorted::Ascending => { - // // unset reverse sorted + // // unset descending sorted self.bit_settings.remove(Settings::SORTED_DSC); - // set sorted + // set ascending sorted self.bit_settings.insert(Settings::SORTED_ASC) } IsSorted::Descending => { - // unset sorted + // unset ascending sorted self.bit_settings.remove(Settings::SORTED_ASC); - // set reverse sorted + // set descending sorted self.bit_settings.insert(Settings::SORTED_DSC) } } @@ -648,7 +648,7 @@ pub(crate) mod test { let a = a.sort(false); let b = a.into_iter().collect::>(); assert_eq!(b, [Some("a"), Some("b"), Some("c")]); - assert_eq!(a.is_sorted_flag(), true); + assert_eq!(a.is_sorted_ascending_flag(), true); } #[test] diff --git a/polars/polars-core/src/chunked_array/ops/aggregate/quantile.rs b/polars/polars-core/src/chunked_array/ops/aggregate/quantile.rs index 682c99e3578e..c4c1cd211a5f 100644 --- a/polars/polars-core/src/chunked_array/ops/aggregate/quantile.rs +++ b/polars/polars-core/src/chunked_array/ops/aggregate/quantile.rs @@ -192,7 +192,7 @@ where interpol: QuantileInterpolOptions, ) -> PolarsResult> { // in case of sorted data, the sort is free, so don't take quickselect route - if let (Ok(slice), false) = (self.cont_slice(), self.is_sorted_flag()) { + if let (Ok(slice), false) = (self.cont_slice(), self.is_sorted_ascending_flag()) { let mut owned = slice.to_vec(); quantile_slice(&mut owned, quantile, interpol) } else { @@ -217,7 +217,7 @@ where interpol: QuantileInterpolOptions, ) -> PolarsResult> { // in case of sorted data, the sort is free, so don't take quickselect route - let is_sorted = self.is_sorted_flag(); + let is_sorted = self.is_sorted_ascending_flag(); if let (Some(slice), false) = (self.cont_slice_mut(), is_sorted) { quantile_slice(slice, quantile, interpol) } else { @@ -238,7 +238,7 @@ impl ChunkQuantile for Float32Chunked { interpol: QuantileInterpolOptions, ) -> PolarsResult> { // in case of sorted data, the sort is free, so don't take quickselect route - let out = if let (Ok(slice), false) = (self.cont_slice(), self.is_sorted_flag()) { + let out = if let (Ok(slice), false) = (self.cont_slice(), self.is_sorted_ascending_flag()) { let mut owned = slice.to_vec(); let owned = f32_to_ordablef32(&mut owned); quantile_slice(owned, quantile, interpol) @@ -260,7 +260,7 @@ impl ChunkQuantile for Float64Chunked { interpol: QuantileInterpolOptions, ) -> PolarsResult> { // in case of sorted data, the sort is free, so don't take quickselect route - if let (Ok(slice), false) = (self.cont_slice(), self.is_sorted_flag()) { + if let (Ok(slice), false) = (self.cont_slice(), self.is_sorted_ascending_flag()) { let mut owned = slice.to_vec(); let owned = f64_to_ordablef64(&mut owned); quantile_slice(owned, quantile, interpol) @@ -281,7 +281,7 @@ impl Float64Chunked { interpol: QuantileInterpolOptions, ) -> PolarsResult> { // in case of sorted data, the sort is free, so don't take quickselect route - let is_sorted = self.is_sorted_flag(); + let is_sorted = self.is_sorted_ascending_flag(); if let (Some(slice), false) = (self.cont_slice_mut(), is_sorted) { let slice = f64_to_ordablef64(slice); quantile_slice(slice, quantile, interpol) @@ -303,7 +303,7 @@ impl Float32Chunked { interpol: QuantileInterpolOptions, ) -> PolarsResult> { // in case of sorted data, the sort is free, so don't take quickselect route - let is_sorted = self.is_sorted_flag(); + let is_sorted = self.is_sorted_ascending_flag(); if let (Some(slice), false) = (self.cont_slice_mut(), is_sorted) { let slice = f32_to_ordablef32(slice); quantile_slice(slice, quantile, interpol).map(|v| v.map(|v| v as f32)) diff --git a/polars/polars-core/src/chunked_array/ops/mod.rs b/polars/polars-core/src/chunked_array/ops/mod.rs index 269fb80b46ab..8742a10134f2 100644 --- a/polars/polars-core/src/chunked_array/ops/mod.rs +++ b/polars/polars-core/src/chunked_array/ops/mod.rs @@ -493,13 +493,13 @@ pub trait ChunkSort { fn sort_with(&self, options: SortOptions) -> ChunkedArray; /// Returned a sorted `ChunkedArray`. - fn sort(&self, reverse: bool) -> ChunkedArray; + fn sort(&self, descending: bool) -> ChunkedArray; /// Retrieve the indexes needed to sort this array. fn arg_sort(&self, options: SortOptions) -> IdxCa; /// Retrieve the indexes need to sort this and the other arrays. - fn arg_sort_multiple(&self, _other: &[Series], _reverse: &[bool]) -> PolarsResult { + fn arg_sort_multiple(&self, _other: &[Series], _descending: &[bool]) -> PolarsResult { Err(PolarsError::InvalidOperation( "arg_sort_multiple not implemented for this dtype".into(), )) diff --git a/polars/polars-core/src/chunked_array/ops/sort/arg_sort.rs b/polars/polars-core/src/chunked_array/ops/sort/arg_sort.rs index 4ca776e73cef..f55117cc050d 100644 --- a/polars/polars-core/src/chunked_array/ops/sort/arg_sort.rs +++ b/polars/polars-core/src/chunked_array/ops/sort/arg_sort.rs @@ -1,12 +1,12 @@ use super::*; #[inline] -fn default_order(a: &(IdxSize, T), b: &(IdxSize, T)) -> Ordering { +fn ascending_order(a: &(IdxSize, T), b: &(IdxSize, T)) -> Ordering { compare_fn_nan_max(&a.1, &b.1) } #[inline] -fn reverse_order(a: &(IdxSize, T), b: &(IdxSize, T)) -> Ordering { +fn descending_order(a: &(IdxSize, T), b: &(IdxSize, T)) -> Ordering { compare_fn_nan_max(&b.1, &a.1) } @@ -22,14 +22,14 @@ where J: IntoIterator>, T: PartialOrd + Send + Sync + IsFloat, { - let reverse = options.descending; + let descending = options.descending; let nulls_last = options.nulls_last; let mut vals = Vec::with_capacity(len - null_count); - // if we sort reverse, the nulls are last - // and need to be extended to the indices in reverse order - let null_cap = if reverse || nulls_last { + // if we sort descending, the nulls are last + // and need to be extended to the indices in descending order + let null_cap = if descending || nulls_last { null_count // if we sort normally, the nulls are first // and can be extended with the sorted indices @@ -58,14 +58,14 @@ where arg_sort_branch( vals.as_mut_slice(), - reverse, - default_order, - reverse_order, + descending, + ascending_order, + descending_order, options.multithreaded, ); let iter = vals.into_iter().map(|(idx, _v)| idx); - let idx = if reverse || nulls_last { + let idx = if descending || nulls_last { let mut idx = Vec::with_capacity(len); idx.extend(iter); idx.extend(nulls_idx.into_iter().rev()); diff --git a/polars/polars-core/src/chunked_array/ops/sort/arg_sort_multiple.rs b/polars/polars-core/src/chunked_array/ops/sort/arg_sort_multiple.rs index 0b3b01a61a4f..9c8a8ab9ab26 100644 --- a/polars/polars-core/src/chunked_array/ops/sort/arg_sort_multiple.rs +++ b/polars/polars-core/src/chunked_array/ops/sort/arg_sort_multiple.rs @@ -5,47 +5,52 @@ use super::*; pub(crate) fn args_validate( ca: &ChunkedArray, other: &[Series], - reverse: &[bool], + descending: &[bool], ) -> PolarsResult<()> { for s in other { assert_eq!(ca.len(), s.len()); } - if other.len() != (reverse.len() - 1) { + if other.len() != (descending.len() - 1) { return Err(PolarsError::ComputeError( format!( "The amount of ordering booleans: {} does not match that no. of Series: {}", - reverse.len(), + descending.len(), other.len() + 1 ) .into(), )); } - assert_eq!(other.len(), reverse.len() - 1); + assert_eq!(other.len(), descending.len() - 1); Ok(()) } pub(crate) fn arg_sort_multiple_impl( mut vals: Vec<(IdxSize, T)>, other: &[Series], - reverse: &[bool], + descending: &[bool], ) -> PolarsResult { - assert_eq!(reverse.len() - 1, other.len()); + assert_eq!(descending.len() - 1, other.len()); let compare_inner: Vec<_> = other .iter() .map(|s| s.into_partial_ord_inner()) .collect_trusted(); - let first_reverse = reverse[0]; + let first_descending = descending[0]; vals.par_sort_by(|tpl_a, tpl_b| { - match (first_reverse, compare_fn_nan_max(&tpl_a.1, &tpl_b.1)) { + match (first_descending, compare_fn_nan_max(&tpl_a.1, &tpl_b.1)) { // if ordering is equal, we check the other arrays until we find a non-equal ordering // if we have exhausted all arrays, we keep the equal ordering. (_, Ordering::Equal) => { let idx_a = tpl_a.0 as usize; let idx_b = tpl_b.0 as usize; unsafe { - ordering_other_columns(&compare_inner, reverse.get_unchecked(1..), idx_a, idx_b) + ordering_other_columns( + &compare_inner, + descending.get_unchecked(1..), + idx_a, + idx_b, + ) } } (true, Ordering::Less) => Ordering::Greater, diff --git a/polars/polars-core/src/chunked_array/ops/sort/categorical.rs b/polars/polars-core/src/chunked_array/ops/sort/categorical.rs index 19e02918bbfd..457ca30ab1b4 100644 --- a/polars/polars-core/src/chunked_array/ops/sort/categorical.rs +++ b/polars/polars-core/src/chunked_array/ops/sort/categorical.rs @@ -12,12 +12,12 @@ fn sort_with_nulls(a: &Option, b: &Option) -> Ordering { } /// Default sorting nulls -pub fn order_default_null(a: &Option, b: &Option) -> Ordering { +pub fn order_ascending_null(a: &Option, b: &Option) -> Ordering { sort_with_nulls(a, b) } /// Default sorting nulls -pub fn order_reverse_null(a: &Option, b: &Option) -> Ordering { +pub fn order_descending_null(a: &Option, b: &Option) -> Ordering { sort_with_nulls(b, a) } @@ -60,8 +60,8 @@ impl CategoricalChunked { arg_sort_branch( vals.as_mut_slice(), options.descending, - |(_, a), (_, b)| order_default_null(a, b), - |(_, a), (_, b)| order_reverse_null(a, b), + |(_, a), (_, b)| order_ascending_null(a, b), + |(_, a), (_, b)| order_descending_null(a, b), options.multithreaded, ); let cats: NoNull = @@ -94,10 +94,10 @@ impl CategoricalChunked { /// Returned a sorted `ChunkedArray`. #[must_use] - pub fn sort(&self, reverse: bool) -> CategoricalChunked { + pub fn sort(&self, descending: bool) -> CategoricalChunked { self.sort_with(SortOptions { nulls_last: false, - descending: reverse, + descending, multithreaded: true, }) } @@ -123,10 +123,10 @@ impl CategoricalChunked { pub(crate) fn arg_sort_multiple( &self, other: &[Series], - reverse: &[bool], + descending: &[bool], ) -> PolarsResult { if self.use_lexical_sort() { - args_validate(self.logical(), other, reverse)?; + args_validate(self.logical(), other, descending)?; let mut count: IdxSize = 0; let vals: Vec<_> = self .iter_str() @@ -137,9 +137,9 @@ impl CategoricalChunked { }) .collect_trusted(); - arg_sort_multiple_impl(vals, other, reverse) + arg_sort_multiple_impl(vals, other, descending) } else { - self.logical().arg_sort_multiple(other, reverse) + self.logical().arg_sort_multiple(other, descending) } } } diff --git a/polars/polars-core/src/chunked_array/ops/sort/mod.rs b/polars/polars-core/src/chunked_array/ops/sort/mod.rs index 74e2639b3081..a5bd55d113ae 100644 --- a/polars/polars-core/src/chunked_array/ops/sort/mod.rs +++ b/polars/polars-core/src/chunked_array/ops/sort/mod.rs @@ -25,16 +25,16 @@ use crate::series::IsSorted; use crate::utils::{CustomIterTools, NoNull}; /// Reverse sorting when there are no nulls -fn order_reverse(a: &T, b: &T) -> Ordering { +fn order_descending(a: &T, b: &T) -> Ordering { b.cmp(a) } /// Default sorting when there are no nulls -fn order_default(a: &T, b: &T) -> Ordering { +fn order_ascending(a: &T, b: &T) -> Ordering { a.cmp(b) } -fn order_default_flt(a: &T, b: &T) -> Ordering { +fn order_ascending_flt(a: &T, b: &T) -> Ordering { a.partial_cmp(b).unwrap_or_else(|| { match (a.is_nan(), b.is_nan()) { (true, true) => Ordering::Equal, @@ -46,15 +46,15 @@ fn order_default_flt(a: &T, b: &T) -> Ordering { }) } -fn order_reverse_flt(a: &T, b: &T) -> Ordering { - order_default_flt(b, a) +fn order_descending_flt(a: &T, b: &T) -> Ordering { + order_ascending_flt(b, a) } fn sort_branch( slice: &mut [T], - reverse: bool, - default_order_fn: Fd, - reverse_order_fn: Fr, + descending: bool, + ascending_order_fn: Fd, + descending_order_fn: Fr, parallel: bool, ) where T: PartialOrd + Send, @@ -62,27 +62,27 @@ fn sort_branch( Fr: FnMut(&T, &T) -> Ordering + for<'r, 's> Fn(&'r T, &'s T) -> Ordering + Sync, { if parallel { - match reverse { - true => slice.par_sort_unstable_by(reverse_order_fn), - false => slice.par_sort_unstable_by(default_order_fn), + match descending { + true => slice.par_sort_unstable_by(descending_order_fn), + false => slice.par_sort_unstable_by(ascending_order_fn), } } else { - match reverse { - true => slice.sort_unstable_by(reverse_order_fn), - false => slice.sort_unstable_by(default_order_fn), + match descending { + true => slice.sort_unstable_by(descending_order_fn), + false => slice.sort_unstable_by(ascending_order_fn), } } } #[cfg(feature = "private")] -pub fn arg_sort_no_nulls(slice: &mut [(Idx, T)], reverse: bool, parallel: bool) +pub fn arg_sort_no_nulls(slice: &mut [(Idx, T)], descending: bool, parallel: bool) where T: PartialOrd + Send + IsFloat, Idx: PartialOrd + Send, { arg_sort_branch( slice, - reverse, + descending, |(_, a), (_, b)| compare_fn_nan_max(a, b), |(_, a), (_, b)| compare_fn_nan_max(b, a), parallel, @@ -91,9 +91,9 @@ where pub fn arg_sort_branch( slice: &mut [T], - reverse: bool, - default_order_fn: Fd, - reverse_order_fn: Fr, + descending: bool, + ascending_order_fn: Fd, + descending_order_fn: Fr, parallel: bool, ) where T: PartialOrd + Send, @@ -101,14 +101,14 @@ pub fn arg_sort_branch( Fr: FnMut(&T, &T) -> Ordering + for<'r, 's> Fn(&'r T, &'s T) -> Ordering + Sync, { if parallel { - match reverse { - true => slice.par_sort_by(reverse_order_fn), - false => slice.par_sort_by(default_order_fn), + match descending { + true => slice.par_sort_by(descending_order_fn), + false => slice.par_sort_by(ascending_order_fn), } } else { - match reverse { - true => slice.sort_by(reverse_order_fn), - false => slice.sort_by(default_order_fn), + match descending { + true => slice.sort_by(descending_order_fn), + false => slice.sort_by(ascending_order_fn), } } } @@ -134,7 +134,7 @@ macro_rules! sort_with_fast_path { } // we can clone if we sort in same order - if $options.descending && $ca.is_sorted_reverse_flag() || ($ca.is_sorted_flag() && !$options.descending) { + if $options.descending && $ca.is_sorted_descending_flag() || ($ca.is_sorted_ascending_flag() && !$options.descending) { // there are nulls if $ca.null_count() > 0 { // if the nulls are already last we can clone @@ -152,7 +152,7 @@ macro_rules! sort_with_fast_path { } } // we can reverse if we sort in other order - else if ($options.descending && $ca.is_sorted_flag() || $ca.is_sorted_reverse_flag()) && $ca.null_count() == 0 { + else if ($options.descending && $ca.is_sorted_ascending_flag() || $ca.is_sorted_descending_flag()) && $ca.null_count() == 0 { return $ca.reverse() }; @@ -163,8 +163,8 @@ macro_rules! sort_with_fast_path { fn sort_with_numeric( ca: &ChunkedArray, options: SortOptions, - order_default: fn(&T::Native, &T::Native) -> Ordering, - order_reverse: fn(&T::Native, &T::Native) -> Ordering, + order_ascending: fn(&T::Native, &T::Native) -> Ordering, + order_descending: fn(&T::Native, &T::Native) -> Ordering, ) -> ChunkedArray where T: PolarsNumericType, @@ -176,8 +176,8 @@ where sort_branch( vals.as_mut_slice(), options.descending, - order_default, - order_reverse, + order_ascending, + order_descending, options.multithreaded, ); @@ -213,8 +213,8 @@ where sort_branch( mut_slice, options.descending, - order_default, - order_reverse, + order_ascending, + order_descending, options.multithreaded, ); @@ -263,7 +263,7 @@ fn arg_sort_numeric(ca: &ChunkedArray, options: SortOptions) -> IdxCa where T: PolarsNumericType, { - let reverse = options.descending; + let descending = options.descending; if ca.null_count() == 0 { let mut vals = Vec::with_capacity(ca.len()); let mut count: IdxSize = 0; @@ -277,7 +277,7 @@ where vals.extend_trusted_len(iter); }); - arg_sort_no_nulls(vals.as_mut_slice(), reverse, options.multithreaded); + arg_sort_no_nulls(vals.as_mut_slice(), descending, options.multithreaded); let out: NoNull = vals.into_iter().map(|(idx, _v)| idx).collect_trusted(); let mut out = out.into_inner(); @@ -295,9 +295,9 @@ where fn arg_sort_multiple_numeric( ca: &ChunkedArray, other: &[Series], - reverse: &[bool], + descending: &[bool], ) -> PolarsResult { - args_validate(ca, other, reverse)?; + args_validate(ca, other, descending)?; let mut count: IdxSize = 0; let vals: Vec<_> = ca .into_iter() @@ -308,7 +308,7 @@ fn arg_sort_multiple_numeric( }) .collect_trusted(); - arg_sort_multiple_impl(vals, other, reverse) + arg_sort_multiple_impl(vals, other, descending) } impl ChunkSort for ChunkedArray @@ -317,12 +317,12 @@ where T::Native: Default + Ord, { fn sort_with(&self, options: SortOptions) -> ChunkedArray { - sort_with_numeric(self, options, order_default, order_reverse) + sort_with_numeric(self, options, order_ascending, order_descending) } - fn sort(&self, reverse: bool) -> ChunkedArray { + fn sort(&self, descending: bool) -> ChunkedArray { self.sort_with(SortOptions { - descending: reverse, + descending, ..Default::default() }) } @@ -336,19 +336,19 @@ where /// /// This function is very opinionated. /// We assume that all numeric `Series` are of the same type, if not it will panic - fn arg_sort_multiple(&self, other: &[Series], reverse: &[bool]) -> PolarsResult { - arg_sort_multiple_numeric(self, other, reverse) + fn arg_sort_multiple(&self, other: &[Series], descending: &[bool]) -> PolarsResult { + arg_sort_multiple_numeric(self, other, descending) } } impl ChunkSort for Float32Chunked { fn sort_with(&self, options: SortOptions) -> Float32Chunked { - sort_with_numeric(self, options, order_default_flt, order_reverse_flt) + sort_with_numeric(self, options, order_ascending_flt, order_descending_flt) } - fn sort(&self, reverse: bool) -> Float32Chunked { + fn sort(&self, descending: bool) -> Float32Chunked { self.sort_with(SortOptions { - descending: reverse, + descending, ..Default::default() }) } @@ -362,19 +362,19 @@ impl ChunkSort for Float32Chunked { /// /// This function is very opinionated. /// We assume that all numeric `Series` are of the same type, if not it will panic - fn arg_sort_multiple(&self, other: &[Series], reverse: &[bool]) -> PolarsResult { - arg_sort_multiple_numeric(self, other, reverse) + fn arg_sort_multiple(&self, other: &[Series], descending: &[bool]) -> PolarsResult { + arg_sort_multiple_numeric(self, other, descending) } } impl ChunkSort for Float64Chunked { fn sort_with(&self, options: SortOptions) -> Float64Chunked { - sort_with_numeric(self, options, order_default_flt, order_reverse_flt) + sort_with_numeric(self, options, order_ascending_flt, order_descending_flt) } - fn sort(&self, reverse: bool) -> Float64Chunked { + fn sort(&self, descending: bool) -> Float64Chunked { self.sort_with(SortOptions { - descending: reverse, + descending, ..Default::default() }) } @@ -388,22 +388,22 @@ impl ChunkSort for Float64Chunked { /// /// This function is very opinionated. /// We assume that all numeric `Series` are of the same type, if not it will panic - fn arg_sort_multiple(&self, other: &[Series], reverse: &[bool]) -> PolarsResult { - arg_sort_multiple_numeric(self, other, reverse) + fn arg_sort_multiple(&self, other: &[Series], descending: &[bool]) -> PolarsResult { + arg_sort_multiple_numeric(self, other, descending) } } fn ordering_other_columns<'a>( compare_inner: &'a [Box], - reverse: &[bool], + descending: &[bool], idx_a: usize, idx_b: usize, ) -> Ordering { - for (cmp, reverse) in compare_inner.iter().zip(reverse) { + for (cmp, descending) in compare_inner.iter().zip(descending) { // Safety: // indices are in bounds let ordering = unsafe { cmp.cmp_element_unchecked(idx_a, idx_b) }; - match (ordering, reverse) { + match (ordering, descending) { (Ordering::Equal, _) => continue, (_, true) => return ordering.reverse(), _ => return ordering, @@ -425,8 +425,8 @@ impl ChunkSort for Utf8Chunked { sort_branch( v.as_mut_slice(), options.descending, - order_default, - order_reverse, + order_ascending, + order_descending, options.multithreaded, ); @@ -507,9 +507,9 @@ impl ChunkSort for Utf8Chunked { ca } - fn sort(&self, reverse: bool) -> Utf8Chunked { + fn sort(&self, descending: bool) -> Utf8Chunked { self.sort_with(SortOptions { - descending: reverse, + descending, nulls_last: false, multithreaded: true, }) @@ -534,8 +534,8 @@ impl ChunkSort for Utf8Chunked { /// In this case we assume that all numeric `Series` are `f64` types. The caller needs to /// uphold this contract. If not, it will panic. /// - fn arg_sort_multiple(&self, other: &[Series], reverse: &[bool]) -> PolarsResult { - args_validate(self, other, reverse)?; + fn arg_sort_multiple(&self, other: &[Series], descending: &[bool]) -> PolarsResult { + args_validate(self, other, descending)?; let mut count: IdxSize = 0; let vals: Vec<_> = self @@ -546,7 +546,7 @@ impl ChunkSort for Utf8Chunked { (i, v) }) .collect_trusted(); - arg_sort_multiple_impl(vals, other, reverse) + arg_sort_multiple_impl(vals, other, descending) } } @@ -563,8 +563,8 @@ impl ChunkSort for BinaryChunked { sort_branch( v.as_mut_slice(), options.descending, - order_default, - order_reverse, + order_ascending, + order_descending, options.multithreaded, ); @@ -645,9 +645,9 @@ impl ChunkSort for BinaryChunked { ca } - fn sort(&self, reverse: bool) -> BinaryChunked { + fn sort(&self, descending: bool) -> BinaryChunked { self.sort_with(SortOptions { - descending: reverse, + descending, nulls_last: false, multithreaded: true, }) @@ -672,8 +672,8 @@ impl ChunkSort for BinaryChunked { /// In this case we assume that all numeric `Series` are `f64` types. The caller needs to /// uphold this contract. If not, it will panic. /// - fn arg_sort_multiple(&self, other: &[Series], reverse: &[bool]) -> PolarsResult { - args_validate(self, other, reverse)?; + fn arg_sort_multiple(&self, other: &[Series], descending: &[bool]) -> PolarsResult { + args_validate(self, other, descending)?; let mut count: IdxSize = 0; let vals: Vec<_> = self @@ -684,7 +684,7 @@ impl ChunkSort for BinaryChunked { (i, v) }) .collect_trusted(); - arg_sort_multiple_impl(vals, other, reverse) + arg_sort_multiple_impl(vals, other, descending) } } @@ -708,9 +708,9 @@ impl ChunkSort for BooleanChunked { ca } - fn sort(&self, reverse: bool) -> BooleanChunked { + fn sort(&self, descending: bool) -> BooleanChunked { self.sort_with(SortOptions { - descending: reverse, + descending, nulls_last: false, multithreaded: true, }) @@ -730,7 +730,7 @@ impl ChunkSort for BooleanChunked { #[cfg(feature = "sort_multiple")] pub(crate) fn prepare_arg_sort( columns: Vec, - mut reverse: Vec, + mut descending: Vec, ) -> PolarsResult<(Series, Vec, Vec)> { let n_cols = columns.len(); @@ -758,12 +758,12 @@ pub(crate) fn prepare_arg_sort( let first = columns.remove(0); // broadcast ordering - if n_cols > reverse.len() && reverse.len() == 1 { - while n_cols != reverse.len() { - reverse.push(reverse[0]); + if n_cols > descending.len() && descending.len() == 1 { + while n_cols != descending.len() { + descending.push(descending[0]); } } - Ok((first, columns, reverse)) + Ok((first, columns, descending)) } #[cfg(test)] diff --git a/polars/polars-core/src/chunked_array/ops/unique/rank.rs b/polars/polars-core/src/chunked_array/ops/unique/rank.rs index 8d48440562cf..5ad4485d8d9e 100644 --- a/polars/polars-core/src/chunked_array/ops/unique/rank.rs +++ b/polars/polars-core/src/chunked_array/ops/unique/rank.rs @@ -33,7 +33,7 @@ impl Default for RankOptions { } } -pub(crate) fn rank(s: &Series, method: RankMethod, reverse: bool) -> Series { +pub(crate) fn rank(s: &Series, method: RankMethod, descending: bool) -> Series { match s.len() { 1 => { return match method { @@ -54,18 +54,18 @@ pub(crate) fn rank(s: &Series, method: RankMethod, reverse: bool) -> Series { let nulls = s.is_not_null().rechunk(); let arr = nulls.downcast_iter().next().unwrap(); let validity = arr.values(); - // Currently, nulls tie with the minimum or maximum bound for a type, depending on reverse. + // Currently, nulls tie with the minimum or maximum bound for a type, depending on descending. // TODO: Need to expose nulls_last in arg_sort to prevent this. // Fill using MaxBound/MinBound to give nulls last rank. // we will replace them later. - let null_strategy = if reverse { + let null_strategy = if descending { FillNullStrategy::MinBound } else { FillNullStrategy::MaxBound }; let s = s.fill_null(null_strategy).unwrap(); - let mut out = rank(&s, method, reverse); + let mut out = rank(&s, method, descending); unsafe { let arr = &mut out.chunks_mut()[0]; *arr = arr.with_validity(Some(validity.clone())) @@ -78,7 +78,7 @@ pub(crate) fn rank(s: &Series, method: RankMethod, reverse: bool) -> Series { let len = s.len(); let null_count = s.null_count(); let sort_idx_ca = s.arg_sort(SortOptions { - descending: reverse, + descending, ..Default::default() }); let sort_idx = sort_idx_ca.downcast_iter().next().unwrap().values(); diff --git a/polars/polars-core/src/frame/groupby/into_groups.rs b/polars/polars-core/src/frame/groupby/into_groups.rs index 05e4ba61737d..3cb893e2a8fc 100644 --- a/polars/polars-core/src/frame/groupby/into_groups.rs +++ b/polars/polars-core/src/frame/groupby/into_groups.rs @@ -104,7 +104,8 @@ where let n_threads = POOL.current_num_threads(); let groups = if multithreaded && n_threads > 1 { - let parts = create_clean_partitions(values, n_threads, self.is_sorted_reverse_flag()); + let parts = + create_clean_partitions(values, n_threads, self.is_sorted_descending_flag()); let n_parts = parts.len(); let first_ptr = &values[0] as *const T::Native as usize; @@ -152,7 +153,9 @@ where { fn group_tuples(&self, multithreaded: bool, sorted: bool) -> PolarsResult { // sorted path - if self.is_sorted_flag() || self.is_sorted_reverse_flag() && self.chunks().len() == 1 { + if self.is_sorted_ascending_flag() + || self.is_sorted_descending_flag() && self.chunks().len() == 1 + { // don't have to pass `sorted` arg, GroupSlice is always sorted. return Ok(GroupsProxy::Slice { groups: self.create_groups_from_sorted(multithreaded), diff --git a/polars/polars-core/src/frame/hash_join/sort_merge.rs b/polars/polars-core/src/frame/hash_join/sort_merge.rs index 3f0cd2794435..87ccd5046c52 100644 --- a/polars/polars-core/src/frame/hash_join/sort_merge.rs +++ b/polars/polars-core/src/frame/hash_join/sort_merge.rs @@ -207,7 +207,7 @@ pub fn _sort_or_hash_inner( } (IsSorted::Ascending, _) if is_numeric && size_factor_rhs < size_factor_acceptable => { if verbose { - eprintln!("right key will be reverse sorted in inner join operation.") + eprintln!("right key will be descending sorted in inner join operation.") } let sort_idx = s_right.arg_sort(SortOptions { @@ -231,7 +231,7 @@ pub fn _sort_or_hash_inner( } (_, IsSorted::Ascending) if is_numeric && size_factor_lhs < size_factor_acceptable => { if verbose { - eprintln!("left key will be reverse sorted in inner join operation.") + eprintln!("left key will be descending sorted in inner join operation.") } let sort_idx = s_left.arg_sort(SortOptions { @@ -251,7 +251,7 @@ pub fn _sort_or_hash_inner( }); }); - // set sorted to `false` as we reverse sorted the left key. + // set sorted to `false` as we descending sorted the left key. ((left, right), false) } _ => s_left.hash_join_inner(s_right), diff --git a/polars/polars-core/src/frame/mod.rs b/polars/polars-core/src/frame/mod.rs index 13f30584d493..4bfa8f90aaf4 100644 --- a/polars/polars-core/src/frame/mod.rs +++ b/polars/polars-core/src/frame/mod.rs @@ -1782,12 +1782,12 @@ impl DataFrame { pub fn sort_in_place( &mut self, by_column: impl IntoVec, - reverse: impl IntoVec, + descending: impl IntoVec, ) -> PolarsResult<&mut Self> { let by_column = self.select_series(by_column)?; - let reverse = reverse.into_vec(); + let descending = descending.into_vec(); self.columns = self - .sort_impl(by_column, reverse, false, None, true)? + .sort_impl(by_column, descending, false, None, true)? .columns; Ok(self) } @@ -1797,7 +1797,7 @@ impl DataFrame { pub fn sort_impl( &self, by_column: Vec, - reverse: Vec, + descending: Vec, nulls_last: bool, slice: Option<(i64, usize)>, parallel: bool, @@ -1813,13 +1813,13 @@ impl DataFrame { // therefore when we try to set the first columns as sorted, we ignore the error // as expressions are not present (they are renamed to _POLARS_SORT_COLUMN_i. - let first_reverse = reverse[0]; + let first_descending = descending[0]; let first_by_column = by_column[0].name().to_string(); let mut take = match by_column.len() { 1 => { let s = &by_column[0]; let options = SortOptions { - descending: reverse[0], + descending: descending[0], nulls_last, multithreaded: parallel, }; @@ -1839,8 +1839,8 @@ impl DataFrame { _ => { #[cfg(feature = "sort_multiple")] { - let (first, by_column, reverse) = prepare_arg_sort(by_column, reverse)?; - first.arg_sort_multiple(&by_column, &reverse)? + let (first, by_column, descending) = prepare_arg_sort(by_column, descending)?; + first.arg_sort_multiple(&by_column, &descending)? } #[cfg(not(feature = "sort_multiple"))] { @@ -1861,7 +1861,7 @@ impl DataFrame { // not present in the dataframe let _ = df.apply(&first_by_column, |s| { let mut s = s.clone(); - if first_reverse { + if first_descending { s.set_sorted_flag(IsSorted::Descending) } else { s.set_sorted_flag(IsSorted::Ascending) @@ -1877,8 +1877,8 @@ impl DataFrame { /// /// ``` /// # use polars_core::prelude::*; - /// fn sort_example(df: &DataFrame, reverse: bool) -> PolarsResult { - /// df.sort(["a"], reverse) + /// fn sort_example(df: &DataFrame, descending: bool) -> PolarsResult { + /// df.sort(["a"], descending) /// } /// /// fn sort_by_multiple_columns_example(df: &DataFrame) -> PolarsResult { @@ -1888,10 +1888,10 @@ impl DataFrame { pub fn sort( &self, by_column: impl IntoVec, - reverse: impl IntoVec, + descending: impl IntoVec, ) -> PolarsResult { let mut df = self.clone(); - df.sort_in_place(by_column, reverse)?; + df.sort_in_place(by_column, descending)?; Ok(df) } @@ -1899,11 +1899,11 @@ impl DataFrame { pub fn sort_with_options(&self, by_column: &str, options: SortOptions) -> PolarsResult { let mut df = self.clone(); let by_column = vec![df.column(by_column)?.clone()]; - let reverse = vec![options.descending]; + let descending = vec![options.descending]; df.columns = df .sort_impl( by_column, - reverse, + descending, options.nulls_last, None, options.multithreaded, diff --git a/polars/polars-core/src/functions.rs b/polars/polars-core/src/functions.rs index dcc97e9280ca..e9cb1bd94f6d 100644 --- a/polars/polars-core/src/functions.rs +++ b/polars/polars-core/src/functions.rs @@ -99,19 +99,19 @@ where /// That means that the first `Series` will be used to determine the ordering /// until duplicates are found. Once duplicates are found, the next `Series` will /// be used and so on. -pub fn arg_sort_by(by: &[Series], reverse: &[bool]) -> PolarsResult { - if by.len() != reverse.len() { +pub fn arg_sort_by(by: &[Series], descending: &[bool]) -> PolarsResult { + if by.len() != descending.len() { return Err(PolarsError::ComputeError( format!( "The amount of ordering booleans: {} does not match amount of Series: {}", - reverse.len(), + descending.len(), by.len() ) .into(), )); } - let (first, by, reverse) = prepare_arg_sort(by.to_vec(), reverse.to_vec()).unwrap(); - first.arg_sort_multiple(&by, &reverse) + let (first, by, descending) = prepare_arg_sort(by.to_vec(), descending.to_vec()).unwrap(); + first.arg_sort_multiple(&by, &descending) } // utility to be able to also add literals to concat_str function diff --git a/polars/polars-core/src/series/implementations/binary.rs b/polars/polars-core/src/series/implementations/binary.rs index f96f2f0f6fa8..6dfcbdc93075 100644 --- a/polars/polars-core/src/series/implementations/binary.rs +++ b/polars/polars-core/src/series/implementations/binary.rs @@ -89,16 +89,16 @@ impl private::PrivateSeries for SeriesWrap { } #[cfg(feature = "sort_multiple")] - fn arg_sort_multiple(&self, by: &[Series], reverse: &[bool]) -> PolarsResult { - self.0.arg_sort_multiple(by, reverse) + fn arg_sort_multiple(&self, by: &[Series], descending: &[bool]) -> PolarsResult { + self.0.arg_sort_multiple(by, descending) } } impl SeriesTrait for SeriesWrap { fn is_sorted_flag(&self) -> IsSorted { - if self.0.is_sorted_flag() { + if self.0.is_sorted_ascending_flag() { IsSorted::Ascending - } else if self.0.is_sorted_reverse_flag() { + } else if self.0.is_sorted_descending_flag() { IsSorted::Descending } else { IsSorted::Not @@ -194,7 +194,9 @@ impl SeriesTrait for SeriesWrap { let mut out = ChunkTake::take_unchecked(&self.0, (&*idx).into()); - if self.0.is_sorted_flag() && (idx.is_sorted_flag() || idx.is_sorted_reverse_flag()) { + if self.0.is_sorted_ascending_flag() + && (idx.is_sorted_ascending_flag() || idx.is_sorted_descending_flag()) + { out.set_sorted_flag(idx.is_sorted_flag2()) } diff --git a/polars/polars-core/src/series/implementations/boolean.rs b/polars/polars-core/src/series/implementations/boolean.rs index 85a9da3578e2..920a5a492af1 100644 --- a/polars/polars-core/src/series/implementations/boolean.rs +++ b/polars/polars-core/src/series/implementations/boolean.rs @@ -100,16 +100,16 @@ impl private::PrivateSeries for SeriesWrap { } #[cfg(feature = "sort_multiple")] - fn arg_sort_multiple(&self, by: &[Series], reverse: &[bool]) -> PolarsResult { - self.0.arg_sort_multiple(by, reverse) + fn arg_sort_multiple(&self, by: &[Series], descending: &[bool]) -> PolarsResult { + self.0.arg_sort_multiple(by, descending) } } impl SeriesTrait for SeriesWrap { fn is_sorted_flag(&self) -> IsSorted { - if self.0.is_sorted_flag() { + if self.0.is_sorted_ascending_flag() { IsSorted::Ascending - } else if self.0.is_sorted_reverse_flag() { + } else if self.0.is_sorted_descending_flag() { IsSorted::Descending } else { IsSorted::Not diff --git a/polars/polars-core/src/series/implementations/categorical.rs b/polars/polars-core/src/series/implementations/categorical.rs index e11743c6d78f..a11343ddb6ee 100644 --- a/polars/polars-core/src/series/implementations/categorical.rs +++ b/polars/polars-core/src/series/implementations/categorical.rs @@ -140,16 +140,16 @@ impl private::PrivateSeries for SeriesWrap { } #[cfg(feature = "sort_multiple")] - fn arg_sort_multiple(&self, by: &[Series], reverse: &[bool]) -> PolarsResult { - self.0.arg_sort_multiple(by, reverse) + fn arg_sort_multiple(&self, by: &[Series], descending: &[bool]) -> PolarsResult { + self.0.arg_sort_multiple(by, descending) } } impl SeriesTrait for SeriesWrap { fn is_sorted_flag(&self) -> IsSorted { - if self.0.logical().is_sorted_flag() { + if self.0.logical().is_sorted_ascending_flag() { IsSorted::Ascending - } else if self.0.logical().is_sorted_reverse_flag() { + } else if self.0.logical().is_sorted_descending_flag() { IsSorted::Descending } else { IsSorted::Not diff --git a/polars/polars-core/src/series/implementations/dates_time.rs b/polars/polars-core/src/series/implementations/dates_time.rs index c5e10fd2dbb9..a161e0d25a5f 100644 --- a/polars/polars-core/src/series/implementations/dates_time.rs +++ b/polars/polars-core/src/series/implementations/dates_time.rs @@ -173,16 +173,16 @@ macro_rules! impl_dyn_series { self.0.group_tuples(multithreaded, sorted) } #[cfg(feature = "sort_multiple")] - fn arg_sort_multiple(&self, by: &[Series], reverse: &[bool]) -> PolarsResult { - self.0.deref().arg_sort_multiple(by, reverse) + fn arg_sort_multiple(&self, by: &[Series], descending: &[bool]) -> PolarsResult { + self.0.deref().arg_sort_multiple(by, descending) } } impl SeriesTrait for SeriesWrap<$ca> { fn is_sorted_flag(&self) -> IsSorted { - if self.0.is_sorted_flag() { + if self.0.is_sorted_ascending_flag() { IsSorted::Ascending - } else if self.0.is_sorted_reverse_flag() { + } else if self.0.is_sorted_descending_flag() { IsSorted::Descending } else { IsSorted::Not @@ -292,7 +292,8 @@ macro_rules! impl_dyn_series { unsafe fn take_unchecked(&self, idx: &IdxCa) -> PolarsResult { let mut out = ChunkTake::take_unchecked(self.0.deref(), idx.into()); - if self.0.is_sorted_flag() && (idx.is_sorted_flag() || idx.is_sorted_reverse_flag()) + if self.0.is_sorted_ascending_flag() + && (idx.is_sorted_ascending_flag() || idx.is_sorted_descending_flag()) { out.set_sorted_flag(idx.is_sorted_flag2()) } diff --git a/polars/polars-core/src/series/implementations/datetime.rs b/polars/polars-core/src/series/implementations/datetime.rs index d5984b29d819..2a35fd4723b3 100644 --- a/polars/polars-core/src/series/implementations/datetime.rs +++ b/polars/polars-core/src/series/implementations/datetime.rs @@ -173,16 +173,16 @@ impl private::PrivateSeries for SeriesWrap { self.0.group_tuples(multithreaded, sorted) } #[cfg(feature = "sort_multiple")] - fn arg_sort_multiple(&self, by: &[Series], reverse: &[bool]) -> PolarsResult { - self.0.deref().arg_sort_multiple(by, reverse) + fn arg_sort_multiple(&self, by: &[Series], descending: &[bool]) -> PolarsResult { + self.0.deref().arg_sort_multiple(by, descending) } } impl SeriesTrait for SeriesWrap { fn is_sorted_flag(&self) -> IsSorted { - if self.0.is_sorted_flag() { + if self.0.is_sorted_ascending_flag() { IsSorted::Ascending - } else if self.0.is_sorted_reverse_flag() { + } else if self.0.is_sorted_descending_flag() { IsSorted::Descending } else { IsSorted::Not @@ -298,7 +298,9 @@ impl SeriesTrait for SeriesWrap { unsafe fn take_unchecked(&self, idx: &IdxCa) -> PolarsResult { let mut out = ChunkTake::take_unchecked(self.0.deref(), idx.into()); - if self.0.is_sorted_flag() && (idx.is_sorted_flag() || idx.is_sorted_reverse_flag()) { + if self.0.is_sorted_ascending_flag() + && (idx.is_sorted_ascending_flag() || idx.is_sorted_descending_flag()) + { out.set_sorted_flag(idx.is_sorted_flag2()) } diff --git a/polars/polars-core/src/series/implementations/decimal.rs b/polars/polars-core/src/series/implementations/decimal.rs index c52f9a177233..77cb47a04324 100644 --- a/polars/polars-core/src/series/implementations/decimal.rs +++ b/polars/polars-core/src/series/implementations/decimal.rs @@ -102,7 +102,9 @@ impl SeriesTrait for SeriesWrap { unsafe fn take_unchecked(&self, idx: &IdxCa) -> PolarsResult { let mut out = ChunkTake::take_unchecked(self.0.deref(), idx.into()); - if self.0.is_sorted_flag() && (idx.is_sorted_flag() || idx.is_sorted_reverse_flag()) { + if self.0.is_sorted_ascending_flag() + && (idx.is_sorted_ascending_flag() || idx.is_sorted_descending_flag()) + { out.set_sorted_flag(idx.is_sorted_flag2()) } diff --git a/polars/polars-core/src/series/implementations/duration.rs b/polars/polars-core/src/series/implementations/duration.rs index b82631e8e7fd..05b03cf4ace3 100644 --- a/polars/polars-core/src/series/implementations/duration.rs +++ b/polars/polars-core/src/series/implementations/duration.rs @@ -199,16 +199,16 @@ impl private::PrivateSeries for SeriesWrap { self.0.group_tuples(multithreaded, sorted) } #[cfg(feature = "sort_multiple")] - fn arg_sort_multiple(&self, by: &[Series], reverse: &[bool]) -> PolarsResult { - self.0.deref().arg_sort_multiple(by, reverse) + fn arg_sort_multiple(&self, by: &[Series], descending: &[bool]) -> PolarsResult { + self.0.deref().arg_sort_multiple(by, descending) } } impl SeriesTrait for SeriesWrap { fn is_sorted_flag(&self) -> IsSorted { - if self.0.is_sorted_flag() { + if self.0.is_sorted_ascending_flag() { IsSorted::Ascending - } else if self.0.is_sorted_reverse_flag() { + } else if self.0.is_sorted_descending_flag() { IsSorted::Descending } else { IsSorted::Not @@ -317,7 +317,9 @@ impl SeriesTrait for SeriesWrap { unsafe fn take_unchecked(&self, idx: &IdxCa) -> PolarsResult { let mut out = ChunkTake::take_unchecked(self.0.deref(), idx.into()); - if self.0.is_sorted_flag() && (idx.is_sorted_flag() || idx.is_sorted_reverse_flag()) { + if self.0.is_sorted_ascending_flag() + && (idx.is_sorted_ascending_flag() || idx.is_sorted_descending_flag()) + { out.set_sorted_flag(idx.is_sorted_flag2()) } diff --git a/polars/polars-core/src/series/implementations/floats.rs b/polars/polars-core/src/series/implementations/floats.rs index f76823b7138d..e80e219a1b8e 100644 --- a/polars/polars-core/src/series/implementations/floats.rs +++ b/polars/polars-core/src/series/implementations/floats.rs @@ -139,16 +139,16 @@ macro_rules! impl_dyn_series { } #[cfg(feature = "sort_multiple")] - fn arg_sort_multiple(&self, by: &[Series], reverse: &[bool]) -> PolarsResult { - self.0.arg_sort_multiple(by, reverse) + fn arg_sort_multiple(&self, by: &[Series], descending: &[bool]) -> PolarsResult { + self.0.arg_sort_multiple(by, descending) } } impl SeriesTrait for SeriesWrap<$ca> { fn is_sorted_flag(&self) -> IsSorted { - if self.0.is_sorted_flag() { + if self.0.is_sorted_ascending_flag() { IsSorted::Ascending - } else if self.0.is_sorted_reverse_flag() { + } else if self.0.is_sorted_descending_flag() { IsSorted::Descending } else { IsSorted::Not @@ -260,7 +260,8 @@ macro_rules! impl_dyn_series { let mut out = ChunkTake::take_unchecked(&self.0, (&*idx).into()); - if self.0.is_sorted_flag() && (idx.is_sorted_flag() || idx.is_sorted_reverse_flag()) + if self.0.is_sorted_ascending_flag() + && (idx.is_sorted_ascending_flag() || idx.is_sorted_descending_flag()) { out.set_sorted_flag(idx.is_sorted_flag2()) } diff --git a/polars/polars-core/src/series/implementations/mod.rs b/polars/polars-core/src/series/implementations/mod.rs index 52103b99de75..6aa55cf51927 100644 --- a/polars/polars-core/src/series/implementations/mod.rs +++ b/polars/polars-core/src/series/implementations/mod.rs @@ -203,16 +203,16 @@ macro_rules! impl_dyn_series { } #[cfg(feature = "sort_multiple")] - fn arg_sort_multiple(&self, by: &[Series], reverse: &[bool]) -> PolarsResult { - self.0.arg_sort_multiple(by, reverse) + fn arg_sort_multiple(&self, by: &[Series], descending: &[bool]) -> PolarsResult { + self.0.arg_sort_multiple(by, descending) } } impl SeriesTrait for SeriesWrap<$ca> { fn is_sorted_flag(&self) -> IsSorted { - if self.0.is_sorted_flag() { + if self.0.is_sorted_ascending_flag() { IsSorted::Ascending - } else if self.0.is_sorted_reverse_flag() { + } else if self.0.is_sorted_descending_flag() { IsSorted::Descending } else { IsSorted::Not @@ -352,7 +352,8 @@ macro_rules! impl_dyn_series { Cow::Borrowed(idx) }; let mut out = ChunkTake::take_unchecked(&self.0, (&*idx).into()); - if self.0.is_sorted_flag() && (idx.is_sorted_flag() || idx.is_sorted_reverse_flag()) + if self.0.is_sorted_ascending_flag() + && (idx.is_sorted_ascending_flag() || idx.is_sorted_descending_flag()) { out.set_sorted_flag(idx.is_sorted_flag2()) } diff --git a/polars/polars-core/src/series/implementations/utf8.rs b/polars/polars-core/src/series/implementations/utf8.rs index a90777b519e0..51d46b913c09 100644 --- a/polars/polars-core/src/series/implementations/utf8.rs +++ b/polars/polars-core/src/series/implementations/utf8.rs @@ -97,16 +97,16 @@ impl private::PrivateSeries for SeriesWrap { } #[cfg(feature = "sort_multiple")] - fn arg_sort_multiple(&self, by: &[Series], reverse: &[bool]) -> PolarsResult { - self.0.arg_sort_multiple(by, reverse) + fn arg_sort_multiple(&self, by: &[Series], descending: &[bool]) -> PolarsResult { + self.0.arg_sort_multiple(by, descending) } } impl SeriesTrait for SeriesWrap { fn is_sorted_flag(&self) -> IsSorted { - if self.0.is_sorted_flag() { + if self.0.is_sorted_ascending_flag() { IsSorted::Ascending - } else if self.0.is_sorted_reverse_flag() { + } else if self.0.is_sorted_descending_flag() { IsSorted::Descending } else { IsSorted::Not @@ -202,7 +202,9 @@ impl SeriesTrait for SeriesWrap { let mut out = ChunkTake::take_unchecked(&self.0, (&*idx).into()); - if self.0.is_sorted_flag() && (idx.is_sorted_flag() || idx.is_sorted_reverse_flag()) { + if self.0.is_sorted_ascending_flag() + && (idx.is_sorted_ascending_flag() || idx.is_sorted_descending_flag()) + { out.set_sorted_flag(idx.is_sorted_flag2()) } diff --git a/polars/polars-core/src/series/mod.rs b/polars/polars-core/src/series/mod.rs index 1e6cde55a684..39d8494331af 100644 --- a/polars/polars-core/src/series/mod.rs +++ b/polars/polars-core/src/series/mod.rs @@ -214,9 +214,9 @@ impl Series { Ok(self) } - pub fn sort(&self, reverse: bool) -> Self { + pub fn sort(&self, descending: bool) -> Self { self.sort_with(SortOptions { - descending: reverse, + descending, ..Default::default() }) } diff --git a/polars/polars-core/src/series/series_trait.rs b/polars/polars-core/src/series/series_trait.rs index c11ce63a22db..4ec25b33e2cd 100644 --- a/polars/polars-core/src/series/series_trait.rs +++ b/polars/polars-core/src/series/series_trait.rs @@ -178,7 +178,7 @@ pub(crate) mod private { invalid_operation_panic!(self) } #[cfg(feature = "sort_multiple")] - fn arg_sort_multiple(&self, _by: &[Series], _reverse: &[bool]) -> PolarsResult { + fn arg_sort_multiple(&self, _by: &[Series], _descending: &[bool]) -> PolarsResult { Err(PolarsError::InvalidOperation( "arg_sort_multiple is not implemented for this Series".into(), )) diff --git a/polars/polars-lazy/polars-pipe/src/executors/sinks/sort/ooc.rs b/polars/polars-lazy/polars-pipe/src/executors/sinks/sort/ooc.rs index f40c86a38072..ffccb722cb43 100644 --- a/polars/polars-lazy/polars-pipe/src/executors/sinks/sort/ooc.rs +++ b/polars/polars-lazy/polars-pipe/src/executors/sinks/sort/ooc.rs @@ -22,7 +22,7 @@ pub(super) fn sort_ooc( io_thread: &IOThread, partitions: Series, idx: usize, - reverse: bool, + descending: bool, slice: Option<(i64, usize)>, ) -> PolarsResult { let partitions = partitions.to_physical_repr().into_owned(); @@ -42,7 +42,7 @@ pub(super) fn sort_ooc( let df = read_df(entry)?; let sort_col = &df.get_columns()[idx]; - let assigned_parts = det_partitions(sort_col, &partitions, reverse); + let assigned_parts = det_partitions(sort_col, &partitions, descending); // partition the dataframe into proper buckets let (iter, unique_assigned_parts) = partition_df(df, &assigned_parts)?; @@ -71,14 +71,14 @@ pub(super) fn sort_ooc( }) .collect::>>()?; - let source = SortSource::new(files, idx, reverse, slice, partitions); + let source = SortSource::new(files, idx, descending, slice, partitions); Ok(FinalizedSink::Source(Box::new(source))) } -fn det_partitions(s: &Series, partitions: &Series, reverse: bool) -> IdxCa { +fn det_partitions(s: &Series, partitions: &Series, descending: bool) -> IdxCa { let s = s.to_physical_repr(); - search_sorted(partitions, &s, SearchSortedSide::Any, reverse).unwrap() + search_sorted(partitions, &s, SearchSortedSide::Any, descending).unwrap() } fn partition_df(df: DataFrame, partitions: &IdxCa) -> PolarsResult<(DfIter, IdxCa)> { diff --git a/polars/polars-lazy/polars-pipe/src/executors/sinks/sort/sink.rs b/polars/polars-lazy/polars-pipe/src/executors/sinks/sort/sink.rs index 9edc879e738a..e0612ac3befe 100644 --- a/polars/polars-lazy/polars-pipe/src/executors/sinks/sort/sink.rs +++ b/polars/polars-lazy/polars-pipe/src/executors/sinks/sort/sink.rs @@ -26,7 +26,7 @@ pub struct SortSink { io_thread: Arc>>, // location in the dataframe of the columns to sort by sort_idx: usize, - reverse: bool, + descending: bool, slice: Option<(i64, usize)>, // sampled values so we can find the distribution. dist_sample: Vec>, @@ -35,7 +35,7 @@ pub struct SortSink { impl SortSink { pub(crate) fn new( sort_idx: usize, - reverse: bool, + descending: bool, schema: SchemaRef, slice: Option<(i64, usize)>, ) -> Self { @@ -50,7 +50,7 @@ impl SortSink { ooc, io_thread: Default::default(), sort_idx, - reverse, + descending, slice, dist_sample: vec![], }; @@ -150,7 +150,7 @@ impl Sink for SortSink { ooc: self.ooc, io_thread: self.io_thread.clone(), sort_idx: self.sort_idx, - reverse: self.reverse, + descending: self.descending, dist_sample: vec![], slice: self.slice, }) @@ -168,15 +168,15 @@ impl Sink for SortSink { let io_thread = lock.as_ref().unwrap(); let dist = Series::from_any_values("", &self.dist_sample).unwrap(); - let dist = dist.sort(self.reverse); + let dist = dist.sort(self.descending); block_thread_until_io_thread_done(io_thread); - sort_ooc(io_thread, dist, self.sort_idx, self.reverse, self.slice) + sort_ooc(io_thread, dist, self.sort_idx, self.descending, self.slice) } else { let chunks = std::mem::take(&mut self.chunks); let df = accumulate_dataframes_vertical_unchecked(chunks); - let df = sort_accumulated(df, self.sort_idx, self.reverse, self.slice)?; + let df = sort_accumulated(df, self.sort_idx, self.descending, self.slice)?; Ok(FinalizedSink::Finished(df)) } } @@ -193,9 +193,9 @@ impl Sink for SortSink { pub(super) fn sort_accumulated( df: DataFrame, sort_idx: usize, - reverse: bool, + descending: bool, slice: Option<(i64, usize)>, ) -> PolarsResult { let sort_column = df.get_columns()[sort_idx].clone(); - df.sort_impl(vec![sort_column], vec![reverse], false, slice, true) + df.sort_impl(vec![sort_column], vec![descending], false, slice, true) } diff --git a/polars/polars-lazy/polars-pipe/src/executors/sinks/sort/source.rs b/polars/polars-lazy/polars-pipe/src/executors/sinks/sort/source.rs index 1b61289c6337..bc2b75c5622d 100644 --- a/polars/polars-lazy/polars-pipe/src/executors/sinks/sort/source.rs +++ b/polars/polars-lazy/polars-pipe/src/executors/sinks/sort/source.rs @@ -14,7 +14,7 @@ pub struct SortSource { files: std::vec::IntoIter<(u32, PathBuf)>, n_threads: usize, sort_idx: usize, - reverse: bool, + descending: bool, chunk_offset: IdxSize, slice: Option<(i64, usize)>, finished: bool, @@ -35,7 +35,7 @@ impl SortSource { pub(super) fn new( mut files: Vec<(u32, PathBuf)>, sort_idx: usize, - reverse: bool, + descending: bool, slice: Option<(i64, usize)>, partitions: Series, ) -> Self { @@ -48,7 +48,7 @@ impl SortSource { files, n_threads, sort_idx, - reverse, + descending, chunk_offset: 0, slice, finished: false, @@ -154,7 +154,7 @@ impl Source for SortSource { // sort a single partition let current_slice = self.slice; let mut df = match &mut self.slice { - None => sort_accumulated(df, self.sort_idx, self.reverse, None), + None => sort_accumulated(df, self.sort_idx, self.descending, None), Some((offset, len)) => { let df_len = df.height(); assert!(*offset >= 0); @@ -163,7 +163,7 @@ impl Source for SortSource { Ok(df.slice(0, 0)) } else { let out = - sort_accumulated(df, self.sort_idx, self.reverse, current_slice); + sort_accumulated(df, self.sort_idx, self.descending, current_slice); *len = len.saturating_sub(df_len); *offset = 0; out diff --git a/polars/polars-lazy/polars-pipe/src/pipeline/convert.rs b/polars/polars-lazy/polars-pipe/src/pipeline/convert.rs index ca8c31a670ed..b9803acbb0e9 100644 --- a/polars/polars-lazy/polars-pipe/src/pipeline/convert.rs +++ b/polars/polars-lazy/polars-pipe/src/pipeline/convert.rs @@ -199,7 +199,7 @@ where let sort_sink = SortSink::new( index, - args.reverse[0], + args.descending[0], input_schema.into_owned(), args.slice, ); diff --git a/polars/polars-lazy/polars-plan/src/dsl/expr.rs b/polars/polars-lazy/polars-plan/src/dsl/expr.rs index d10616be89f2..46ba564bd9cd 100644 --- a/polars/polars-lazy/polars-plan/src/dsl/expr.rs +++ b/polars/polars-lazy/polars-plan/src/dsl/expr.rs @@ -309,7 +309,7 @@ pub enum Expr { SortBy { expr: Box, by: Vec, - reverse: Vec, + descending: Vec, }, Agg(AggExpr), /// A ternary operation diff --git a/polars/polars-lazy/polars-plan/src/dsl/function_expr/mod.rs b/polars/polars-lazy/polars-plan/src/dsl/function_expr/mod.rs index 77cfe84eb99f..7dc322589f87 100644 --- a/polars/polars-lazy/polars-plan/src/dsl/function_expr/mod.rs +++ b/polars/polars-lazy/polars-plan/src/dsl/function_expr/mod.rs @@ -102,7 +102,7 @@ pub enum FunctionExpr { #[cfg(feature = "top_k")] TopK { k: usize, - reverse: bool, + descending: bool, }, Shift(i64), Reverse, @@ -361,8 +361,8 @@ impl From for SpecialEq> { } } #[cfg(feature = "top_k")] - TopK { k, reverse } => { - map!(top_k, k, reverse) + TopK { k, descending } => { + map!(top_k, k, descending) } Shift(periods) => map!(dispatch::shift, periods), Reverse => map!(dispatch::reverse), diff --git a/polars/polars-lazy/polars-plan/src/dsl/functions.rs b/polars/polars-lazy/polars-plan/src/dsl/functions.rs index 275205e1d0bb..34dc682202c1 100644 --- a/polars/polars-lazy/polars-plan/src/dsl/functions.rs +++ b/polars/polars-lazy/polars-plan/src/dsl/functions.rs @@ -233,10 +233,10 @@ pub fn spearman_rank_corr(a: Expr, b: Expr, ddof: u8, propagate_nans: bool) -> E /// That means that the first `Series` will be used to determine the ordering /// until duplicates are found. Once duplicates are found, the next `Series` will /// be used and so on. -pub fn arg_sort_by>(by: E, reverse: &[bool]) -> Expr { - let reverse = reverse.to_vec(); +pub fn arg_sort_by>(by: E, descending: &[bool]) -> Expr { + let descending = descending.to_vec(); let function = SpecialEq::new(Arc::new(move |by: &mut [Series]| { - polars_core::functions::arg_sort_by(by, &reverse).map(|ca| Some(ca.into_series())) + polars_core::functions::arg_sort_by(by, &descending).map(|ca| Some(ca.into_series())) }) as Arc); Expr::AnonymousFunction { diff --git a/polars/polars-lazy/polars-plan/src/dsl/mod.rs b/polars/polars-lazy/polars-plan/src/dsl/mod.rs index 736d7411c567..17aff0a51e64 100644 --- a/polars/polars-lazy/polars-plan/src/dsl/mod.rs +++ b/polars/polars-lazy/polars-plan/src/dsl/mod.rs @@ -592,11 +592,11 @@ impl Expr { } /// Sort in increasing order. See [the eager implementation](Series::sort). - pub fn sort(self, reverse: bool) -> Self { + pub fn sort(self, descending: bool) -> Self { Expr::Sort { expr: Box::new(self), options: SortOptions { - descending: reverse, + descending, ..Default::default() }, } @@ -614,8 +614,8 @@ impl Expr { /// /// This has time complexity `O(n + k log(n))`. #[cfg(feature = "top_k")] - pub fn top_k(self, k: usize, reverse: bool) -> Self { - self.apply_private(FunctionExpr::TopK { k, reverse }) + pub fn top_k(self, k: usize, descending: bool) -> Self { + self.apply_private(FunctionExpr::TopK { k, descending }) } /// Reverse column @@ -1231,14 +1231,14 @@ impl Expr { pub fn sort_by, IE: Into + Clone, R: AsRef<[bool]>>( self, by: E, - reverse: R, + descending: R, ) -> Expr { let by = by.as_ref().iter().map(|e| e.clone().into()).collect(); - let reverse = reverse.as_ref().to_vec(); + let descending = descending.as_ref().to_vec(); Expr::SortBy { expr: Box::new(self), by, - reverse, + descending, } } diff --git a/polars/polars-lazy/polars-plan/src/logical_plan/aexpr/mod.rs b/polars/polars-lazy/polars-plan/src/logical_plan/aexpr/mod.rs index 3c6cb98f6c31..c92d2d82c120 100644 --- a/polars/polars-lazy/polars-plan/src/logical_plan/aexpr/mod.rs +++ b/polars/polars-lazy/polars-plan/src/logical_plan/aexpr/mod.rs @@ -69,7 +69,7 @@ pub enum AExpr { SortBy { expr: Node, by: Vec, - reverse: Vec, + descending: Vec, }, Filter { input: Node, diff --git a/polars/polars-lazy/polars-plan/src/logical_plan/builder.rs b/polars/polars-lazy/polars-plan/src/logical_plan/builder.rs index cc68458f977d..0eb00b82eb02 100644 --- a/polars/polars-lazy/polars-plan/src/logical_plan/builder.rs +++ b/polars/polars-lazy/polars-plan/src/logical_plan/builder.rs @@ -556,14 +556,14 @@ impl LogicalPlanBuilder { .into() } - pub fn sort(self, by_column: Vec, reverse: Vec, null_last: bool) -> Self { + pub fn sort(self, by_column: Vec, descending: Vec, null_last: bool) -> Self { let schema = try_delayed!(self.0.schema(), &self.0, into); let by_column = try_delayed!(rewrite_projections(by_column, &schema, &[]), &self.0, into); LogicalPlan::Sort { input: Box::new(self.0), by_column, args: SortArguments { - reverse, + descending, nulls_last: null_last, slice: None, }, diff --git a/polars/polars-lazy/polars-plan/src/logical_plan/conversion.rs b/polars/polars-lazy/polars-plan/src/logical_plan/conversion.rs index 6633affcfe0f..6445536e0dc6 100644 --- a/polars/polars-lazy/polars-plan/src/logical_plan/conversion.rs +++ b/polars/polars-lazy/polars-plan/src/logical_plan/conversion.rs @@ -39,10 +39,14 @@ pub fn to_aexpr(expr: Expr, arena: &mut Arena) -> Node { expr: to_aexpr(*expr, arena), options, }, - Expr::SortBy { expr, by, reverse } => AExpr::SortBy { + Expr::SortBy { + expr, + by, + descending, + } => AExpr::SortBy { expr: to_aexpr(*expr, arena), by: by.into_iter().map(|e| to_aexpr(e, arena)).collect(), - reverse, + descending, }, Expr::Filter { input, by } => AExpr::Filter { input: to_aexpr(*input, arena), @@ -475,7 +479,11 @@ pub fn node_to_expr(node: Node, expr_arena: &Arena) -> Expr { idx: Box::new(idx), } } - AExpr::SortBy { expr, by, reverse } => { + AExpr::SortBy { + expr, + by, + descending, + } => { let expr = node_to_expr(expr, expr_arena); let by = by .iter() @@ -484,7 +492,7 @@ pub fn node_to_expr(node: Node, expr_arena: &Arena) -> Expr { Expr::SortBy { expr: Box::new(expr), by, - reverse, + descending, } } AExpr::Filter { input, by } => { diff --git a/polars/polars-lazy/polars-plan/src/logical_plan/format.rs b/polars/polars-lazy/polars-plan/src/logical_plan/format.rs index 2bc22bf15d4d..81ecd1457418 100644 --- a/polars/polars-lazy/polars-plan/src/logical_plan/format.rs +++ b/polars/polars-lazy/polars-plan/src/logical_plan/format.rs @@ -306,8 +306,12 @@ impl fmt::Debug for Expr { true => write!(f, "{expr:?} DESC"), false => write!(f, "{expr:?} ASC"), }, - SortBy { expr, by, reverse } => { - write!(f, "SORT {expr:?} BY {by:?} REVERSE ORDERING {reverse:?}",) + SortBy { + expr, + by, + descending, + } => { + write!(f, "SORT {expr:?} BY {by:?} REVERSE ORDERING {descending:?}",) } Filter { input, by } => { write!(f, "{input:?}\nFILTER WHERE {by:?}") diff --git a/polars/polars-lazy/polars-plan/src/logical_plan/optimizer/simplify_expr.rs b/polars/polars-lazy/polars-plan/src/logical_plan/optimizer/simplify_expr.rs index 45140068bb57..d7af577e319e 100644 --- a/polars/polars-lazy/polars-plan/src/logical_plan/optimizer/simplify_expr.rs +++ b/polars/polars-lazy/polars-plan/src/logical_plan/optimizer/simplify_expr.rs @@ -610,10 +610,14 @@ impl OptimizationRule for SimplifyExprRule { options, }) } - AExpr::SortBy { expr, by, reverse } => Some(AExpr::SortBy { + AExpr::SortBy { + expr, + by, + descending, + } => Some(AExpr::SortBy { expr: *expr, by: by.clone(), - reverse: reverse.iter().map(|r| !*r).collect(), + descending: descending.iter().map(|r| !*r).collect(), }), // TODO: add support for cumsum and other operation that allow reversing. _ => None, diff --git a/polars/polars-lazy/polars-plan/src/logical_plan/options.rs b/polars/polars-lazy/polars-plan/src/logical_plan/options.rs index 5b24473dacd3..97feb1ae1349 100644 --- a/polars/polars-lazy/polars-plan/src/logical_plan/options.rs +++ b/polars/polars-lazy/polars-plan/src/logical_plan/options.rs @@ -253,7 +253,7 @@ pub struct LogicalPlanUdfOptions { #[derive(Clone, PartialEq, Eq, Debug)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct SortArguments { - pub reverse: Vec, + pub descending: Vec, // Can only be true in case of a single column. pub nulls_last: bool, pub slice: Option<(i64, usize)>, diff --git a/polars/polars-lazy/src/frame/mod.rs b/polars/polars-lazy/src/frame/mod.rs index d2c23b95a235..8fb4fd78b714 100644 --- a/polars/polars-lazy/src/frame/mod.rs +++ b/polars/polars-lazy/src/frame/mod.rs @@ -206,13 +206,13 @@ impl LazyFrame { /// } /// ``` pub fn sort(self, by_column: &str, options: SortOptions) -> Self { - let reverse = options.descending; + let descending = options.descending; let nulls_last = options.nulls_last; let opt_state = self.get_opt_state(); let lp = self .get_plan_builder() - .sort(vec![col(by_column)], vec![reverse], nulls_last) + .sort(vec![col(by_column)], vec![descending], nulls_last) .build(); Self::from_logical_plan(lp, opt_state) } @@ -234,18 +234,18 @@ impl LazyFrame { pub fn sort_by_exprs, B: AsRef<[bool]>>( self, by_exprs: E, - reverse: B, + descending: B, nulls_last: bool, ) -> Self { let by_exprs = by_exprs.as_ref().to_vec(); - let reverse = reverse.as_ref().to_vec(); + let descending = descending.as_ref().to_vec(); if by_exprs.is_empty() { self } else { let opt_state = self.get_opt_state(); let lp = self .get_plan_builder() - .sort(by_exprs, reverse, nulls_last) + .sort(by_exprs, descending, nulls_last) .build(); Self::from_logical_plan(lp, opt_state) } diff --git a/polars/polars-lazy/src/physical_plan/executors/sort.rs b/polars/polars-lazy/src/physical_plan/executors/sort.rs index 72b39afefdfb..0296ae6bc742 100644 --- a/polars/polars-lazy/src/physical_plan/executors/sort.rs +++ b/polars/polars-lazy/src/physical_plan/executors/sort.rs @@ -33,7 +33,7 @@ impl SortExec { df.sort_impl( by_columns, - std::mem::take(&mut self.args.reverse), + std::mem::take(&mut self.args.descending), self.args.nulls_last, self.args.slice, true, diff --git a/polars/polars-lazy/src/physical_plan/expressions/sortby.rs b/polars/polars-lazy/src/physical_plan/expressions/sortby.rs index 454c4a6bc3ad..736ab9bf8507 100644 --- a/polars/polars-lazy/src/physical_plan/expressions/sortby.rs +++ b/polars/polars-lazy/src/physical_plan/expressions/sortby.rs @@ -13,7 +13,7 @@ use crate::prelude::*; pub struct SortByExpr { pub(crate) input: Arc, pub(crate) by: Vec>, - pub(crate) reverse: Vec, + pub(crate) descending: Vec, pub(crate) expr: Expr, } @@ -21,26 +21,26 @@ impl SortByExpr { pub fn new( input: Arc, by: Vec>, - reverse: Vec, + descending: Vec, expr: Expr, ) -> Self { Self { input, by, - reverse, + descending, expr, } } } -fn prepare_reverse(reverse: &[bool], by_len: usize) -> Vec { - match (reverse.len(), by_len) { +fn prepare_descending(descending: &[bool], by_len: usize) -> Vec { + match (descending.len(), by_len) { // equal length - (n_reverse, n) if n_reverse == n => reverse.to_vec(), + (n_rdescending, n) if n_rdescending == n => descending.to_vec(), // none given all false (0, n) => vec![false; n], // broadcast first - (_, n) => vec![reverse[0]; n], + (_, n) => vec![descending[0]; n], } } @@ -50,13 +50,13 @@ impl PhysicalExpr for SortByExpr { } fn evaluate(&self, df: &DataFrame, state: &ExecutionState) -> PolarsResult { let series_f = || self.input.evaluate(df, state); - let reverse = prepare_reverse(&self.reverse, self.by.len()); + let descending = prepare_descending(&self.descending, self.by.len()); let (series, sorted_idx) = if self.by.len() == 1 { let sorted_idx_f = || { let s_sort_by = self.by[0].evaluate(df, state)?; Ok(s_sort_by.arg_sort(SortOptions { - descending: reverse[0], + descending: descending[0], ..Default::default() })) }; @@ -69,7 +69,7 @@ impl PhysicalExpr for SortByExpr { .map(|e| e.evaluate(df, state)) .collect::>>()?; - s_sort_by[0].arg_sort_multiple(&s_sort_by[1..], &reverse) + s_sort_by[0].arg_sort_multiple(&s_sort_by[1..], &descending) }; POOL.install(|| rayon::join(series_f, sorted_idx_f)) }; @@ -114,7 +114,7 @@ impl PhysicalExpr for SortByExpr { let s = ac_in.aggregated(); let mut s = s.list().unwrap().clone(); - let descending = self.reverse[0]; + let descending = self.descending[0]; let mut ca: ListChunked = s .par_iter_indexed() .zip(sort_by.par_iter_indexed()) @@ -139,7 +139,7 @@ impl PhysicalExpr for SortByExpr { ac_in.with_series(s, true, Some(&self.expr))?; Ok(ac_in) } else { - let reverse = prepare_reverse(&self.reverse, self.by.len()); + let descending = prepare_descending(&self.descending, self.by.len()); let (groups, ordered_by_group_operation) = if self.by.len() == 1 { let mut ac_sort_by = self.by[0].evaluate_on_groups(df, groups, state)?; @@ -169,7 +169,7 @@ impl PhysicalExpr for SortByExpr { }; let sorted_idx = group.arg_sort(SortOptions { - descending: reverse[0], + descending: descending[0], ..Default::default() }); map_sorted_indices_to_group_idx(&sorted_idx, idx) @@ -177,7 +177,7 @@ impl PhysicalExpr for SortByExpr { GroupsIndicator::Slice([first, len]) => { let group = sort_by_s.slice(first as i64, len as usize); let sorted_idx = group.arg_sort(SortOptions { - descending: reverse[0], + descending: descending[0], ..Default::default() }); map_sorted_indices_to_group_slice(&sorted_idx, first) @@ -228,8 +228,9 @@ impl PhysicalExpr for SortByExpr { }) .collect::>(); - let sorted_idx = - groups[0].arg_sort_multiple(&groups[1..], &reverse).unwrap(); + let sorted_idx = groups[0] + .arg_sort_multiple(&groups[1..], &descending) + .unwrap(); map_sorted_indices_to_group_idx(&sorted_idx, idx) } GroupsIndicator::Slice([first, len]) => { @@ -237,8 +238,9 @@ impl PhysicalExpr for SortByExpr { .iter() .map(|s| s.slice(first as i64, len as usize)) .collect::>(); - let sorted_idx = - groups[0].arg_sort_multiple(&groups[1..], &reverse).unwrap(); + let sorted_idx = groups[0] + .arg_sort_multiple(&groups[1..], &descending) + .unwrap(); map_sorted_indices_to_group_slice(&sorted_idx, first) } }; diff --git a/polars/polars-lazy/src/physical_plan/planner/expr.rs b/polars/polars-lazy/src/physical_plan/planner/expr.rs index 252071b65642..c36c77110b67 100644 --- a/polars/polars-lazy/src/physical_plan/planner/expr.rs +++ b/polars/polars-lazy/src/physical_plan/planner/expr.rs @@ -114,13 +114,17 @@ pub(crate) fn create_physical_expr( expr: node_to_expr(expression, expr_arena), })) } - SortBy { expr, by, reverse } => { + SortBy { + expr, + by, + descending, + } => { let phys_expr = create_physical_expr(expr, ctxt, expr_arena, schema)?; let phys_by = create_physical_expressions(&by, ctxt, expr_arena, schema)?; Ok(Arc::new(SortByExpr::new( phys_expr, phys_by, - reverse, + descending, node_to_expr(expression, expr_arena), ))) } diff --git a/polars/polars-ops/src/chunked_array/top_k.rs b/polars/polars-ops/src/chunked_array/top_k.rs index 84b95d2207d9..5d1811ca7457 100644 --- a/polars/polars-ops/src/chunked_array/top_k.rs +++ b/polars/polars-ops/src/chunked_array/top_k.rs @@ -60,7 +60,7 @@ where Ok(out) } -pub fn top_k(s: &Series, k: usize, reverse: bool) -> PolarsResult { +pub fn top_k(s: &Series, k: usize, descending: bool) -> PolarsResult { if s.is_empty() { return Ok(s.clone()); } @@ -70,7 +70,7 @@ pub fn top_k(s: &Series, k: usize, reverse: bool) -> PolarsResult { macro_rules! dispatch { ($ca:expr) => {{ - let mult_order = if reverse { -1 } else { 1 }; + let mult_order = if descending { -1 } else { 1 }; top_k_impl($ca, k, NumCast::from(mult_order).unwrap()).map(|ca| ca.into_series()) }}; } diff --git a/polars/polars-ops/src/series/ops/search_sorted.rs b/polars/polars-ops/src/series/ops/search_sorted.rs index 9f43160b4fed..68e4c98c187f 100644 --- a/polars/polars-ops/src/series/ops/search_sorted.rs +++ b/polars/polars-ops/src/series/ops/search_sorted.rs @@ -100,7 +100,7 @@ fn binary_search_array( arr: G, len: usize, search_value: I, - reverse: bool, + descending: bool, ) where G: GetArray, I: PartialEq + Debug + Copy + PartialOrd + IsFloat, @@ -118,7 +118,7 @@ fn binary_search_array( let cmp = match unsafe { arr._get_value_unchecked(mid as usize) } { None => Ordering::Less, Some(value) => { - if reverse { + if descending { compare_fn_nan_max(&search_value, &value) } else { compare_fn_nan_max(&value, &search_value) @@ -149,7 +149,7 @@ fn search_sorted_ca_array( ca: &ChunkedArray, search_values: &ChunkedArray, side: SearchSortedSide, - reverse: bool, + descending: bool, ) -> Vec where T: PolarsNumericType, @@ -162,15 +162,20 @@ where for search_arr in search_values.downcast_iter() { if search_arr.null_count() == 0 { for search_value in search_arr.values_iter() { - binary_search_array(side, &mut out, arr, ca.len(), *search_value, reverse) + binary_search_array(side, &mut out, arr, ca.len(), *search_value, descending) } } else { for opt_v in search_arr.into_iter() { match opt_v { None => out.push(0), - Some(search_value) => { - binary_search_array(side, &mut out, arr, ca.len(), *search_value, reverse) - } + Some(search_value) => binary_search_array( + side, + &mut out, + arr, + ca.len(), + *search_value, + descending, + ), } } } @@ -182,7 +187,7 @@ fn search_sorted_utf8_array( ca: &Utf8Chunked, search_values: &Utf8Chunked, side: SearchSortedSide, - reverse: bool, + descending: bool, ) -> Vec { let ca = ca.rechunk(); let arr = ca.downcast_iter().next().unwrap(); @@ -192,14 +197,14 @@ fn search_sorted_utf8_array( for search_arr in search_values.downcast_iter() { if search_arr.null_count() == 0 { for search_value in search_arr.values_iter() { - binary_search_array(side, &mut out, arr, ca.len(), search_value, reverse) + binary_search_array(side, &mut out, arr, ca.len(), search_value, descending) } } else { for opt_v in search_arr.into_iter() { match opt_v { None => out.push(0), Some(search_value) => { - binary_search_array(side, &mut out, arr, ca.len(), search_value, reverse) + binary_search_array(side, &mut out, arr, ca.len(), search_value, descending) } } } @@ -212,7 +217,7 @@ pub fn search_sorted( s: &Series, search_values: &Series, side: SearchSortedSide, - reverse: bool, + descending: bool, ) -> PolarsResult { let original_dtype = s.dtype(); let s = s.to_physical_repr(); @@ -222,7 +227,7 @@ pub fn search_sorted( DataType::Utf8 => { let ca = s.utf8().unwrap(); let search_values = search_values.utf8()?; - let idx = search_sorted_utf8_array(ca, search_values, side, reverse); + let idx = search_sorted_utf8_array(ca, search_values, side, descending); Ok(IdxCa::new_vec(s.name(), idx)) } @@ -233,7 +238,7 @@ pub fn search_sorted( let ca: &ChunkedArray<$T> = s.as_ref().as_ref().as_ref(); let search_values: &ChunkedArray<$T> = search_values.as_ref().as_ref().as_ref(); - search_sorted_ca_array(ca, search_values, side, reverse) + search_sorted_ca_array(ca, search_values, side, descending) }); Ok(IdxCa::new_vec(s.name(), idx)) } diff --git a/polars/polars-sql/src/context.rs b/polars/polars-sql/src/context.rs index 508714dfc811..b068bebe6138 100644 --- a/polars/polars-sql/src/context.rs +++ b/polars/polars-sql/src/context.rs @@ -235,14 +235,14 @@ impl SQLContext { fn process_order_by(&self, lf: LazyFrame, ob: &[OrderByExpr]) -> PolarsResult { let mut by = Vec::with_capacity(ob.len()); - let mut reverse = Vec::with_capacity(ob.len()); + let mut descending = Vec::with_capacity(ob.len()); for ob in ob { by.push(parse_sql_expr(&ob.expr)?); if let Some(false) = ob.asc { - reverse.push(true) + descending.push(true) } else { - reverse.push(false) + descending.push(false) } if ob.nulls_first.is_some() { @@ -252,7 +252,7 @@ impl SQLContext { } } - Ok(lf.sort_by_exprs(&by, reverse, false)) + Ok(lf.sort_by_exprs(&by, descending, false)) } fn process_groupby( diff --git a/polars/src/docs/eager.rs b/polars/src/docs/eager.rs index 56303d2378c1..670fac34b3b0 100644 --- a/polars/src/docs/eager.rs +++ b/polars/src/docs/eager.rs @@ -345,11 +345,11 @@ //! // sort this DataFrame by multiple columns //! //! // ordering of the columns -//! let reverse = vec![true, false]; +//! let descending = vec![true, false]; //! // columns to sort by //! let by = &["b", "a"]; //! // do the sort operation -//! let sorted = df.sort(by, reverse)?; +//! let sorted = df.sort(by, descending)?; //! //! // sorted: //! diff --git a/polars/src/docs/lazy.rs b/polars/src/docs/lazy.rs index 1dce8a7b5c5c..80ee9b8b356d 100644 --- a/polars/src/docs/lazy.rs +++ b/polars/src/docs/lazy.rs @@ -82,10 +82,10 @@ //! // sort this DataFrame by multiple columns //! //! // ordering of the columns -//! let reverse = vec![true, false]; +//! let descending = vec![true, false]; //! //! let sorted = df.lazy() -//! .sort_by_exprs(vec![col("b"), col("a")], reverse, false) +//! .sort_by_exprs(vec![col("b"), col("a")], descending, false) //! .collect()?; //! //! // sorted: diff --git a/polars/tests/it/core/groupby.rs b/polars/tests/it/core/groupby.rs index afebfed8ce27..9a5696e5aa35 100644 --- a/polars/tests/it/core/groupby.rs +++ b/polars/tests/it/core/groupby.rs @@ -31,7 +31,7 @@ fn test_sorted_groupby() -> PolarsResult<()> { assert_eq!(out.unwrap_slice(), &[[0, 3], [3, 2], [5, 1]]); } - // nulls first reverse sorted + // nulls first descending sorted let mut s = Series::new( "a", &[ @@ -51,7 +51,7 @@ fn test_sorted_groupby() -> PolarsResult<()> { assert_eq!(out.unwrap_slice(), &[[0, 2], [2, 2], [4, 3], [7, 1]]); } - // nulls last reverse sorted + // nulls last descending sorted let mut s = Series::new( "a", &[ diff --git a/py-polars/polars/internals/dataframe/frame.py b/py-polars/polars/internals/dataframe/frame.py index 24556b92fe97..d5bc64e3cb0a 100644 --- a/py-polars/polars/internals/dataframe/frame.py +++ b/py-polars/polars/internals/dataframe/frame.py @@ -3054,11 +3054,12 @@ def replace_at_idx(self, index: int, series: pli.Series) -> Self: self._df.replace_at_idx(index, series._s) return self + @deprecated_alias(reverse="descending") def sort( self, by: IntoExpr | Iterable[IntoExpr], *more_by: IntoExpr, - reverse: bool | Sequence[bool] = False, + descending: bool | Sequence[bool] = False, nulls_last: bool = False, ) -> Self: """ @@ -3071,7 +3072,7 @@ def sort( names. *more_by Additional columns to sort by, specified as positional arguments. - reverse + descending Sort in descending order. When sorting by multiple columns, can be specified per column by passing a sequence of booleans. nulls_last @@ -3116,7 +3117,7 @@ def sort( Sort by multiple columns by passing a list of columns. - >>> df.sort(["c", "a"], reverse=True) + >>> df.sort(["c", "a"], descending=True) shape: (3, 3) ┌──────┬─────┬─────┐ │ a ┆ b ┆ c │ @@ -3130,7 +3131,7 @@ def sort( Or use positional arguments to sort by multiple columns in the same way. - >>> df.sort("c", "a", reverse=[False, True]) + >>> df.sort("c", "a", descending=[False, True]) shape: (3, 3) ┌──────┬─────┬─────┐ │ a ┆ b ┆ c │ @@ -3145,7 +3146,7 @@ def sort( """ return self._from_pydf( self.lazy() - .sort(by, *more_by, reverse=reverse, nulls_last=nulls_last) + .sort(by, *more_by, descending=descending, nulls_last=nulls_last) .collect(no_optimization=True) ._df ) diff --git a/py-polars/polars/internals/expr/expr.py b/py-polars/polars/internals/expr/expr.py index b1de27a5163f..5ff2acf75140 100644 --- a/py-polars/polars/internals/expr/expr.py +++ b/py-polars/polars/internals/expr/expr.py @@ -1694,8 +1694,9 @@ def cast(self, dtype: PolarsDataType | type[Any], strict: bool = True) -> Self: dtype = py_type_to_dtype(dtype) return self._from_pyexpr(self._pyexpr.cast(dtype, strict)) + @deprecated_alias(reverse="descending") @deprecate_nonkeyword_arguments() - def sort(self, reverse: bool = False, nulls_last: bool = False) -> Self: + def sort(self, descending: bool = False, nulls_last: bool = False) -> Self: """ Sort this column. @@ -1704,7 +1705,7 @@ def sort(self, reverse: bool = False, nulls_last: bool = False) -> Self: Parameters ---------- - reverse + descending Sort in descending order. nulls_last Place null values last. @@ -1728,7 +1729,7 @@ def sort(self, reverse: bool = False, nulls_last: bool = False) -> Self: │ 2 │ │ 3 │ └──────┘ - >>> df.select(pl.col("a").sort(reverse=True)) + >>> df.select(pl.col("a").sort(descending=True)) shape: (4, 1) ┌──────┐ │ a │ @@ -1774,13 +1775,14 @@ def sort(self, reverse: bool = False, nulls_last: bool = False) -> Self: └───────┴────────────┘ """ - return self._from_pyexpr(self._pyexpr.sort_with(reverse, nulls_last)) + return self._from_pyexpr(self._pyexpr.sort_with(descending, nulls_last)) - def top_k(self, k: int = 5, reverse: bool = False) -> Self: + @deprecated_alias(reverse="descending") + def top_k(self, k: int = 5, descending: bool = False) -> Self: r""" Return the `k` largest elements. - If 'reverse=True` the smallest elements will be given. + If 'descending=True` the smallest elements will be given. This has time complexity: @@ -1790,7 +1792,7 @@ def top_k(self, k: int = 5, reverse: bool = False) -> Self: ---------- k Number of elements to return. - reverse + descending Return the smallest elements. Examples @@ -1803,7 +1805,7 @@ def top_k(self, k: int = 5, reverse: bool = False) -> Self: >>> df.select( ... [ ... pl.col("value").top_k().alias("top_k"), - ... pl.col("value").top_k(reverse=True).alias("bottom_k"), + ... pl.col("value").top_k(descending=True).alias("bottom_k"), ... ] ... ) shape: (5, 2) @@ -1820,17 +1822,18 @@ def top_k(self, k: int = 5, reverse: bool = False) -> Self: └───────┴──────────┘ """ - return self._from_pyexpr(self._pyexpr.top_k(k, reverse)) + return self._from_pyexpr(self._pyexpr.top_k(k, descending)) + @deprecated_alias(reverse="descending") @deprecate_nonkeyword_arguments() - def arg_sort(self, reverse: bool = False, nulls_last: bool = False) -> Self: + def arg_sort(self, descending: bool = False, nulls_last: bool = False) -> Self: """ Get the index values that would sort this column. Parameters ---------- - reverse - Sort in reverse (descending) order. + descending + Sort in descending (descending) order. nulls_last Place null values last instead of first. @@ -1859,7 +1862,7 @@ def arg_sort(self, reverse: bool = False, nulls_last: bool = False) -> Self: └─────┘ """ - return self._from_pyexpr(self._pyexpr.arg_sort(reverse, nulls_last)) + return self._from_pyexpr(self._pyexpr.arg_sort(descending, nulls_last)) def arg_max(self) -> Self: """ @@ -1953,11 +1956,12 @@ def search_sorted( element = expr_to_lit_or_expr(element, str_to_lit=False) return self._from_pyexpr(self._pyexpr.search_sorted(element._pyexpr, side)) + @deprecated_alias(reverse="descending") def sort_by( self, by: IntoExpr | Iterable[IntoExpr], *more_by: IntoExpr, - reverse: bool | Sequence[bool] = False, + descending: bool | Sequence[bool] = False, ) -> Self: """ Sort this column by the ordering of other columns. @@ -1972,7 +1976,7 @@ def sort_by( names. *more_by Additional columns to sort by, specified as positional arguments. - reverse + descending Sort in descending order. When sorting by multiple columns, can be specified per column by passing a sequence of booleans. @@ -2017,7 +2021,7 @@ def sort_by( Sort by multiple columns by passing a list of columns. - >>> df.select(pl.col("group").sort_by(["value1", "value2"], reverse=True)) + >>> df.select(pl.col("group").sort_by(["value1", "value2"], descending=True)) shape: (4, 1) ┌───────┐ │ group │ @@ -2061,12 +2065,12 @@ def sort_by( └───────┴───────────┘ """ - if isinstance(reverse, bool): - reverse = [reverse] + if isinstance(descending, bool): + descending = [descending] by = selection_to_pyexpr_list(by) if more_by: by.extend(pli.selection_to_pyexpr_list(more_by)) - return self._from_pyexpr(self._pyexpr.sort_by(by, reverse)) + return self._from_pyexpr(self._pyexpr.sort_by(by, descending)) def take( self, indices: int | list[int] | Expr | pli.Series | np.ndarray[Any, Any] @@ -4666,7 +4670,8 @@ def abs(self) -> Self: """ return self._from_pyexpr(self._pyexpr.abs()) - def argsort(self, reverse: bool = False, nulls_last: bool = False) -> Self: + @deprecated_alias(reverse="descending") + def argsort(self, descending: bool = False, nulls_last: bool = False) -> Self: """ Get the index values that would sort this column. @@ -4677,8 +4682,8 @@ def argsort(self, reverse: bool = False, nulls_last: bool = False) -> Self: Parameters ---------- - reverse - Sort in reverse (descending) order. + descending + Sort in descending order. nulls_last Place null values last instead of first. @@ -4712,9 +4717,10 @@ def argsort(self, reverse: bool = False, nulls_last: bool = False) -> Self: DeprecationWarning, stacklevel=2, ) - return self.arg_sort(reverse, nulls_last) + return self.arg_sort(descending, nulls_last) - def rank(self, method: RankMethod = "average", reverse: bool = False) -> Self: + @deprecated_alias(reverse="descending") + def rank(self, method: RankMethod = "average", descending: bool = False) -> Self: """ Assign ranks to data, dealing with ties appropriately. @@ -4738,8 +4744,8 @@ def rank(self, method: RankMethod = "average", reverse: bool = False) -> Self: the order that the values occur in the Series. - 'random' : Like 'ordinal', but the rank for ties is not dependent on the order that the values occur in the Series. - reverse - Reverse the operation. + descending + Rank in descending order. Examples -------- @@ -4778,7 +4784,7 @@ def rank(self, method: RankMethod = "average", reverse: bool = False) -> Self: └─────┘ """ - return self._from_pyexpr(self._pyexpr.rank(method, reverse)) + return self._from_pyexpr(self._pyexpr.rank(method, descending)) def diff(self, n: int = 1, null_behavior: NullBehavior = "ignore") -> Self: """ @@ -6054,7 +6060,8 @@ def cumulative_eval( self._pyexpr.cumulative_eval(expr._pyexpr, min_periods, parallel) ) - def set_sorted(self, reverse: bool = False) -> Self: + @deprecated_alias(reverse="descending") + def set_sorted(self, descending: bool = False) -> Self: """ Flags the expression as 'sorted'. @@ -6062,8 +6069,8 @@ def set_sorted(self, reverse: bool = False) -> Self: Parameters ---------- - reverse - If the `Series` order is reversed, e.g. descending. + descending + Whether the `Series` order is descending. Warnings -------- @@ -6084,7 +6091,7 @@ def set_sorted(self, reverse: bool = False) -> Self: └────────┘ """ - return self._from_pyexpr(self._pyexpr.set_sorted_flag(reverse)) + return self._from_pyexpr(self._pyexpr.set_sorted_flag(descending)) # Keep the `list` and `str` methods below at the end of the definition of Expr, # as to not confuse mypy with the type annotation `str` and `list` diff --git a/py-polars/polars/internals/expr/list.py b/py-polars/polars/internals/expr/list.py index b552c53205a5..2a1be7499378 100644 --- a/py-polars/polars/internals/expr/list.py +++ b/py-polars/polars/internals/expr/list.py @@ -5,7 +5,7 @@ from typing import TYPE_CHECKING, Any, Callable import polars.internals as pli -from polars.utils import deprecate_nonkeyword_arguments +from polars.utils import deprecate_nonkeyword_arguments, deprecated_alias if TYPE_CHECKING: from polars.internals.type_aliases import NullBehavior, ToStructStrategy @@ -125,13 +125,14 @@ def mean(self) -> pli.Expr: return pli.wrap_expr(self._pyexpr.lst_mean()) @deprecate_nonkeyword_arguments() - def sort(self, reverse: bool = False) -> pli.Expr: + @deprecated_alias(reverse="descending") + def sort(self, descending: bool = False) -> pli.Expr: """ Sort the arrays in this column. Parameters ---------- - reverse + descending Sort in descending order. Examples @@ -163,7 +164,7 @@ def sort(self, reverse: bool = False) -> pli.Expr: └───────────┘ """ - return pli.wrap_expr(self._pyexpr.lst_sort(reverse)) + return pli.wrap_expr(self._pyexpr.lst_sort(descending)) def reverse(self) -> pli.Expr: """ diff --git a/py-polars/polars/internals/functions.py b/py-polars/polars/internals/functions.py index 55e9182f94d7..8cf483ee4fde 100644 --- a/py-polars/polars/internals/functions.py +++ b/py-polars/polars/internals/functions.py @@ -6,7 +6,11 @@ from polars import internals as pli from polars.datatypes import Categorical, Date, Float64, PolarsDataType -from polars.utils import _datetime_to_pl_timestamp, _timedelta_to_pl_duration +from polars.utils import ( + _datetime_to_pl_timestamp, + _timedelta_to_pl_duration, + deprecated_alias, +) if sys.version_info >= (3, 10): from typing import Literal @@ -590,7 +594,7 @@ def align_frames( *frames: pli.DataFrame, on: str | pli.Expr | Sequence[str] | Sequence[pli.Expr] | Sequence[str | pli.Expr], select: str | pli.Expr | Sequence[str | pli.Expr] | None = None, - reverse: bool | Sequence[bool] = False, + descending: bool | Sequence[bool] = False, ) -> list[pli.DataFrame]: ... @@ -600,16 +604,17 @@ def align_frames( *frames: pli.LazyFrame, on: str | pli.Expr | Sequence[str] | Sequence[pli.Expr] | Sequence[str | pli.Expr], select: str | pli.Expr | Sequence[str | pli.Expr] | None = None, - reverse: bool | Sequence[bool] = False, + descending: bool | Sequence[bool] = False, ) -> list[pli.LazyFrame]: ... +@deprecated_alias(reverse="descending") def align_frames( *frames: pli.DataFrame | pli.LazyFrame, on: str | pli.Expr | Sequence[str] | Sequence[pli.Expr] | Sequence[str | pli.Expr], select: str | pli.Expr | Sequence[str | pli.Expr] | None = None, - reverse: bool | Sequence[bool] = False, + descending: bool | Sequence[bool] = False, ) -> list[pli.DataFrame] | list[pli.LazyFrame]: r""" Align a sequence of frames using the unique values from one or more columns as a key. @@ -633,7 +638,7 @@ def align_frames( select optional post-alignment column select to constrain and/or order the columns returned from the newly aligned frames. - reverse + descending sort the alignment column values in descending order; can be a single boolean or a list of booleans associated with each column in ``on``. @@ -743,7 +748,7 @@ def align_frames( alignment_frame = ( concat([df.lazy().select(on) for df in frames]) .unique(maintain_order=False) - .sort(by=on, reverse=reverse) + .sort(by=on, descending=descending) ) alignment_frame = ( alignment_frame.collect().lazy() if eager else alignment_frame.cache() diff --git a/py-polars/polars/internals/lazy_functions.py b/py-polars/polars/internals/lazy_functions.py index 738fbb24918b..f997a514d058 100644 --- a/py-polars/polars/internals/lazy_functions.py +++ b/py-polars/polars/internals/lazy_functions.py @@ -1886,10 +1886,11 @@ def arange( ) +@deprecated_alias(reverse="descending") @deprecate_nonkeyword_arguments() def arg_sort_by( exprs: pli.Expr | str | Sequence[pli.Expr | str], - reverse: Sequence[bool] | bool = False, + descending: Sequence[bool] | bool = False, ) -> pli.Expr: """ Find the indexes that would sort the columns. @@ -1902,21 +1903,22 @@ def arg_sort_by( ---------- exprs Columns use to determine the ordering. - reverse - Default is ascending. + descending + Sort descending; default is ascending. """ if isinstance(exprs, str) or not isinstance(exprs, Sequence): exprs = [exprs] - if isinstance(reverse, bool): - reverse = [reverse] * len(exprs) + if isinstance(descending, bool): + descending = [descending] * len(exprs) exprs = pli.selection_to_pyexpr_list(exprs) - return pli.wrap_expr(py_arg_sort_by(exprs, reverse)) + return pli.wrap_expr(py_arg_sort_by(exprs, descending)) +@deprecated_alias(reverse="descending") def argsort_by( exprs: pli.Expr | str | Sequence[pli.Expr | str], - reverse: Sequence[bool] | bool = False, + descending: Sequence[bool] | bool = False, ) -> pli.Expr: """ Find the indexes that would sort the columns. @@ -1932,8 +1934,8 @@ def argsort_by( ---------- exprs Columns use to determine the ordering. - reverse - Default is ascending. + descending + Sort in descending order; default is ascending. """ warnings.warn( @@ -1941,7 +1943,7 @@ def argsort_by( DeprecationWarning, stacklevel=2, ) - return pli.arg_sort_by(exprs, reverse) + return pli.arg_sort_by(exprs, descending) def duration( diff --git a/py-polars/polars/internals/lazyframe/frame.py b/py-polars/polars/internals/lazyframe/frame.py index b64f7c180303..905d5f1b91dc 100644 --- a/py-polars/polars/internals/lazyframe/frame.py +++ b/py-polars/polars/internals/lazyframe/frame.py @@ -813,11 +813,12 @@ def inspect(s: pli.DataFrame) -> pli.DataFrame: return self.map(inspect, predicate_pushdown=True, projection_pushdown=True) + @deprecated_alias(reverse="descending") def sort( self, by: IntoExpr | Iterable[IntoExpr], *more_by: IntoExpr, - reverse: bool | Sequence[bool] = False, + descending: bool | Sequence[bool] = False, nulls_last: bool = False, ) -> Self: """ @@ -830,7 +831,7 @@ def sort( names. *more_by Additional columns to sort by, specified as positional arguments. - reverse + descending Sort in descending order. When sorting by multiple columns, can be specified per column by passing a sequence of booleans. nulls_last @@ -875,7 +876,7 @@ def sort( Sort by multiple columns by passing a list of columns. - >>> ldf.sort(["c", "a"], reverse=True).collect() + >>> ldf.sort(["c", "a"], descending=True).collect() shape: (3, 3) ┌──────┬─────┬─────┐ │ a ┆ b ┆ c │ @@ -889,7 +890,7 @@ def sort( Or use positional arguments to sort by multiple columns in the same way. - >>> ldf.sort("c", "a", reverse=[False, True]).collect() + >>> ldf.sort("c", "a", descending=[False, True]).collect() shape: (3, 3) ┌──────┬─────┬─────┐ │ a ┆ b ┆ c │ @@ -904,7 +905,7 @@ def sort( """ # Fast path for sorting by a single existing column if isinstance(by, str) and not more_by: - return self._from_pyldf(self._ldf.sort(by, reverse, nulls_last)) + return self._from_pyldf(self._ldf.sort(by, descending, nulls_last)) by = pli.selection_to_pyexpr_list(by) if more_by: @@ -916,9 +917,9 @@ def sort( "`nulls_last=True` only works when sorting by a single column" ) - if isinstance(reverse, bool): - reverse = [reverse] - return self._from_pyldf(self._ldf.sort_by_exprs(by, reverse, nulls_last)) + if isinstance(descending, bool): + descending = [descending] + return self._from_pyldf(self._ldf.sort_by_exprs(by, descending, nulls_last)) def profile( self, diff --git a/py-polars/polars/internals/series/list.py b/py-polars/polars/internals/series/list.py index 7e96fd7994e1..0cad55f88eb4 100644 --- a/py-polars/polars/internals/series/list.py +++ b/py-polars/polars/internals/series/list.py @@ -51,13 +51,13 @@ def mean(self) -> pli.Series: """Compute the mean value of the arrays in the list.""" @deprecate_nonkeyword_arguments() - def sort(self, reverse: bool = False) -> pli.Series: + def sort(self, descending: bool = False) -> pli.Series: """ Sort the arrays in this column. Parameters ---------- - reverse + descending Sort in descending order. Examples @@ -70,7 +70,7 @@ def sort(self, reverse: bool = False) -> pli.Series: [1, 2, 3] [1, 2, 9] ] - >>> s.arr.sort(reverse=True) + >>> s.arr.sort(descending=True) shape: (2,) Series: 'a' [list[i64]] [ @@ -82,7 +82,7 @@ def sort(self, reverse: bool = False) -> pli.Series: return ( pli.wrap_s(self._s) .to_frame() - .select(pli.col(self._s.name()).arr.sort(reverse=reverse)) + .select(pli.col(self._s.name()).arr.sort(descending=descending)) .to_series() ) diff --git a/py-polars/polars/internals/series/series.py b/py-polars/polars/internals/series/series.py index 8bd9f2b2e7ef..e04b443bb57f 100644 --- a/py-polars/polars/internals/series/series.py +++ b/py-polars/polars/internals/series/series.py @@ -358,8 +358,8 @@ def flags(self) -> dict[str, bool]: """ out = { - "SORTED_ASC": self._s.is_sorted_flag(), - "SORTED_DESC": self._s.is_sorted_reverse_flag(), + "SORTED_ASC": self._s.is_sorted_ascending_flag(), + "SORTED_DESC": self._s.is_sorted_descending_flag(), } if self.dtype == List: out["FAST_EXPLODE"] = self._s.can_fast_explode_flag() @@ -1926,13 +1926,13 @@ def take_every(self, n: int) -> Series: """ @deprecate_nonkeyword_arguments() - def sort(self, reverse: bool = False, *, in_place: bool = False) -> Self: + def sort(self, descending: bool = False, *, in_place: bool = False) -> Self: """ Sort this Series. Parameters ---------- - reverse + descending Sort in descending order. in_place Sort in-place. @@ -1949,7 +1949,7 @@ def sort(self, reverse: bool = False, *, in_place: bool = False) -> Self: 3 4 ] - >>> s.sort(reverse=True) + >>> s.sort(descending=True) shape: (4,) Series: 'a' [i64] [ @@ -1961,16 +1961,17 @@ def sort(self, reverse: bool = False, *, in_place: bool = False) -> Self: """ if in_place: - self._s = self._s.sort(reverse) + self._s = self._s.sort(descending) return self else: - return self._from_pyseries(self._s.sort(reverse)) + return self._from_pyseries(self._s.sort(descending)) - def top_k(self, k: int = 5, reverse: bool = False) -> Series: + @deprecated_alias(reverse="descending") + def top_k(self, k: int = 5, descending: bool = False) -> Series: r""" Return the `k` largest elements. - If 'reverse=True` the smallest elements will be given. + If 'descending=True` the smallest elements will be given. This has time complexity: @@ -1980,20 +1981,27 @@ def top_k(self, k: int = 5, reverse: bool = False) -> Series: ---------- k Number of elements to return. - reverse + descending Return the smallest elements. """ + return ( + pli.wrap_s(self._s) + .to_frame() + .select(pli.col(self._s.name()).top_k(k=k, descending=descending)) + .to_series() + ) + @deprecated_alias(reverse="descending") @deprecate_nonkeyword_arguments() - def arg_sort(self, reverse: bool = False, nulls_last: bool = False) -> Series: + def arg_sort(self, descending: bool = False, nulls_last: bool = False) -> Series: """ Get the index values that would sort this Series. Parameters ---------- - reverse - Sort in reverse (descending) order. + descending + Sort in descending order. nulls_last Place null values last instead of first. @@ -2016,12 +2024,15 @@ def arg_sort(self, reverse: bool = False, nulls_last: bool = False) -> Series: pli.wrap_s(self._s) .to_frame() .select( - pli.col(self._s.name()).arg_sort(reverse=reverse, nulls_last=nulls_last) + pli.col(self._s.name()).arg_sort( + descending=descending, nulls_last=nulls_last + ) ) .to_series() ) - def argsort(self, reverse: bool = False, nulls_last: bool = False) -> Series: + @deprecated_alias(reverse="descending") + def argsort(self, descending: bool = False, nulls_last: bool = False) -> Series: """ Get the index values that would sort this Series. @@ -2032,8 +2043,8 @@ def argsort(self, reverse: bool = False, nulls_last: bool = False) -> Series: Parameters ---------- - reverse - Sort in reverse (descending) order. + descending + Sort in descending order. nulls_last Place null values last instead of first. @@ -2207,17 +2218,18 @@ def is_empty(self) -> bool: """ return self.len() == 0 - def is_sorted(self, reverse: bool = False) -> bool: + @deprecated_alias(reverse="descending") + def is_sorted(self, descending: bool = False) -> bool: """ Check if the Series is sorted. Parameters ---------- - reverse + descending Check if the Series is sorted in descending order """ - return self._s.is_sorted(reverse) + return self._s.is_sorted(descending) def is_null(self) -> Series: """ @@ -4502,7 +4514,8 @@ def abs(self) -> Series: Same as `abs(series)`. """ - def rank(self, method: RankMethod = "average", reverse: bool = False) -> Series: + @deprecated_alias(reverse="descending") + def rank(self, method: RankMethod = "average", descending: bool = False) -> Series: """ Assign ranks to data, dealing with ties appropriately. @@ -4526,8 +4539,8 @@ def rank(self, method: RankMethod = "average", reverse: bool = False) -> Series: the order that the values occur in the Series. - 'random' : Like 'ordinal', but the rank for ties is not dependent on the order that the values occur in the Series. - reverse - Reverse the operation. + descending + Rank in descending order. Examples -------- @@ -4560,6 +4573,12 @@ def rank(self, method: RankMethod = "average", reverse: bool = False) -> Series: ] """ + return ( + pli.wrap_s(self._s) + .to_frame() + .select(pli.col(self._s.name()).rank(method=method, descending=descending)) + .to_series() + ) def diff(self, n: int = 1, null_behavior: NullBehavior = "ignore") -> Series: """ @@ -5199,7 +5218,8 @@ def extend_constant(self, value: PythonLiteral | None, n: int) -> Series: """ - def set_sorted(self, reverse: bool = False) -> Series: + @deprecated_alias(reverse="descending") + def set_sorted(self, descending: bool = False) -> Series: """ Flags the Series as 'sorted'. @@ -5207,8 +5227,8 @@ def set_sorted(self, reverse: bool = False) -> Series: Parameters ---------- - reverse - If the `Series` order is reversed, e.g. descending. + descending + If the `Series` order is descending. Warnings -------- @@ -5222,7 +5242,7 @@ def set_sorted(self, reverse: bool = False) -> Series: 3 """ - return wrap_s(self._s.set_sorted_flag(reverse)) + return wrap_s(self._s.set_sorted_flag(descending)) def new_from_index(self, index: int, length: int) -> pli.Series: """Create a new Series filled with values from the given index.""" diff --git a/py-polars/src/lazy/dataframe.rs b/py-polars/src/lazy/dataframe.rs index 4f9d469790f6..825fbf4b2937 100644 --- a/py-polars/src/lazy/dataframe.rs +++ b/py-polars/src/lazy/dataframe.rs @@ -402,12 +402,12 @@ impl PyLazyFrame { ldf.into() } - pub fn sort(&self, by_column: &str, reverse: bool, nulls_last: bool) -> PyLazyFrame { + pub fn sort(&self, by_column: &str, descending: bool, nulls_last: bool) -> PyLazyFrame { let ldf = self.ldf.clone(); ldf.sort( by_column, SortOptions { - descending: reverse, + descending, nulls_last, multithreaded: true, }, @@ -418,12 +418,12 @@ impl PyLazyFrame { pub fn sort_by_exprs( &self, by: Vec, - reverse: Vec, + descending: Vec, nulls_last: bool, ) -> PyLazyFrame { let ldf = self.ldf.clone(); let exprs = py_exprs_to_exprs(by); - ldf.sort_by_exprs(exprs, reverse, nulls_last).into() + ldf.sort_by_exprs(exprs, descending, nulls_last).into() } pub fn cache(&self) -> PyLazyFrame { diff --git a/py-polars/src/lazy/dsl.rs b/py-polars/src/lazy/dsl.rs index 3fed5031aa19..b280345cb1df 100644 --- a/py-polars/src/lazy/dsl.rs +++ b/py-polars/src/lazy/dsl.rs @@ -237,11 +237,11 @@ impl PyExpr { .into() } - pub fn arg_sort(&self, reverse: bool, nulls_last: bool) -> PyExpr { + pub fn arg_sort(&self, descending: bool, nulls_last: bool) -> PyExpr { self.clone() .inner .arg_sort(SortOptions { - descending: reverse, + descending, nulls_last, multithreaded: true, }) @@ -249,8 +249,8 @@ impl PyExpr { } #[cfg(feature = "top_k")] - pub fn top_k(&self, k: usize, reverse: bool) -> PyExpr { - self.inner.clone().top_k(k, reverse).into() + pub fn top_k(&self, k: usize, descending: bool) -> PyExpr { + self.inner.clone().top_k(k, descending).into() } pub fn arg_max(&self) -> PyExpr { @@ -271,9 +271,9 @@ impl PyExpr { self.clone().inner.take(idx.inner).into() } - pub fn sort_by(&self, by: Vec, reverse: Vec) -> PyExpr { + pub fn sort_by(&self, by: Vec, descending: Vec) -> PyExpr { let by = by.into_iter().map(|e| e.inner).collect::>(); - self.clone().inner.sort_by(by, reverse).into() + self.clone().inner.sort_by(by, descending).into() } pub fn backward_fill(&self, limit: FillNullLimit) -> PyExpr { @@ -1507,12 +1507,12 @@ impl PyExpr { self.inner.clone().arr().mean().with_fmt("arr.mean").into() } - fn lst_sort(&self, reverse: bool) -> Self { + fn lst_sort(&self, descending: bool) -> Self { self.inner .clone() .arr() .sort(SortOptions { - descending: reverse, + descending, ..Default::default() }) .with_fmt("arr.sort") @@ -1618,10 +1618,10 @@ impl PyExpr { .into()) } - fn rank(&self, method: Wrap, reverse: bool) -> Self { + fn rank(&self, method: Wrap, descending: bool) -> Self { let options = RankOptions { method: method.0, - descending: reverse, + descending, }; self.inner.clone().rank(options).into() } @@ -1798,8 +1798,8 @@ impl PyExpr { pub fn hash(&self, seed: u64, seed_1: u64, seed_2: u64, seed_3: u64) -> Self { self.inner.clone().hash(seed, seed_1, seed_2, seed_3).into() } - pub fn set_sorted_flag(&self, reverse: bool) -> Self { - let is_sorted = if reverse { + pub fn set_sorted_flag(&self, descending: bool) -> Self { + let is_sorted = if descending { IsSorted::Descending } else { IsSorted::Ascending diff --git a/py-polars/src/lib.rs b/py-polars/src/lib.rs index dfc726a784e1..763669ce2e02 100644 --- a/py-polars/src/lib.rs +++ b/py-polars/src/lib.rs @@ -205,12 +205,12 @@ fn cov(a: dsl::PyExpr, b: dsl::PyExpr) -> dsl::PyExpr { } #[pyfunction] -fn arg_sort_by(by: Vec, reverse: Vec) -> dsl::PyExpr { +fn arg_sort_by(by: Vec, descending: Vec) -> dsl::PyExpr { let by = by .into_iter() .map(|e| e.inner) .collect::>(); - polars_rs::lazy::dsl::arg_sort_by(by, &reverse).into() + polars_rs::lazy::dsl::arg_sort_by(by, &descending).into() } #[pyfunction] diff --git a/py-polars/src/series.rs b/py-polars/src/series.rs index 63126cc8b7e6..40776c3887af 100644 --- a/py-polars/src/series.rs +++ b/py-polars/src/series.rs @@ -113,10 +113,10 @@ impl PySeries { #[pymethods] impl PySeries { - pub fn is_sorted_flag(&self) -> bool { + pub fn is_sorted_ascending_flag(&self) -> bool { matches!(self.series.is_sorted_flag(), IsSorted::Ascending) } - pub fn is_sorted_reverse_flag(&self) -> bool { + pub fn is_sorted_descending_flag(&self) -> bool { matches!(self.series.is_sorted_flag(), IsSorted::Descending) } pub fn can_fast_explode_flag(&self) -> bool { @@ -434,9 +434,9 @@ impl PySeries { .map(|dt| Wrap(dt.clone()).to_object(py)) } - fn set_sorted_flag(&self, reverse: bool) -> Self { + fn set_sorted_flag(&self, descending: bool) -> Self { let mut out = self.series.clone(); - if reverse { + if descending { out.set_sorted_flag(IsSorted::Descending); } else { out.set_sorted_flag(IsSorted::Ascending) @@ -544,8 +544,8 @@ impl PySeries { (&self.series % &other.series).into() } - pub fn sort(&mut self, reverse: bool) -> Self { - PySeries::new(self.series.sort(reverse)) + pub fn sort(&mut self, descending: bool) -> Self { + PySeries::new(self.series.sort(descending)) } pub fn value_counts(&self, sorted: bool) -> PyResult { @@ -1145,10 +1145,10 @@ impl PySeries { }) } - pub fn is_sorted(&self, reverse: bool) -> bool { + pub fn is_sorted(&self, descending: bool) -> bool { let options = SortOptions { - descending: reverse, - nulls_last: reverse, + descending, + nulls_last: descending, multithreaded: true, }; self.series.is_sorted(options) diff --git a/py-polars/tests/benchmark/run_h2oai_benchmark.py b/py-polars/tests/benchmark/run_h2oai_benchmark.py index f691ef9cda5e..684e84d2ff0a 100644 --- a/py-polars/tests/benchmark/run_h2oai_benchmark.py +++ b/py-polars/tests/benchmark/run_h2oai_benchmark.py @@ -261,7 +261,7 @@ print("q8") out = ( x.drop_nulls("v3") - .sort("v3", reverse=True) + .sort("v3", descending=True) .groupby("id6") .agg(pl.col("v3").head(2).alias("largest2_v3")) .explode("largest2_v3") diff --git a/py-polars/tests/unit/namespaces/test_list.py b/py-polars/tests/unit/namespaces/test_list.py index cd0dc5ddc21a..94b86b928773 100644 --- a/py-polars/tests/unit/namespaces/test_list.py +++ b/py-polars/tests/unit/namespaces/test_list.py @@ -155,7 +155,7 @@ def test_list_eval_dtype_inference() -> None: } ) - rank_pct = pl.col("").rank(reverse=True) / pl.col("").count().cast(pl.UInt16) + rank_pct = pl.col("").rank(descending=True) / pl.col("").count().cast(pl.UInt16) # the .arr.first() would fail if .arr.eval did not correctly infer the output type assert grades.with_columns( diff --git a/py-polars/tests/unit/operations/test_sort.py b/py-polars/tests/unit/operations/test_sort.py index d9b95e42625e..e6ab7dbae2fd 100644 --- a/py-polars/tests/unit/operations/test_sort.py +++ b/py-polars/tests/unit/operations/test_sort.py @@ -50,17 +50,17 @@ def test_sort_by() -> None: out = df.select(pl.col("a").sort_by("b", "c")) assert out["a"].to_list() == [3, 1, 2, 5, 4] - out = df.select(pl.col("a").sort_by(by, reverse=[False])) + out = df.select(pl.col("a").sort_by(by, descending=[False])) assert out["a"].to_list() == [3, 1, 2, 5, 4] - out = df.select(pl.col("a").sort_by(by, reverse=[True])) + out = df.select(pl.col("a").sort_by(by, descending=[True])) assert out["a"].to_list() == [4, 5, 2, 1, 3] - out = df.select(pl.col("a").sort_by(by, reverse=[True, False])) + out = df.select(pl.col("a").sort_by(by, descending=[True, False])) assert out["a"].to_list() == [5, 4, 3, 1, 2] # by can also be a single column - out = df.select(pl.col("a").sort_by("b", reverse=[False])) + out = df.select(pl.col("a").sort_by("b", descending=[False])) assert out["a"].to_list() == [1, 2, 3, 4, 5] @@ -161,16 +161,16 @@ def test_sort_aggregation_fast_paths() -> None: "f_min": [1], } - for reverse in [True, False]: + for descending in [True, False]: for null_last in [True, False]: out = df.select( [ pl.all() - .sort(reverse=reverse, nulls_last=null_last) + .sort(descending=descending, nulls_last=null_last) .max() .suffix("_max"), pl.all() - .sort(reverse=reverse, nulls_last=null_last) + .sort(descending=descending, nulls_last=null_last) .min() .suffix("_min"), ] @@ -217,15 +217,15 @@ def test_sorted_flag() -> None: def test_sorted_fast_paths() -> None: s = pl.Series([1, 2, 3]).sort() - rev = s.sort(reverse=True) + rev = s.sort(descending=True) assert rev.to_list() == [3, 2, 1] assert s.sort().to_list() == [1, 2, 3] s = pl.Series([None, 1, 2, 3]).sort() - rev = s.sort(reverse=True) + rev = s.sort(descending=True) assert rev.to_list() == [None, 3, 2, 1] - assert rev.sort(reverse=True).to_list() == [None, 3, 2, 1] + assert rev.sort(descending=True).to_list() == [None, 3, 2, 1] assert rev.sort().to_list() == [None, 1, 2, 3] @@ -250,7 +250,7 @@ def test_top_k() -> None: s = pl.Series([3, 1, 2, 5, 8]) assert s.top_k(3).to_list() == [8, 5, 3] - assert s.top_k(4, reverse=True).to_list() == [1, 2, 3, 5] + assert s.top_k(4, descending=True).to_list() == [1, 2, 3, 5] # 5886 df = pl.DataFrame({"test": [4, 3, 2, 1]}) @@ -312,8 +312,8 @@ def test_explicit_list_agg_sort_in_groupby() -> None: df = pl.DataFrame({"A": ["a", "a", "a", "b", "b", "a"], "B": [1, 2, 3, 4, 5, 6]}) # this was col().list().sort() before we changed the logic - result = df.groupby("A").agg(pl.col("B").sort(reverse=True)).sort("A") - expected = df.groupby("A").agg(pl.col("B").sort(reverse=True)).sort("A") + result = df.groupby("A").agg(pl.col("B").sort(descending=True)).sort("A") + expected = df.groupby("A").agg(pl.col("B").sort(descending=True)).sort("A") assert_frame_equal(result, expected) @@ -341,7 +341,7 @@ def test_sorted_join_query_5406() -> None: filter1 = ( df1.groupby(["Datetime", "Group"]) - .agg([pl.all().sort_by("Value", reverse=True).first()]) + .agg([pl.all().sort_by("Value", descending=True).first()]) .sort(["Datetime", "RowId"]) ) diff --git a/py-polars/tests/unit/test_df.py b/py-polars/tests/unit/test_df.py index 5e6db830688d..56a261c80fee 100644 --- a/py-polars/tests/unit/test_df.py +++ b/py-polars/tests/unit/test_df.py @@ -929,13 +929,15 @@ def test_multiple_column_sort() -> None: df = pl.DataFrame({"a": np.arange(1, 4, dtype=np.int64), "b": ["a", "a", "b"]}) assert_frame_equal( - df.sort("a", reverse=True), pl.DataFrame({"a": [3, 2, 1], "b": ["b", "a", "a"]}) + df.sort("a", descending=True), + pl.DataFrame({"a": [3, 2, 1], "b": ["b", "a", "a"]}), ) assert_frame_equal( - df.sort("b", reverse=True), pl.DataFrame({"a": [3, 1, 2], "b": ["b", "a", "a"]}) + df.sort("b", descending=True), + pl.DataFrame({"a": [3, 1, 2], "b": ["b", "a", "a"]}), ) assert_frame_equal( - df.sort(["b", "a"], reverse=[False, True]), + df.sort(["b", "a"], descending=[False, True]), pl.DataFrame({"a": [2, 1, 3], "b": ["a", "a", "b"]}), ) @@ -1051,21 +1053,21 @@ def test_to_numpy() -> None: def test_arg_sort_by(df: pl.DataFrame) -> None: idx_df = df.select( - pl.arg_sort_by(["int_nulls", "floats"], reverse=[False, True]).alias("idx") + pl.arg_sort_by(["int_nulls", "floats"], descending=[False, True]).alias("idx") ) assert (idx_df["idx"] == [1, 0, 2]).all() idx_df = df.select( - pl.arg_sort_by(["int_nulls", "floats"], reverse=False).alias("idx") + pl.arg_sort_by(["int_nulls", "floats"], descending=False).alias("idx") ) assert (idx_df["idx"] == [1, 0, 2]).all() df = pl.DataFrame({"x": [0, 0, 0, 1, 1, 2], "y": [9, 9, 8, 7, 6, 6]}) for expr, expected in ( (pl.arg_sort_by(["x", "y"]), [2, 0, 1, 4, 3, 5]), - (pl.arg_sort_by(["x", "y"], reverse=[True, True]), [5, 3, 4, 0, 1, 2]), - (pl.arg_sort_by(["x", "y"], reverse=[True, False]), [5, 4, 3, 2, 0, 1]), - (pl.arg_sort_by(["x", "y"], reverse=[False, True]), [0, 1, 2, 3, 4, 5]), + (pl.arg_sort_by(["x", "y"], descending=[True, True]), [5, 3, 4, 0, 1, 2]), + (pl.arg_sort_by(["x", "y"], descending=[True, False]), [5, 4, 3, 2, 0, 1]), + (pl.arg_sort_by(["x", "y"], descending=[False, True]), [0, 1, 2, 3, 4, 5]), ): assert (df.select(expr.alias("idx"))["idx"] == expected).all() diff --git a/py-polars/tests/unit/test_errors.py b/py-polars/tests/unit/test_errors.py index 7754e4963660..7f23751d64b0 100644 --- a/py-polars/tests/unit/test_errors.py +++ b/py-polars/tests/unit/test_errors.py @@ -285,7 +285,7 @@ def test_invalid_sort_by() -> None: pl.ComputeError, match="The sortby operation produced a different length than the Series that has to be sorted.", ): - df.select(pl.col("a").filter(pl.col("b") == "M").sort_by("c", reverse=True)) + df.select(pl.col("a").filter(pl.col("b") == "M").sort_by("c", descending=True)) def test_epoch_time_type() -> None: diff --git a/py-polars/tests/unit/test_functions.py b/py-polars/tests/unit/test_functions.py index 687cbbb5596a..bae197aa7609 100644 --- a/py-polars/tests/unit/test_functions.py +++ b/py-polars/tests/unit/test_functions.py @@ -211,12 +211,12 @@ def test_align_frames() -> None: on="date", ) - # reverse + # descending pf1, pf2 = pl.align_frames( pl.DataFrame([[3, 5, 6], [5, 8, 9]], orient="row"), pl.DataFrame([[2, 5, 6], [3, 8, 9], [4, 2, 0]], orient="row"), on="column_0", - reverse=True, + descending=True, ) assert pf1.rows() == [(5, 8, 9), (4, None, None), (3, 5, 6), (2, None, None)] assert pf2.rows() == [(5, None, None), (4, 2, 0), (3, 8, 9), (2, 5, 6)] diff --git a/py-polars/tests/unit/test_lazy.py b/py-polars/tests/unit/test_lazy.py index aa065247290e..466651bb39ba 100644 --- a/py-polars/tests/unit/test_lazy.py +++ b/py-polars/tests/unit/test_lazy.py @@ -194,7 +194,7 @@ def test_apply_custom_function() -> None: pl.count("cars").alias("cars_count"), ] ) - .sort("custom_1", reverse=True) + .sort("custom_1", descending=True) ).collect() expected = pl.DataFrame( { @@ -456,7 +456,7 @@ def test_head_groupby() -> None: # this query flexes the wildcard exclusion quite a bit. keys = ["commodity", "location"] out = ( - df.sort(by="price", reverse=True) + df.sort(by="price", descending=True) .groupby(keys, maintain_order=True) .agg([col("*").exclude(keys).head(2).keep_name()]) .explode(col("*").exclude(keys)) @@ -973,20 +973,6 @@ def test_ufunc_expr_not_first() -> None: assert_frame_equal(out, expected) -def test_ufunc_multiple_expr() -> None: - df = pl.DataFrame( - [ - pl.Series("a", [1, 2, 3], dtype=pl.Float64), - pl.Series("b", [4, 5, 6], dtype=pl.Float64), - ] - ) - - with pytest.raises( - ValueError, match="Numpy ufunc can only be used with one expression, 2 given" - ): - df.select(np.arctan2(pl.col("a"), pl.col("b"))) # type: ignore[call-overload] - - def test_clip() -> None: df = pl.DataFrame({"a": [1, 2, 3, 4, 5]}) assert df.select(pl.col("a").clip(2, 4))["a"].to_list() == [2, 2, 3, 4, 4] diff --git a/py-polars/tests/unit/test_queries.py b/py-polars/tests/unit/test_queries.py index f15237f1c96d..279418f68405 100644 --- a/py-polars/tests/unit/test_queries.py +++ b/py-polars/tests/unit/test_queries.py @@ -145,13 +145,15 @@ def test_sorted_groupby_optimization(monkeypatch: Any) -> None: # the sorted optimization should not randomize the # groups, so this is tests that we hit the sorted optimization - for reverse in [True, False]: + for descending in [True, False]: sorted_implicit = ( - df.with_columns(pl.col("a").sort(reverse=reverse)) + df.with_columns(pl.col("a").sort(descending=descending)) .groupby("a") .agg(pl.count()) ) - sorted_explicit = df.groupby("a").agg(pl.count()).sort("a", reverse=reverse) + sorted_explicit = ( + df.groupby("a").agg(pl.count()).sort("a", descending=descending) + ) assert_frame_equal(sorted_explicit, sorted_implicit) diff --git a/py-polars/tests/unit/test_series.py b/py-polars/tests/unit/test_series.py index d9af31a62260..0384e78f0964 100644 --- a/py-polars/tests/unit/test_series.py +++ b/py-polars/tests/unit/test_series.py @@ -505,7 +505,7 @@ def test_to_python() -> None: def test_sort() -> None: a = pl.Series("a", [2, 1, 3]) assert_series_equal(a.sort(), pl.Series("a", [1, 2, 3])) - assert_series_equal(a.sort(reverse=True), pl.Series("a", [3, 2, 1])) + assert_series_equal(a.sort(descending=True), pl.Series("a", [3, 2, 1])) def test_rechunk() -> None: @@ -1188,7 +1188,7 @@ def test_rank() -> None: assert df.select(pl.col("a").rank("dense"))["a"].to_list() == [2, 3, 4, 3, 3, 4, 1] assert_series_equal( - s.rank("dense", reverse=True), + s.rank("dense", descending=True), pl.Series("a", [3, 2, 1, 2, 2, 1, 4], dtype=UInt32), ) @@ -1628,8 +1628,8 @@ def test_arg_sort() -> None: assert_series_equal(s.arg_sort(), expected) - expected_reverse = pl.Series("a", [0, 2, 1, 4, 3], dtype=UInt32) - assert_series_equal(s.arg_sort(reverse=True), expected_reverse) + expected_descending = pl.Series("a", [0, 2, 1, 4, 3], dtype=UInt32) + assert_series_equal(s.arg_sort(descending=True), expected_descending) def test_arg_min_and_arg_max() -> None: @@ -1660,7 +1660,7 @@ def test_arg_min_and_arg_max() -> None: assert s.arg_min() == 0 assert s.arg_max() == 4 s = pl.Series("a", [5, 4, 3, 2, 1]) - s.sort(reverse=True, in_place=True) # set descing sorted flag + s.sort(descending=True, in_place=True) # set descing sorted flag assert s.flags == {"SORTED_ASC": False, "SORTED_DESC": True} assert s.arg_min() == 4 assert s.arg_max() == 0 diff --git a/py-polars/tests/unit/test_streaming.py b/py-polars/tests/unit/test_streaming.py index 5f54dd9a832a..b56cd5d0d838 100644 --- a/py-polars/tests/unit/test_streaming.py +++ b/py-polars/tests/unit/test_streaming.py @@ -237,12 +237,12 @@ def test_ooc_sort(monkeypatch: Any) -> None: df = s.shuffle().to_frame() - for reverse in [True, False]: + for descending in [True, False]: out = ( - df.lazy().sort("idx", reverse=reverse).collect(streaming=True) + df.lazy().sort("idx", descending=descending).collect(streaming=True) ).to_series() - assert_series_equal(out, s.sort(reverse=reverse)) + assert_series_equal(out, s.sort(descending=descending)) def test_streaming_literal_expansion() -> None: