polars_core/chunked_array/
float.rs1use arrow::legacy::kernels::set::set_at_nulls;
2use num_traits::Float;
3use polars_utils::total_ord::{canonical_f32, canonical_f64};
4
5use crate::prelude::arity::unary_elementwise_values;
6use crate::prelude::*;
7
8impl<T> ChunkedArray<T>
9where
10 T: PolarsFloatType,
11 T::Native: Float,
12{
13 pub fn is_nan(&self) -> BooleanChunked {
14 unary_elementwise_values(self, |x| x.is_nan())
15 }
16 pub fn is_not_nan(&self) -> BooleanChunked {
17 unary_elementwise_values(self, |x| !x.is_nan())
18 }
19 pub fn is_finite(&self) -> BooleanChunked {
20 unary_elementwise_values(self, |x| x.is_finite())
21 }
22 pub fn is_infinite(&self) -> BooleanChunked {
23 unary_elementwise_values(self, |x| x.is_infinite())
24 }
25
26 #[must_use]
27 pub fn none_to_nan(&self) -> Self {
29 let chunks = self
30 .downcast_iter()
31 .map(|arr| set_at_nulls(arr, T::Native::nan()));
32 ChunkedArray::from_chunk_iter(self.name().clone(), chunks)
33 }
34}
35
36pub trait Canonical {
37 fn canonical(self) -> Self;
38}
39
40impl Canonical for f32 {
41 #[inline]
42 fn canonical(self) -> Self {
43 canonical_f32(self)
44 }
45}
46
47impl Canonical for f64 {
48 #[inline]
49 fn canonical(self) -> Self {
50 canonical_f64(self)
51 }
52}
53
54impl<T> ChunkedArray<T>
55where
56 T: PolarsFloatType,
57 T::Native: Float + Canonical,
58{
59 pub fn to_canonical(&self) -> Self {
60 unary_elementwise_values(self, |v| v.canonical())
61 }
62}