Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-core/src/chunked_array/float.rs
6940 views
1
use arrow::legacy::kernels::set::set_at_nulls;
2
use num_traits::Float;
3
use polars_utils::total_ord::{canonical_f32, canonical_f64};
4
5
use crate::prelude::arity::unary_elementwise_values;
6
use crate::prelude::*;
7
8
impl<T> ChunkedArray<T>
9
where
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
/// Convert missing values to `NaN` values.
28
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
36
pub trait Canonical {
37
fn canonical(self) -> Self;
38
}
39
40
impl Canonical for f32 {
41
#[inline]
42
fn canonical(self) -> Self {
43
canonical_f32(self)
44
}
45
}
46
47
impl Canonical for f64 {
48
#[inline]
49
fn canonical(self) -> Self {
50
canonical_f64(self)
51
}
52
}
53
54
impl<T> ChunkedArray<T>
55
where
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
}
63
64