Skip to content

Commit

Permalink
fix quantile all null
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Dec 4, 2021
1 parent f5a2fd6 commit 7bf795a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
33 changes: 17 additions & 16 deletions polars/polars-core/src/chunked_array/ops/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,6 @@ pub trait VarAggSeries {
fn std_as_series(&self) -> Series;
}

macro_rules! impl_quantile {
($self:expr, $quantile:expr) => {{
let null_count = $self.null_count();
let opt = ChunkSort::sort($self, false)
.slice(
((($self.len() - null_count) as f64) * $quantile + null_count as f64) as i64,
1,
)
.into_iter()
.next()
.unwrap();
opt
}};
}

impl<T> ChunkAgg<T::Native> for ChunkedArray<T>
where
T: PolarsNumericType,
Expand Down Expand Up @@ -120,7 +105,16 @@ where
"quantile should be between 0.0 and 1.0".into(),
))
} else {
let opt = impl_quantile!(self, quantile);
let null_count = self.null_count();
let opt = ChunkSort::sort(self, false)
.slice(
(((self.len() - null_count) as f64) * quantile + null_count as f64) as i64,
1,
)
.into_iter()
.next()
.flatten();

Ok(opt)
}
}
Expand Down Expand Up @@ -603,4 +597,11 @@ mod test {
assert_eq!(ca.mean(), None);
assert_eq!(ca.mean_as_series().f64().unwrap().get(0), None);
}

#[test]
fn test_quantile_all_null() {
let ca = Float32Chunked::new_from_opt_slice("", &[None, None, None]);
let out = ca.quantile(0.9).unwrap();
assert_eq!(out, None)
}
}
12 changes: 6 additions & 6 deletions polars/polars-core/src/chunked_array/ops/unique/rank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ pub(crate) fn rank(s: &Series, method: RankMethod) -> Series {
// impute with the maximum value possible.
// todo! maybe add + 1 at the end on the null values.
if s.has_validity() {
// if s.null_count() == s.len() {
// return match method {
// Average => Float32Chunked::full_null(s.name(), s.len()).into_series(),
// _ => UInt32Chunked::full_null(s.name(), s.len()).into_series()
// }
// }
if s.null_count() == s.len() {
return match method {
Average => Float32Chunked::full_null(s.name(), s.len()).into_series(),
_ => UInt32Chunked::full_null(s.name(), s.len()).into_series(),
};
}

// replace null values with the maximum value of that dtype
let s = s.fill_null(FillNullStrategy::MaxBound).unwrap();
Expand Down

0 comments on commit 7bf795a

Please sign in to comment.