From 3a9bf949b5342f8c5d7b5822a26ddb6f29c7cf0c Mon Sep 17 00:00:00 2001 From: Roman Nikitin Date: Mon, 14 Oct 2024 14:08:45 +0300 Subject: [PATCH] Fix formatting --- .../legacy/kernels/rolling/nulls/quantile.rs | 4 +- crates/polars-arrow/src/legacy/prelude.rs | 2 +- .../src/chunked_array/ops/aggregate/mod.rs | 110 ++++-------------- .../chunked_array/ops/aggregate/quantile.rs | 39 ++----- .../polars-core/src/chunked_array/ops/mod.rs | 6 +- .../frame/group_by/aggregations/dispatch.rs | 3 +- .../src/frame/group_by/aggregations/mod.rs | 21 +--- crates/polars-core/src/frame/group_by/mod.rs | 6 +- .../src/series/implementations/datetime.rs | 6 +- .../src/series/implementations/decimal.rs | 6 +- .../src/series/implementations/duration.rs | 6 +- crates/polars-core/src/series/series_trait.rs | 6 +- crates/polars-expr/src/planner.rs | 4 +- crates/polars-ops/src/series/ops/cut.rs | 6 +- crates/polars-plan/src/plans/aexpr/mod.rs | 4 +- .../polars-python/src/series/aggregation.rs | 6 +- 16 files changed, 52 insertions(+), 183 deletions(-) diff --git a/crates/polars-arrow/src/legacy/kernels/rolling/nulls/quantile.rs b/crates/polars-arrow/src/legacy/kernels/rolling/nulls/quantile.rs index 787dc381d4c8..3d5dd664bd34 100644 --- a/crates/polars-arrow/src/legacy/kernels/rolling/nulls/quantile.rs +++ b/crates/polars-arrow/src/legacy/kernels/rolling/nulls/quantile.rs @@ -55,9 +55,7 @@ impl< let mut idx = match self.method { QuantileMethod::Nearest => ((length as f64) * self.prob) as usize, - QuantileMethod::Lower - | QuantileMethod::Midpoint - | QuantileMethod::Linear => { + QuantileMethod::Lower | QuantileMethod::Midpoint | QuantileMethod::Linear => { ((length as f64 - 1.0) * self.prob).floor() as usize }, QuantileMethod::Higher => ((length as f64 - 1.0) * self.prob).ceil() as usize, diff --git a/crates/polars-arrow/src/legacy/prelude.rs b/crates/polars-arrow/src/legacy/prelude.rs index 49c3661d115e..6afeb0c6c9be 100644 --- a/crates/polars-arrow/src/legacy/prelude.rs +++ b/crates/polars-arrow/src/legacy/prelude.rs @@ -13,4 +13,4 @@ pub type LargeBinaryArray = BinaryArray; pub type LargeListArray = ListArray; #[allow(deprecated)] -pub use crate::legacy::kernels::rolling::no_nulls::QuantileInterpolOptions; \ No newline at end of file +pub use crate::legacy::kernels::rolling::no_nulls::QuantileInterpolOptions; diff --git a/crates/polars-core/src/chunked_array/ops/aggregate/mod.rs b/crates/polars-core/src/chunked_array/ops/aggregate/mod.rs index ea2fc490f342..52927a97c9ef 100644 --- a/crates/polars-core/src/chunked_array/ops/aggregate/mod.rs +++ b/crates/polars-core/src/chunked_array/ops/aggregate/mod.rs @@ -369,11 +369,7 @@ where ::Simd: Add::Simd> + compute::aggregate::Sum, { - fn quantile_reduce( - &self, - quantile: f64, - method: QuantileMethod, - ) -> PolarsResult { + fn quantile_reduce(&self, quantile: f64, method: QuantileMethod) -> PolarsResult { let v = self.quantile(quantile, method)?; Ok(Scalar::new(DataType::Float64, v.into())) } @@ -385,11 +381,7 @@ where } impl QuantileAggSeries for Float32Chunked { - fn quantile_reduce( - &self, - quantile: f64, - method: QuantileMethod, - ) -> PolarsResult { + fn quantile_reduce(&self, quantile: f64, method: QuantileMethod) -> PolarsResult { let v = self.quantile(quantile, method)?; Ok(Scalar::new(DataType::Float32, v.into())) } @@ -401,11 +393,7 @@ impl QuantileAggSeries for Float32Chunked { } impl QuantileAggSeries for Float64Chunked { - fn quantile_reduce( - &self, - quantile: f64, - method: QuantileMethod, - ) -> PolarsResult { + fn quantile_reduce(&self, quantile: f64, method: QuantileMethod) -> PolarsResult { let v = self.quantile(quantile, method)?; Ok(Scalar::new(DataType::Float64, v.into())) } @@ -852,31 +840,13 @@ mod test { Some(3.0) ); - assert_eq!( - ca.quantile(0.1, QuantileMethod::Lower).unwrap(), - Some(1.0) - ); - assert_eq!( - ca.quantile(0.9, QuantileMethod::Lower).unwrap(), - Some(4.0) - ); - assert_eq!( - ca.quantile(0.6, QuantileMethod::Lower).unwrap(), - Some(3.0) - ); + assert_eq!(ca.quantile(0.1, QuantileMethod::Lower).unwrap(), Some(1.0)); + assert_eq!(ca.quantile(0.9, QuantileMethod::Lower).unwrap(), Some(4.0)); + assert_eq!(ca.quantile(0.6, QuantileMethod::Lower).unwrap(), Some(3.0)); - assert_eq!( - ca.quantile(0.1, QuantileMethod::Higher).unwrap(), - Some(2.0) - ); - assert_eq!( - ca.quantile(0.9, QuantileMethod::Higher).unwrap(), - Some(5.0) - ); - assert_eq!( - ca.quantile(0.6, QuantileMethod::Higher).unwrap(), - Some(4.0) - ); + assert_eq!(ca.quantile(0.1, QuantileMethod::Higher).unwrap(), Some(2.0)); + assert_eq!(ca.quantile(0.9, QuantileMethod::Higher).unwrap(), Some(5.0)); + assert_eq!(ca.quantile(0.6, QuantileMethod::Higher).unwrap(), Some(4.0)); assert_eq!( ca.quantile(0.1, QuantileMethod::Midpoint).unwrap(), @@ -891,21 +861,10 @@ mod test { Some(3.5) ); - assert_eq!( - ca.quantile(0.1, QuantileMethod::Linear).unwrap(), - Some(1.4) - ); - assert_eq!( - ca.quantile(0.9, QuantileMethod::Linear).unwrap(), - Some(4.6) - ); + assert_eq!(ca.quantile(0.1, QuantileMethod::Linear).unwrap(), Some(1.4)); + assert_eq!(ca.quantile(0.9, QuantileMethod::Linear).unwrap(), Some(4.6)); assert!( - (ca.quantile(0.6, QuantileMethod::Linear) - .unwrap() - .unwrap() - - 3.4) - .abs() - < 0.0000001 + (ca.quantile(0.6, QuantileMethod::Linear).unwrap().unwrap() - 3.4).abs() < 0.0000001 ); assert_eq!( @@ -950,31 +909,13 @@ mod test { Some(5.0) ); - assert_eq!( - ca.quantile(0.1, QuantileMethod::Lower).unwrap(), - Some(1.0) - ); - assert_eq!( - ca.quantile(0.9, QuantileMethod::Lower).unwrap(), - Some(6.0) - ); - assert_eq!( - ca.quantile(0.6, QuantileMethod::Lower).unwrap(), - Some(4.0) - ); + assert_eq!(ca.quantile(0.1, QuantileMethod::Lower).unwrap(), Some(1.0)); + assert_eq!(ca.quantile(0.9, QuantileMethod::Lower).unwrap(), Some(6.0)); + assert_eq!(ca.quantile(0.6, QuantileMethod::Lower).unwrap(), Some(4.0)); - assert_eq!( - ca.quantile(0.1, QuantileMethod::Higher).unwrap(), - Some(2.0) - ); - assert_eq!( - ca.quantile(0.9, QuantileMethod::Higher).unwrap(), - Some(7.0) - ); - assert_eq!( - ca.quantile(0.6, QuantileMethod::Higher).unwrap(), - Some(5.0) - ); + assert_eq!(ca.quantile(0.1, QuantileMethod::Higher).unwrap(), Some(2.0)); + assert_eq!(ca.quantile(0.9, QuantileMethod::Higher).unwrap(), Some(7.0)); + assert_eq!(ca.quantile(0.6, QuantileMethod::Higher).unwrap(), Some(5.0)); assert_eq!( ca.quantile(0.1, QuantileMethod::Midpoint).unwrap(), @@ -989,18 +930,9 @@ mod test { Some(4.5) ); - assert_eq!( - ca.quantile(0.1, QuantileMethod::Linear).unwrap(), - Some(1.6) - ); - assert_eq!( - ca.quantile(0.9, QuantileMethod::Linear).unwrap(), - Some(6.4) - ); - assert_eq!( - ca.quantile(0.6, QuantileMethod::Linear).unwrap(), - Some(4.6) - ); + assert_eq!(ca.quantile(0.1, QuantileMethod::Linear).unwrap(), Some(1.6)); + assert_eq!(ca.quantile(0.9, QuantileMethod::Linear).unwrap(), Some(6.4)); + assert_eq!(ca.quantile(0.6, QuantileMethod::Linear).unwrap(), Some(4.6)); assert_eq!( ca.quantile(0.14, QuantileMethod::Equiprobable).unwrap(), diff --git a/crates/polars-core/src/chunked_array/ops/aggregate/quantile.rs b/crates/polars-core/src/chunked_array/ops/aggregate/quantile.rs index c642ec1f5175..f7716c864559 100644 --- a/crates/polars-core/src/chunked_array/ops/aggregate/quantile.rs +++ b/crates/polars-core/src/chunked_array/ops/aggregate/quantile.rs @@ -4,11 +4,7 @@ pub trait QuantileAggSeries { /// Get the median of the [`ChunkedArray`] as a new [`Series`] of length 1. fn median_reduce(&self) -> Scalar; /// Get the quantile of the [`ChunkedArray`] as a new [`Series`] of length 1. - fn quantile_reduce( - &self, - _quantile: f64, - _method: QuantileMethod, - ) -> PolarsResult; + fn quantile_reduce(&self, _quantile: f64, _method: QuantileMethod) -> PolarsResult; } /// helper @@ -25,9 +21,9 @@ fn quantile_idx( let idx = float_idx.round() as usize; return (idx, 0.0, idx); }, - QuantileMethod::Lower - | QuantileMethod::Midpoint - | QuantileMethod::Linear => float_idx as usize, + QuantileMethod::Lower | QuantileMethod::Midpoint | QuantileMethod::Linear => { + float_idx as usize + }, QuantileMethod::Higher => float_idx.ceil() as usize, QuantileMethod::Equiprobable => { let idx = ((nonnull_count * quantile).ceil() - 1.0).max(0.0) as usize + null_count; @@ -154,11 +150,7 @@ where T: PolarsIntegerType, T::Native: TotalOrd, { - fn quantile( - &self, - quantile: f64, - method: QuantileMethod, - ) -> PolarsResult> { + fn quantile(&self, quantile: f64, method: QuantileMethod) -> 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_ascending_flag()) { let mut owned = slice.to_vec(); @@ -194,17 +186,12 @@ where } pub(crate) fn median_faster(self) -> Option { - self.quantile_faster(0.5, QuantileMethod::Linear) - .unwrap() + self.quantile_faster(0.5, QuantileMethod::Linear).unwrap() } } impl ChunkQuantile for Float32Chunked { - fn quantile( - &self, - quantile: f64, - method: QuantileMethod, - ) -> PolarsResult> { + fn quantile(&self, quantile: f64, method: QuantileMethod) -> 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_ascending_flag()) { let mut owned = slice.to_vec(); @@ -221,11 +208,7 @@ impl ChunkQuantile for Float32Chunked { } impl ChunkQuantile for Float64Chunked { - fn quantile( - &self, - quantile: f64, - method: QuantileMethod, - ) -> PolarsResult> { + fn quantile(&self, quantile: f64, method: QuantileMethod) -> 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_ascending_flag()) { let mut owned = slice.to_vec(); @@ -256,8 +239,7 @@ impl Float64Chunked { } pub(crate) fn median_faster(self) -> Option { - self.quantile_faster(0.5, QuantileMethod::Linear) - .unwrap() + self.quantile_faster(0.5, QuantileMethod::Linear).unwrap() } } @@ -277,8 +259,7 @@ impl Float32Chunked { } pub(crate) fn median_faster(self) -> Option { - self.quantile_faster(0.5, QuantileMethod::Linear) - .unwrap() + self.quantile_faster(0.5, QuantileMethod::Linear).unwrap() } } diff --git a/crates/polars-core/src/chunked_array/ops/mod.rs b/crates/polars-core/src/chunked_array/ops/mod.rs index a32ebd34e7fe..d70e7835f25d 100644 --- a/crates/polars-core/src/chunked_array/ops/mod.rs +++ b/crates/polars-core/src/chunked_array/ops/mod.rs @@ -277,11 +277,7 @@ pub trait ChunkQuantile { } /// Aggregate a given quantile of the ChunkedArray. /// Returns `None` if the array is empty or only contains null values. - fn quantile( - &self, - _quantile: f64, - _method: QuantileMethod, - ) -> PolarsResult> { + fn quantile(&self, _quantile: f64, _method: QuantileMethod) -> PolarsResult> { Ok(None) } } diff --git a/crates/polars-core/src/frame/group_by/aggregations/dispatch.rs b/crates/polars-core/src/frame/group_by/aggregations/dispatch.rs index 1edd5ec5449a..aaf24a470969 100644 --- a/crates/polars-core/src/frame/group_by/aggregations/dispatch.rs +++ b/crates/polars-core/src/frame/group_by/aggregations/dispatch.rs @@ -252,8 +252,7 @@ impl Series { dt if dt.is_numeric() || dt.is_temporal() => { let ca = s.to_physical_repr(); let physical_type = ca.dtype(); - let s = - apply_method_physical_integer!(ca, agg_quantile, groups, quantile, method); + let s = apply_method_physical_integer!(ca, agg_quantile, groups, quantile, method); if dt.is_logical() { // back to physical and then // back to logical type diff --git a/crates/polars-core/src/frame/group_by/aggregations/mod.rs b/crates/polars-core/src/frame/group_by/aggregations/mod.rs index 4c389df56bbc..19b8d5c2d061 100644 --- a/crates/polars-core/src/frame/group_by/aggregations/mod.rs +++ b/crates/polars-core/src/frame/group_by/aggregations/mod.rs @@ -295,8 +295,7 @@ impl_take_extremum!(float: f64); /// This trait will ensure the specific dispatch works without complicating /// the trait bounds. trait QuantileDispatcher { - fn _quantile(self, quantile: f64, method: QuantileMethod) - -> PolarsResult>; + fn _quantile(self, quantile: f64, method: QuantileMethod) -> PolarsResult>; fn _median(self) -> Option; } @@ -307,11 +306,7 @@ where T::Native: Ord, ChunkedArray: IntoSeries, { - fn _quantile( - self, - quantile: f64, - method: QuantileMethod, - ) -> PolarsResult> { + fn _quantile(self, quantile: f64, method: QuantileMethod) -> PolarsResult> { self.quantile_faster(quantile, method) } fn _median(self) -> Option { @@ -320,11 +315,7 @@ where } impl QuantileDispatcher for Float32Chunked { - fn _quantile( - self, - quantile: f64, - method: QuantileMethod, - ) -> PolarsResult> { + fn _quantile(self, quantile: f64, method: QuantileMethod) -> PolarsResult> { self.quantile_faster(quantile, method) } fn _median(self) -> Option { @@ -332,11 +323,7 @@ impl QuantileDispatcher for Float32Chunked { } } impl QuantileDispatcher for Float64Chunked { - fn _quantile( - self, - quantile: f64, - method: QuantileMethod, - ) -> PolarsResult> { + fn _quantile(self, quantile: f64, method: QuantileMethod) -> PolarsResult> { self.quantile_faster(quantile, method) } fn _median(self) -> Option { diff --git a/crates/polars-core/src/frame/group_by/mod.rs b/crates/polars-core/src/frame/group_by/mod.rs index f4ce303cae92..d8a2fbbd20c9 100644 --- a/crates/polars-core/src/frame/group_by/mod.rs +++ b/crates/polars-core/src/frame/group_by/mod.rs @@ -601,11 +601,7 @@ impl<'df> GroupBy<'df> { /// } /// ``` #[deprecated(since = "0.24.1", note = "use polars.lazy aggregations")] - pub fn quantile( - &self, - quantile: f64, - method: QuantileMethod, - ) -> PolarsResult { + pub fn quantile(&self, quantile: f64, method: QuantileMethod) -> PolarsResult { polars_ensure!( (0.0..=1.0).contains(&quantile), ComputeError: "`quantile` should be within 0.0 and 1.0" diff --git a/crates/polars-core/src/series/implementations/datetime.rs b/crates/polars-core/src/series/implementations/datetime.rs index bbbcb4a20c38..ace52993b8a1 100644 --- a/crates/polars-core/src/series/implementations/datetime.rs +++ b/crates/polars-core/src/series/implementations/datetime.rs @@ -358,11 +358,7 @@ impl SeriesTrait for SeriesWrap { Ok(Scalar::new(self.dtype().clone(), av)) } - fn quantile_reduce( - &self, - _quantile: f64, - _method: QuantileMethod, - ) -> PolarsResult { + fn quantile_reduce(&self, _quantile: f64, _method: QuantileMethod) -> PolarsResult { Ok(Scalar::new(self.dtype().clone(), AnyValue::Null)) } diff --git a/crates/polars-core/src/series/implementations/decimal.rs b/crates/polars-core/src/series/implementations/decimal.rs index 6e8dca78fb31..612505057eca 100644 --- a/crates/polars-core/src/series/implementations/decimal.rs +++ b/crates/polars-core/src/series/implementations/decimal.rs @@ -404,11 +404,7 @@ impl SeriesTrait for SeriesWrap { Ok(self.apply_scale(self.0.std_reduce(ddof))) } - fn quantile_reduce( - &self, - quantile: f64, - method: QuantileMethod, - ) -> PolarsResult { + fn quantile_reduce(&self, quantile: f64, method: QuantileMethod) -> PolarsResult { self.0 .quantile_reduce(quantile, method) .map(|v| self.apply_scale(v)) diff --git a/crates/polars-core/src/series/implementations/duration.rs b/crates/polars-core/src/series/implementations/duration.rs index 9344a73df4f9..803ca813aa1c 100644 --- a/crates/polars-core/src/series/implementations/duration.rs +++ b/crates/polars-core/src/series/implementations/duration.rs @@ -501,11 +501,7 @@ impl SeriesTrait for SeriesWrap { v.as_duration(self.0.time_unit()), )) } - fn quantile_reduce( - &self, - quantile: f64, - method: QuantileMethod, - ) -> PolarsResult { + fn quantile_reduce(&self, quantile: f64, method: QuantileMethod) -> PolarsResult { let v = self.0.quantile_reduce(quantile, method)?; let to = self.dtype().to_physical(); let v = v.value().cast(&to); diff --git a/crates/polars-core/src/series/series_trait.rs b/crates/polars-core/src/series/series_trait.rs index 1513b99ff935..53e6f8d334b6 100644 --- a/crates/polars-core/src/series/series_trait.rs +++ b/crates/polars-core/src/series/series_trait.rs @@ -498,11 +498,7 @@ pub trait SeriesTrait: polars_bail!(opq = std, self._dtype()); } /// Get the quantile of the ChunkedArray as a new Series of length 1. - fn quantile_reduce( - &self, - _quantile: f64, - _method: QuantileMethod, - ) -> PolarsResult { + fn quantile_reduce(&self, _quantile: f64, _method: QuantileMethod) -> PolarsResult { polars_bail!(opq = quantile, self._dtype()); } /// Get the bitwise AND of the Series as a new Series of length 1, diff --git a/crates/polars-expr/src/planner.rs b/crates/polars-expr/src/planner.rs index 7d5d2144bc33..f43e0f42a302 100644 --- a/crates/polars-expr/src/planner.rs +++ b/crates/polars-expr/src/planner.rs @@ -402,7 +402,9 @@ fn create_physical_expr_inner( }, _ => { if let IRAggExpr::Quantile { - quantile, method: interpol, .. + quantile, + method: interpol, + .. } = agg { let quantile = diff --git a/crates/polars-ops/src/series/ops/cut.rs b/crates/polars-ops/src/series/ops/cut.rs index 52648483e4d3..c4ff09f8ec04 100644 --- a/crates/polars-ops/src/series/ops/cut.rs +++ b/crates/polars-ops/src/series/ops/cut.rs @@ -144,11 +144,7 @@ pub fn qcut( let s2 = s.sort(SortOptions::default())?; let ca = s2.f64()?; - let f = |&p| { - ca.quantile(p, QuantileMethod::Linear) - .unwrap() - .unwrap() - }; + let f = |&p| ca.quantile(p, QuantileMethod::Linear).unwrap().unwrap(); let mut qbreaks: Vec<_> = probs.iter().map(f).collect(); qbreaks.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap()); diff --git a/crates/polars-plan/src/plans/aexpr/mod.rs b/crates/polars-plan/src/plans/aexpr/mod.rs index ded9f0c996d1..286ea86ac968 100644 --- a/crates/polars-plan/src/plans/aexpr/mod.rs +++ b/crates/polars-plan/src/plans/aexpr/mod.rs @@ -62,7 +62,9 @@ impl Hash for IRAggExpr { Self::Min { propagate_nans, .. } | Self::Max { propagate_nans, .. } => { propagate_nans.hash(state) }, - Self::Quantile { method: interpol, .. } => interpol.hash(state), + Self::Quantile { + method: interpol, .. + } => interpol.hash(state), Self::Std(_, v) | Self::Var(_, v) => v.hash(state), #[cfg(feature = "bitwise")] Self::Bitwise(_, f) => f.hash(state), diff --git a/crates/polars-python/src/series/aggregation.rs b/crates/polars-python/src/series/aggregation.rs index 28fde7fd0d93..5aa8ee16639e 100644 --- a/crates/polars-python/src/series/aggregation.rs +++ b/crates/polars-python/src/series/aggregation.rs @@ -105,11 +105,7 @@ impl PySeries { .into_py(py)) } - fn quantile( - &self, - quantile: f64, - interpolation: Wrap, - ) -> PyResult { + fn quantile(&self, quantile: f64, interpolation: Wrap) -> PyResult { let bind = self.series.quantile_reduce(quantile, interpolation.0); let sc = bind.map_err(PyPolarsErr::from)?;