Path: blob/main/crates/polars-core/src/chunked_array/float.rs
8362 views
use arrow::bitmap::Bitmap;1use arrow::legacy::kernels::set::set_at_nulls;2use num_traits::Float;3use polars_compute::nan::{is_nan, is_not_nan};4use polars_utils::float16::pf16;5use polars_utils::total_ord::{canonical_f16, canonical_f32, canonical_f64};67use crate::prelude::arity::{unary_elementwise_values, unary_kernel};8use crate::prelude::*;910impl<T> ChunkedArray<T>11where12T: PolarsFloatType,13T::Native: Float,14{15pub fn is_nan(&self) -> BooleanChunked {16unary_kernel(self, |arr| {17let out = is_nan(arr.values()).unwrap_or_else(|| Bitmap::new_zeroed(arr.len()));18BooleanArray::from(out).with_validity(arr.validity().cloned())19})20}21pub fn is_not_nan(&self) -> BooleanChunked {22unary_kernel(self, |arr| {23let out =24is_not_nan(arr.values()).unwrap_or_else(|| Bitmap::new_with_value(true, arr.len()));25BooleanArray::from(out).with_validity(arr.validity().cloned())26})27}28pub fn is_finite(&self) -> BooleanChunked {29unary_elementwise_values(self, |x| x.is_finite())30}31pub fn is_infinite(&self) -> BooleanChunked {32unary_elementwise_values(self, |x| x.is_infinite())33}3435#[must_use]36/// Convert missing values to `NaN` values.37pub fn none_to_nan(&self) -> Self {38let chunks = self39.downcast_iter()40.map(|arr| set_at_nulls(arr, T::Native::nan()));41ChunkedArray::from_chunk_iter(self.name().clone(), chunks)42}43}4445pub trait Canonical {46fn canonical(self) -> Self;47}4849impl Canonical for pf16 {50#[inline]51fn canonical(self) -> Self {52canonical_f16(self)53}54}5556impl Canonical for f32 {57#[inline]58fn canonical(self) -> Self {59canonical_f32(self)60}61}6263impl Canonical for f64 {64#[inline]65fn canonical(self) -> Self {66canonical_f64(self)67}68}6970impl<T> ChunkedArray<T>71where72T: PolarsFloatType,73T::Native: Float + Canonical,74{75pub fn to_canonical(&self) -> Self {76unary_elementwise_values(self, |v| v.canonical())77}78}798081