Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
pomo-mondreganto committed Oct 14, 2024
1 parent eacf072 commit 3a9bf94
Show file tree
Hide file tree
Showing 16 changed files with 52 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-arrow/src/legacy/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ pub type LargeBinaryArray = BinaryArray<i64>;
pub type LargeListArray = ListArray<i64>;

#[allow(deprecated)]
pub use crate::legacy::kernels::rolling::no_nulls::QuantileInterpolOptions;
pub use crate::legacy::kernels::rolling::no_nulls::QuantileInterpolOptions;
110 changes: 21 additions & 89 deletions crates/polars-core/src/chunked_array/ops/aggregate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,7 @@ where
<T::Native as Simd>::Simd:
Add<Output = <T::Native as Simd>::Simd> + compute::aggregate::Sum<T::Native>,
{
fn quantile_reduce(
&self,
quantile: f64,
method: QuantileMethod,
) -> PolarsResult<Scalar> {
fn quantile_reduce(&self, quantile: f64, method: QuantileMethod) -> PolarsResult<Scalar> {
let v = self.quantile(quantile, method)?;
Ok(Scalar::new(DataType::Float64, v.into()))
}
Expand All @@ -385,11 +381,7 @@ where
}

impl QuantileAggSeries for Float32Chunked {
fn quantile_reduce(
&self,
quantile: f64,
method: QuantileMethod,
) -> PolarsResult<Scalar> {
fn quantile_reduce(&self, quantile: f64, method: QuantileMethod) -> PolarsResult<Scalar> {
let v = self.quantile(quantile, method)?;
Ok(Scalar::new(DataType::Float32, v.into()))
}
Expand All @@ -401,11 +393,7 @@ impl QuantileAggSeries for Float32Chunked {
}

impl QuantileAggSeries for Float64Chunked {
fn quantile_reduce(
&self,
quantile: f64,
method: QuantileMethod,
) -> PolarsResult<Scalar> {
fn quantile_reduce(&self, quantile: f64, method: QuantileMethod) -> PolarsResult<Scalar> {
let v = self.quantile(quantile, method)?;
Ok(Scalar::new(DataType::Float64, v.into()))
}
Expand Down Expand Up @@ -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(),
Expand All @@ -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!(
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down
39 changes: 10 additions & 29 deletions crates/polars-core/src/chunked_array/ops/aggregate/quantile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Scalar>;
fn quantile_reduce(&self, _quantile: f64, _method: QuantileMethod) -> PolarsResult<Scalar>;
}

/// helper
Expand All @@ -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;
Expand Down Expand Up @@ -154,11 +150,7 @@ where
T: PolarsIntegerType,
T::Native: TotalOrd,
{
fn quantile(
&self,
quantile: f64,
method: QuantileMethod,
) -> PolarsResult<Option<f64>> {
fn quantile(&self, quantile: f64, method: QuantileMethod) -> PolarsResult<Option<f64>> {
// 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();
Expand Down Expand Up @@ -194,17 +186,12 @@ where
}

pub(crate) fn median_faster(self) -> Option<f64> {
self.quantile_faster(0.5, QuantileMethod::Linear)
.unwrap()
self.quantile_faster(0.5, QuantileMethod::Linear).unwrap()
}
}

impl ChunkQuantile<f32> for Float32Chunked {
fn quantile(
&self,
quantile: f64,
method: QuantileMethod,
) -> PolarsResult<Option<f32>> {
fn quantile(&self, quantile: f64, method: QuantileMethod) -> PolarsResult<Option<f32>> {
// 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();
Expand All @@ -221,11 +208,7 @@ impl ChunkQuantile<f32> for Float32Chunked {
}

impl ChunkQuantile<f64> for Float64Chunked {
fn quantile(
&self,
quantile: f64,
method: QuantileMethod,
) -> PolarsResult<Option<f64>> {
fn quantile(&self, quantile: f64, method: QuantileMethod) -> PolarsResult<Option<f64>> {
// 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();
Expand Down Expand Up @@ -256,8 +239,7 @@ impl Float64Chunked {
}

pub(crate) fn median_faster(self) -> Option<f64> {
self.quantile_faster(0.5, QuantileMethod::Linear)
.unwrap()
self.quantile_faster(0.5, QuantileMethod::Linear).unwrap()
}
}

Expand All @@ -277,8 +259,7 @@ impl Float32Chunked {
}

pub(crate) fn median_faster(self) -> Option<f32> {
self.quantile_faster(0.5, QuantileMethod::Linear)
.unwrap()
self.quantile_faster(0.5, QuantileMethod::Linear).unwrap()
}
}

Expand Down
6 changes: 1 addition & 5 deletions crates/polars-core/src/chunked_array/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,7 @@ pub trait ChunkQuantile<T> {
}
/// 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<Option<T>> {
fn quantile(&self, _quantile: f64, _method: QuantileMethod) -> PolarsResult<Option<T>> {
Ok(None)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 4 additions & 17 deletions crates/polars-core/src/frame/group_by/aggregations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,7 @@ impl_take_extremum!(float: f64);
/// This trait will ensure the specific dispatch works without complicating
/// the trait bounds.
trait QuantileDispatcher<K> {
fn _quantile(self, quantile: f64, method: QuantileMethod)
-> PolarsResult<Option<K>>;
fn _quantile(self, quantile: f64, method: QuantileMethod) -> PolarsResult<Option<K>>;

fn _median(self) -> Option<K>;
}
Expand All @@ -307,11 +306,7 @@ where
T::Native: Ord,
ChunkedArray<T>: IntoSeries,
{
fn _quantile(
self,
quantile: f64,
method: QuantileMethod,
) -> PolarsResult<Option<f64>> {
fn _quantile(self, quantile: f64, method: QuantileMethod) -> PolarsResult<Option<f64>> {
self.quantile_faster(quantile, method)
}
fn _median(self) -> Option<f64> {
Expand All @@ -320,23 +315,15 @@ where
}

impl QuantileDispatcher<f32> for Float32Chunked {
fn _quantile(
self,
quantile: f64,
method: QuantileMethod,
) -> PolarsResult<Option<f32>> {
fn _quantile(self, quantile: f64, method: QuantileMethod) -> PolarsResult<Option<f32>> {
self.quantile_faster(quantile, method)
}
fn _median(self) -> Option<f32> {
self.median_faster()
}
}
impl QuantileDispatcher<f64> for Float64Chunked {
fn _quantile(
self,
quantile: f64,
method: QuantileMethod,
) -> PolarsResult<Option<f64>> {
fn _quantile(self, quantile: f64, method: QuantileMethod) -> PolarsResult<Option<f64>> {
self.quantile_faster(quantile, method)
}
fn _median(self) -> Option<f64> {
Expand Down
6 changes: 1 addition & 5 deletions crates/polars-core/src/frame/group_by/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DataFrame> {
pub fn quantile(&self, quantile: f64, method: QuantileMethod) -> PolarsResult<DataFrame> {
polars_ensure!(
(0.0..=1.0).contains(&quantile),
ComputeError: "`quantile` should be within 0.0 and 1.0"
Expand Down
6 changes: 1 addition & 5 deletions crates/polars-core/src/series/implementations/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,7 @@ impl SeriesTrait for SeriesWrap<DatetimeChunked> {
Ok(Scalar::new(self.dtype().clone(), av))
}

fn quantile_reduce(
&self,
_quantile: f64,
_method: QuantileMethod,
) -> PolarsResult<Scalar> {
fn quantile_reduce(&self, _quantile: f64, _method: QuantileMethod) -> PolarsResult<Scalar> {
Ok(Scalar::new(self.dtype().clone(), AnyValue::Null))
}

Expand Down
6 changes: 1 addition & 5 deletions crates/polars-core/src/series/implementations/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,7 @@ impl SeriesTrait for SeriesWrap<DecimalChunked> {
Ok(self.apply_scale(self.0.std_reduce(ddof)))
}

fn quantile_reduce(
&self,
quantile: f64,
method: QuantileMethod,
) -> PolarsResult<Scalar> {
fn quantile_reduce(&self, quantile: f64, method: QuantileMethod) -> PolarsResult<Scalar> {
self.0
.quantile_reduce(quantile, method)
.map(|v| self.apply_scale(v))
Expand Down
6 changes: 1 addition & 5 deletions crates/polars-core/src/series/implementations/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,7 @@ impl SeriesTrait for SeriesWrap<DurationChunked> {
v.as_duration(self.0.time_unit()),
))
}
fn quantile_reduce(
&self,
quantile: f64,
method: QuantileMethod,
) -> PolarsResult<Scalar> {
fn quantile_reduce(&self, quantile: f64, method: QuantileMethod) -> PolarsResult<Scalar> {
let v = self.0.quantile_reduce(quantile, method)?;
let to = self.dtype().to_physical();
let v = v.value().cast(&to);
Expand Down
Loading

0 comments on commit 3a9bf94

Please sign in to comment.