Path: blob/main/crates/polars-core/src/series/implementations/floats.rs
8327 views
use num_traits::AsPrimitive;1use polars_compute::rolling::QuantileMethod;23use super::*;4use crate::chunked_array::comparison::*;5#[cfg(feature = "algorithm_group_by")]6use crate::frame::group_by::*;7use crate::prelude::*;89macro_rules! impl_dyn_series {10($ca: ident, $pdt:ident) => {11impl private::PrivateSeries for SeriesWrap<$ca> {12fn compute_len(&mut self) {13self.0.compute_len()14}15fn _field(&self) -> Cow<'_, Field> {16Cow::Borrowed(self.0.ref_field())17}18fn _dtype(&self) -> &DataType {19self.0.ref_field().dtype()20}2122fn _set_flags(&mut self, flags: StatisticsFlags) {23self.0.set_flags(flags)24}25fn _get_flags(&self) -> StatisticsFlags {26self.0.get_flags()27}28unsafe fn equal_element(29&self,30idx_self: usize,31idx_other: usize,32other: &Series,33) -> bool {34self.0.equal_element(idx_self, idx_other, other)35}3637#[cfg(feature = "zip_with")]38fn zip_with_same_type(39&self,40mask: &BooleanChunked,41other: &Series,42) -> PolarsResult<Series> {43ChunkZip::zip_with(&self.0, mask, other.as_ref().as_ref())44.map(|ca| ca.into_series())45}46fn into_total_eq_inner<'a>(&'a self) -> Box<dyn TotalEqInner + 'a> {47(&self.0).into_total_eq_inner()48}49fn into_total_ord_inner<'a>(&'a self) -> Box<dyn TotalOrdInner + 'a> {50(&self.0).into_total_ord_inner()51}5253fn vec_hash(54&self,55random_state: PlSeedableRandomStateQuality,56buf: &mut Vec<u64>,57) -> PolarsResult<()> {58self.0.vec_hash(random_state, buf)?;59Ok(())60}6162fn vec_hash_combine(63&self,64build_hasher: PlSeedableRandomStateQuality,65hashes: &mut [u64],66) -> PolarsResult<()> {67self.0.vec_hash_combine(build_hasher, hashes)?;68Ok(())69}7071#[cfg(feature = "algorithm_group_by")]72unsafe fn agg_min(&self, groups: &GroupsType) -> Series {73self.0.agg_min(groups)74}7576#[cfg(feature = "algorithm_group_by")]77unsafe fn agg_max(&self, groups: &GroupsType) -> Series {78self.0.agg_max(groups)79}8081#[cfg(feature = "algorithm_group_by")]82unsafe fn agg_arg_min(&self, groups: &GroupsType) -> Series {83self.0.agg_arg_min(groups)84}8586#[cfg(feature = "algorithm_group_by")]87unsafe fn agg_arg_max(&self, groups: &GroupsType) -> Series {88self.0.agg_arg_max(groups)89}9091#[cfg(feature = "algorithm_group_by")]92unsafe fn agg_sum(&self, groups: &GroupsType) -> Series {93self.0.agg_sum(groups)94}9596#[cfg(feature = "algorithm_group_by")]97unsafe fn agg_std(&self, groups: &GroupsType, ddof: u8) -> Series {98SeriesWrap::agg_std(self, groups, ddof)99}100101#[cfg(feature = "algorithm_group_by")]102unsafe fn agg_var(&self, groups: &GroupsType, ddof: u8) -> Series {103SeriesWrap::agg_var(self, groups, ddof)104}105106#[cfg(feature = "algorithm_group_by")]107unsafe fn agg_list(&self, groups: &GroupsType) -> Series {108self.0.agg_list(groups)109}110111#[cfg(feature = "bitwise")]112unsafe fn agg_and(&self, groups: &GroupsType) -> Series {113self.0.agg_and(groups)114}115#[cfg(feature = "bitwise")]116unsafe fn agg_or(&self, groups: &GroupsType) -> Series {117self.0.agg_or(groups)118}119#[cfg(feature = "bitwise")]120unsafe fn agg_xor(&self, groups: &GroupsType) -> Series {121self.0.agg_xor(groups)122}123124fn subtract(&self, rhs: &Series) -> PolarsResult<Series> {125NumOpsDispatch::subtract(&self.0, rhs)126}127fn add_to(&self, rhs: &Series) -> PolarsResult<Series> {128NumOpsDispatch::add_to(&self.0, rhs)129}130fn multiply(&self, rhs: &Series) -> PolarsResult<Series> {131NumOpsDispatch::multiply(&self.0, rhs)132}133fn divide(&self, rhs: &Series) -> PolarsResult<Series> {134NumOpsDispatch::divide(&self.0, rhs)135}136fn remainder(&self, rhs: &Series) -> PolarsResult<Series> {137NumOpsDispatch::remainder(&self.0, rhs)138}139#[cfg(feature = "algorithm_group_by")]140fn group_tuples(&self, multithreaded: bool, sorted: bool) -> PolarsResult<GroupsType> {141IntoGroupsType::group_tuples(&self.0, multithreaded, sorted)142}143144fn arg_sort_multiple(145&self,146by: &[Column],147options: &SortMultipleOptions,148) -> PolarsResult<IdxCa> {149self.0.arg_sort_multiple(by, options)150}151}152153impl SeriesTrait for SeriesWrap<$ca> {154#[cfg(feature = "rolling_window")]155fn rolling_map(156&self,157_f: &dyn Fn(&Series) -> PolarsResult<Series>,158_options: RollingOptionsFixedWindow,159) -> PolarsResult<Series> {160ChunkRollApply::rolling_map(&self.0, _f, _options).map(|ca| ca.into_series())161}162163fn rename(&mut self, name: PlSmallStr) {164self.0.rename(name);165}166167fn chunk_lengths(&self) -> ChunkLenIter<'_> {168self.0.chunk_lengths()169}170fn name(&self) -> &PlSmallStr {171self.0.name()172}173174fn chunks(&self) -> &Vec<ArrayRef> {175self.0.chunks()176}177unsafe fn chunks_mut(&mut self) -> &mut Vec<ArrayRef> {178self.0.chunks_mut()179}180fn shrink_to_fit(&mut self) {181self.0.shrink_to_fit()182}183184fn slice(&self, offset: i64, length: usize) -> Series {185return self.0.slice(offset, length).into_series();186}187188fn split_at(&self, offset: i64) -> (Series, Series) {189let (a, b) = self.0.split_at(offset);190(a.into_series(), b.into_series())191}192193fn append(&mut self, other: &Series) -> PolarsResult<()> {194polars_ensure!(self.0.dtype() == other.dtype(), append);195self.0.append(other.as_ref().as_ref())?;196Ok(())197}198fn append_owned(&mut self, other: Series) -> PolarsResult<()> {199polars_ensure!(self.0.dtype() == other.dtype(), append);200self.0.append_owned(other.take_inner())201}202203fn extend(&mut self, other: &Series) -> PolarsResult<()> {204polars_ensure!(self.0.dtype() == other.dtype(), extend);205self.0.extend(other.as_ref().as_ref())?;206Ok(())207}208209fn filter(&self, filter: &BooleanChunked) -> PolarsResult<Series> {210ChunkFilter::filter(&self.0, filter).map(|ca| ca.into_series())211}212213fn _sum_as_f64(&self) -> f64 {214self.0._sum_as_f64()215}216217fn mean(&self) -> Option<f64> {218self.0.mean()219}220221fn median(&self) -> Option<f64> {222self.0.median().map(|v| v.as_())223}224225fn std(&self, ddof: u8) -> Option<f64> {226self.0.std(ddof)227}228229fn var(&self, ddof: u8) -> Option<f64> {230self.0.var(ddof)231}232233fn take(&self, indices: &IdxCa) -> PolarsResult<Series> {234Ok(self.0.take(indices)?.into_series())235}236237unsafe fn take_unchecked(&self, indices: &IdxCa) -> Series {238self.0.take_unchecked(indices).into_series()239}240241fn take_slice(&self, indices: &[IdxSize]) -> PolarsResult<Series> {242Ok(self.0.take(indices)?.into_series())243}244245unsafe fn take_slice_unchecked(&self, indices: &[IdxSize]) -> Series {246self.0.take_unchecked(indices).into_series()247}248249fn deposit(&self, validity: &Bitmap) -> Series {250self.0.deposit(validity).into_series()251}252253fn len(&self) -> usize {254self.0.len()255}256257fn rechunk(&self) -> Series {258self.0.rechunk().into_owned().into_series()259}260261fn new_from_index(&self, index: usize, length: usize) -> Series {262ChunkExpandAtIndex::new_from_index(&self.0, index, length).into_series()263}264265fn cast(&self, dtype: &DataType, cast_options: CastOptions) -> PolarsResult<Series> {266self.0.cast_with_options(dtype, cast_options)267}268269#[inline]270unsafe fn get_unchecked(&self, index: usize) -> AnyValue<'_> {271self.0.get_any_value_unchecked(index)272}273274fn sort_with(&self, options: SortOptions) -> PolarsResult<Series> {275Ok(ChunkSort::sort_with(&self.0, options).into_series())276}277278fn arg_sort(&self, options: SortOptions) -> IdxCa {279ChunkSort::arg_sort(&self.0, options)280}281282fn null_count(&self) -> usize {283self.0.null_count()284}285286fn has_nulls(&self) -> bool {287self.0.has_nulls()288}289290#[cfg(feature = "algorithm_group_by")]291fn unique(&self) -> PolarsResult<Series> {292ChunkUnique::unique(&self.0).map(|ca| ca.into_series())293}294295#[cfg(feature = "algorithm_group_by")]296fn n_unique(&self) -> PolarsResult<usize> {297ChunkUnique::n_unique(&self.0)298}299300#[cfg(feature = "algorithm_group_by")]301fn arg_unique(&self) -> PolarsResult<IdxCa> {302ChunkUnique::arg_unique(&self.0)303}304305fn unique_id(&self) -> PolarsResult<(IdxSize, Vec<IdxSize>)> {306ChunkUnique::unique_id(&self.0)307}308309fn is_null(&self) -> BooleanChunked {310self.0.is_null()311}312313fn is_not_null(&self) -> BooleanChunked {314self.0.is_not_null()315}316317fn reverse(&self) -> Series {318ChunkReverse::reverse(&self.0).into_series()319}320321fn as_single_ptr(&mut self) -> PolarsResult<usize> {322self.0.as_single_ptr()323}324325fn shift(&self, periods: i64) -> Series {326ChunkShift::shift(&self.0, periods).into_series()327}328329fn sum_reduce(&self) -> PolarsResult<Scalar> {330Ok(ChunkAggSeries::sum_reduce(&self.0))331}332fn max_reduce(&self) -> PolarsResult<Scalar> {333Ok(ChunkAggSeries::max_reduce(&self.0))334}335fn min_reduce(&self) -> PolarsResult<Scalar> {336Ok(ChunkAggSeries::min_reduce(&self.0))337}338fn mean_reduce(&self) -> PolarsResult<Scalar> {339let mean = self340.mean()341.map(AsPrimitive::<<$pdt as PolarsDataType>::OwnedPhysical>::as_);342Ok(Scalar::new(self.dtype().clone(), mean.into()))343}344fn median_reduce(&self) -> PolarsResult<Scalar> {345Ok(QuantileAggSeries::median_reduce(&self.0))346}347fn var_reduce(&self, ddof: u8) -> PolarsResult<Scalar> {348Ok(VarAggSeries::var_reduce(&self.0, ddof))349}350fn std_reduce(&self, ddof: u8) -> PolarsResult<Scalar> {351Ok(VarAggSeries::std_reduce(&self.0, ddof))352}353354fn quantile_reduce(355&self,356quantile: f64,357method: QuantileMethod,358) -> PolarsResult<Scalar> {359QuantileAggSeries::quantile_reduce(&self.0, quantile, method)360}361362fn quantiles_reduce(363&self,364quantiles: &[f64],365method: QuantileMethod,366) -> PolarsResult<Scalar> {367QuantileAggSeries::quantiles_reduce(&self.0, quantiles, method)368}369370#[cfg(feature = "bitwise")]371fn and_reduce(&self) -> PolarsResult<Scalar> {372let dt = <$pdt as PolarsDataType>::get_static_dtype();373let av = self.0.and_reduce().map_or(AnyValue::Null, Into::into);374375Ok(Scalar::new(dt, av))376}377#[cfg(feature = "bitwise")]378fn or_reduce(&self) -> PolarsResult<Scalar> {379let dt = <$pdt as PolarsDataType>::get_static_dtype();380let av = self.0.or_reduce().map_or(AnyValue::Null, Into::into);381382Ok(Scalar::new(dt, av))383}384#[cfg(feature = "bitwise")]385fn xor_reduce(&self) -> PolarsResult<Scalar> {386let dt = <$pdt as PolarsDataType>::get_static_dtype();387let av = self.0.xor_reduce().map_or(AnyValue::Null, Into::into);388389Ok(Scalar::new(dt, av))390}391392#[cfg(feature = "approx_unique")]393fn approx_n_unique(&self) -> PolarsResult<IdxSize> {394Ok(ChunkApproxNUnique::approx_n_unique(&self.0))395}396397fn clone_inner(&self) -> Arc<dyn SeriesTrait> {398Arc::new(SeriesWrap(Clone::clone(&self.0)))399}400401fn find_validity_mismatch(&self, other: &Series, idxs: &mut Vec<IdxSize>) {402self.0.find_validity_mismatch(other, idxs)403}404405#[cfg(feature = "checked_arithmetic")]406fn checked_div(&self, rhs: &Series) -> PolarsResult<Series> {407self.0.checked_div(rhs)408}409410fn as_any(&self) -> &dyn Any {411&self.0412}413414fn as_any_mut(&mut self) -> &mut dyn Any {415&mut self.0416}417418fn as_phys_any(&self) -> &dyn Any {419&self.0420}421422fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {423self as _424}425}426};427}428429#[cfg(feature = "dtype-f16")]430impl_dyn_series!(Float16Chunked, Float16Type);431impl_dyn_series!(Float32Chunked, Float32Type);432impl_dyn_series!(Float64Chunked, Float64Type);433434435