Path: blob/main/crates/polars-core/src/series/implementations/floats.rs
6940 views
use polars_compute::rolling::QuantileMethod;12use super::*;3use crate::chunked_array::comparison::*;4#[cfg(feature = "algorithm_group_by")]5use crate::frame::group_by::*;6use crate::prelude::*;78macro_rules! impl_dyn_series {9($ca: ident, $pdt:ident) => {10impl private::PrivateSeries for SeriesWrap<$ca> {11fn compute_len(&mut self) {12self.0.compute_len()13}14fn _field(&self) -> Cow<'_, Field> {15Cow::Borrowed(self.0.ref_field())16}17fn _dtype(&self) -> &DataType {18self.0.ref_field().dtype()19}2021fn _set_flags(&mut self, flags: StatisticsFlags) {22self.0.set_flags(flags)23}24fn _get_flags(&self) -> StatisticsFlags {25self.0.get_flags()26}27unsafe fn equal_element(28&self,29idx_self: usize,30idx_other: usize,31other: &Series,32) -> bool {33self.0.equal_element(idx_self, idx_other, other)34}3536#[cfg(feature = "zip_with")]37fn zip_with_same_type(38&self,39mask: &BooleanChunked,40other: &Series,41) -> PolarsResult<Series> {42ChunkZip::zip_with(&self.0, mask, other.as_ref().as_ref())43.map(|ca| ca.into_series())44}45fn into_total_eq_inner<'a>(&'a self) -> Box<dyn TotalEqInner + 'a> {46(&self.0).into_total_eq_inner()47}48fn into_total_ord_inner<'a>(&'a self) -> Box<dyn TotalOrdInner + 'a> {49(&self.0).into_total_ord_inner()50}5152fn vec_hash(53&self,54random_state: PlSeedableRandomStateQuality,55buf: &mut Vec<u64>,56) -> PolarsResult<()> {57self.0.vec_hash(random_state, buf)?;58Ok(())59}6061fn vec_hash_combine(62&self,63build_hasher: PlSeedableRandomStateQuality,64hashes: &mut [u64],65) -> PolarsResult<()> {66self.0.vec_hash_combine(build_hasher, hashes)?;67Ok(())68}6970#[cfg(feature = "algorithm_group_by")]71unsafe fn agg_min(&self, groups: &GroupsType) -> Series {72self.0.agg_min(groups)73}7475#[cfg(feature = "algorithm_group_by")]76unsafe fn agg_max(&self, groups: &GroupsType) -> Series {77self.0.agg_max(groups)78}7980#[cfg(feature = "algorithm_group_by")]81unsafe fn agg_sum(&self, groups: &GroupsType) -> Series {82self.0.agg_sum(groups)83}8485#[cfg(feature = "algorithm_group_by")]86unsafe fn agg_std(&self, groups: &GroupsType, ddof: u8) -> Series {87self.agg_std(groups, ddof)88}8990#[cfg(feature = "algorithm_group_by")]91unsafe fn agg_var(&self, groups: &GroupsType, ddof: u8) -> Series {92self.agg_var(groups, ddof)93}9495#[cfg(feature = "algorithm_group_by")]96unsafe fn agg_list(&self, groups: &GroupsType) -> Series {97self.0.agg_list(groups)98}99100#[cfg(feature = "bitwise")]101unsafe fn agg_and(&self, groups: &GroupsType) -> Series {102self.0.agg_and(groups)103}104#[cfg(feature = "bitwise")]105unsafe fn agg_or(&self, groups: &GroupsType) -> Series {106self.0.agg_or(groups)107}108#[cfg(feature = "bitwise")]109unsafe fn agg_xor(&self, groups: &GroupsType) -> Series {110self.0.agg_xor(groups)111}112113fn subtract(&self, rhs: &Series) -> PolarsResult<Series> {114NumOpsDispatch::subtract(&self.0, rhs)115}116fn add_to(&self, rhs: &Series) -> PolarsResult<Series> {117NumOpsDispatch::add_to(&self.0, rhs)118}119fn multiply(&self, rhs: &Series) -> PolarsResult<Series> {120NumOpsDispatch::multiply(&self.0, rhs)121}122fn divide(&self, rhs: &Series) -> PolarsResult<Series> {123NumOpsDispatch::divide(&self.0, rhs)124}125fn remainder(&self, rhs: &Series) -> PolarsResult<Series> {126NumOpsDispatch::remainder(&self.0, rhs)127}128#[cfg(feature = "algorithm_group_by")]129fn group_tuples(&self, multithreaded: bool, sorted: bool) -> PolarsResult<GroupsType> {130IntoGroupsType::group_tuples(&self.0, multithreaded, sorted)131}132133fn arg_sort_multiple(134&self,135by: &[Column],136options: &SortMultipleOptions,137) -> PolarsResult<IdxCa> {138self.0.arg_sort_multiple(by, options)139}140}141142impl SeriesTrait for SeriesWrap<$ca> {143#[cfg(feature = "rolling_window")]144fn rolling_map(145&self,146_f: &dyn Fn(&Series) -> PolarsResult<Series>,147_options: RollingOptionsFixedWindow,148) -> PolarsResult<Series> {149ChunkRollApply::rolling_map(&self.0, _f, _options).map(|ca| ca.into_series())150}151152fn rename(&mut self, name: PlSmallStr) {153self.0.rename(name);154}155156fn chunk_lengths(&self) -> ChunkLenIter<'_> {157self.0.chunk_lengths()158}159fn name(&self) -> &PlSmallStr {160self.0.name()161}162163fn chunks(&self) -> &Vec<ArrayRef> {164self.0.chunks()165}166unsafe fn chunks_mut(&mut self) -> &mut Vec<ArrayRef> {167self.0.chunks_mut()168}169fn shrink_to_fit(&mut self) {170self.0.shrink_to_fit()171}172173fn slice(&self, offset: i64, length: usize) -> Series {174return self.0.slice(offset, length).into_series();175}176177fn split_at(&self, offset: i64) -> (Series, Series) {178let (a, b) = self.0.split_at(offset);179(a.into_series(), b.into_series())180}181182fn append(&mut self, other: &Series) -> PolarsResult<()> {183polars_ensure!(self.0.dtype() == other.dtype(), append);184self.0.append(other.as_ref().as_ref())?;185Ok(())186}187fn append_owned(&mut self, other: Series) -> PolarsResult<()> {188polars_ensure!(self.0.dtype() == other.dtype(), append);189self.0.append_owned(other.take_inner())190}191192fn extend(&mut self, other: &Series) -> PolarsResult<()> {193polars_ensure!(self.0.dtype() == other.dtype(), extend);194self.0.extend(other.as_ref().as_ref())?;195Ok(())196}197198fn filter(&self, filter: &BooleanChunked) -> PolarsResult<Series> {199ChunkFilter::filter(&self.0, filter).map(|ca| ca.into_series())200}201202fn _sum_as_f64(&self) -> f64 {203self.0._sum_as_f64()204}205206fn mean(&self) -> Option<f64> {207self.0.mean()208}209210fn median(&self) -> Option<f64> {211self.0.median().map(|v| v as f64)212}213214fn std(&self, ddof: u8) -> Option<f64> {215self.0.std(ddof)216}217218fn var(&self, ddof: u8) -> Option<f64> {219self.0.var(ddof)220}221222fn take(&self, indices: &IdxCa) -> PolarsResult<Series> {223Ok(self.0.take(indices)?.into_series())224}225226unsafe fn take_unchecked(&self, indices: &IdxCa) -> Series {227self.0.take_unchecked(indices).into_series()228}229230fn take_slice(&self, indices: &[IdxSize]) -> PolarsResult<Series> {231Ok(self.0.take(indices)?.into_series())232}233234unsafe fn take_slice_unchecked(&self, indices: &[IdxSize]) -> Series {235self.0.take_unchecked(indices).into_series()236}237238fn len(&self) -> usize {239self.0.len()240}241242fn rechunk(&self) -> Series {243self.0.rechunk().into_owned().into_series()244}245246fn new_from_index(&self, index: usize, length: usize) -> Series {247ChunkExpandAtIndex::new_from_index(&self.0, index, length).into_series()248}249250fn cast(&self, dtype: &DataType, cast_options: CastOptions) -> PolarsResult<Series> {251self.0.cast_with_options(dtype, cast_options)252}253254#[inline]255unsafe fn get_unchecked(&self, index: usize) -> AnyValue<'_> {256self.0.get_any_value_unchecked(index)257}258259fn sort_with(&self, options: SortOptions) -> PolarsResult<Series> {260Ok(ChunkSort::sort_with(&self.0, options).into_series())261}262263fn arg_sort(&self, options: SortOptions) -> IdxCa {264ChunkSort::arg_sort(&self.0, options)265}266267fn null_count(&self) -> usize {268self.0.null_count()269}270271fn has_nulls(&self) -> bool {272self.0.has_nulls()273}274275#[cfg(feature = "algorithm_group_by")]276fn unique(&self) -> PolarsResult<Series> {277ChunkUnique::unique(&self.0).map(|ca| ca.into_series())278}279280#[cfg(feature = "algorithm_group_by")]281fn n_unique(&self) -> PolarsResult<usize> {282ChunkUnique::n_unique(&self.0)283}284285#[cfg(feature = "algorithm_group_by")]286fn arg_unique(&self) -> PolarsResult<IdxCa> {287ChunkUnique::arg_unique(&self.0)288}289290fn is_null(&self) -> BooleanChunked {291self.0.is_null()292}293294fn is_not_null(&self) -> BooleanChunked {295self.0.is_not_null()296}297298fn reverse(&self) -> Series {299ChunkReverse::reverse(&self.0).into_series()300}301302fn as_single_ptr(&mut self) -> PolarsResult<usize> {303self.0.as_single_ptr()304}305306fn shift(&self, periods: i64) -> Series {307ChunkShift::shift(&self.0, periods).into_series()308}309310fn sum_reduce(&self) -> PolarsResult<Scalar> {311Ok(ChunkAggSeries::sum_reduce(&self.0))312}313fn max_reduce(&self) -> PolarsResult<Scalar> {314Ok(ChunkAggSeries::max_reduce(&self.0))315}316fn min_reduce(&self) -> PolarsResult<Scalar> {317Ok(ChunkAggSeries::min_reduce(&self.0))318}319fn median_reduce(&self) -> PolarsResult<Scalar> {320Ok(QuantileAggSeries::median_reduce(&self.0))321}322fn var_reduce(&self, ddof: u8) -> PolarsResult<Scalar> {323Ok(VarAggSeries::var_reduce(&self.0, ddof))324}325fn std_reduce(&self, ddof: u8) -> PolarsResult<Scalar> {326Ok(VarAggSeries::std_reduce(&self.0, ddof))327}328fn quantile_reduce(329&self,330quantile: f64,331method: QuantileMethod,332) -> PolarsResult<Scalar> {333QuantileAggSeries::quantile_reduce(&self.0, quantile, method)334}335#[cfg(feature = "bitwise")]336fn and_reduce(&self) -> PolarsResult<Scalar> {337let dt = <$pdt as PolarsDataType>::get_static_dtype();338let av = self.0.and_reduce().map_or(AnyValue::Null, Into::into);339340Ok(Scalar::new(dt, av))341}342#[cfg(feature = "bitwise")]343fn or_reduce(&self) -> PolarsResult<Scalar> {344let dt = <$pdt as PolarsDataType>::get_static_dtype();345let av = self.0.or_reduce().map_or(AnyValue::Null, Into::into);346347Ok(Scalar::new(dt, av))348}349#[cfg(feature = "bitwise")]350fn xor_reduce(&self) -> PolarsResult<Scalar> {351let dt = <$pdt as PolarsDataType>::get_static_dtype();352let av = self.0.xor_reduce().map_or(AnyValue::Null, Into::into);353354Ok(Scalar::new(dt, av))355}356357#[cfg(feature = "approx_unique")]358fn approx_n_unique(&self) -> PolarsResult<IdxSize> {359Ok(ChunkApproxNUnique::approx_n_unique(&self.0))360}361362fn clone_inner(&self) -> Arc<dyn SeriesTrait> {363Arc::new(SeriesWrap(Clone::clone(&self.0)))364}365366fn find_validity_mismatch(&self, other: &Series, idxs: &mut Vec<IdxSize>) {367self.0.find_validity_mismatch(other, idxs)368}369370#[cfg(feature = "checked_arithmetic")]371fn checked_div(&self, rhs: &Series) -> PolarsResult<Series> {372self.0.checked_div(rhs)373}374375fn as_any(&self) -> &dyn Any {376&self.0377}378379fn as_any_mut(&mut self) -> &mut dyn Any {380&mut self.0381}382383fn as_phys_any(&self) -> &dyn Any {384&self.0385}386387fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {388self as _389}390}391};392}393394impl_dyn_series!(Float32Chunked, Float32Type);395impl_dyn_series!(Float64Chunked, Float64Type);396397398