polars_core/chunked_array/
float.rs

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