Path: blob/main/crates/polars-core/src/series/implementations/mod.rs
8426 views
#![allow(unsafe_op_in_unsafe_fn)]1#[cfg(feature = "dtype-array")]2mod array;3mod binary;4mod binary_offset;5mod boolean;6#[cfg(feature = "dtype-categorical")]7mod categorical;8#[cfg(feature = "dtype-date")]9mod date;10#[cfg(feature = "dtype-datetime")]11mod datetime;12#[cfg(feature = "dtype-decimal")]13mod decimal;14#[cfg(feature = "dtype-duration")]15mod duration;16#[cfg(feature = "dtype-extension")]17mod extension;18mod floats;19mod list;20pub(crate) mod null;21#[cfg(feature = "object")]22mod object;23mod string;24#[cfg(feature = "dtype-struct")]25mod struct_;26#[cfg(feature = "dtype-time")]27mod time;2829use std::any::Any;30use std::borrow::Cow;3132use arrow::bitmap::Bitmap;33use polars_compute::rolling::QuantileMethod;34use polars_utils::aliases::PlSeedableRandomStateQuality;3536use super::*;37use crate::chunked_array::AsSinglePtr;38use crate::chunked_array::comparison::*;39use crate::chunked_array::ops::compare_inner::{40IntoTotalEqInner, IntoTotalOrdInner, TotalEqInner, TotalOrdInner,41};4243// Utility wrapper struct44#[repr(transparent)]45pub(crate) struct SeriesWrap<T>(pub T);4647impl<T: PolarsDataType> From<ChunkedArray<T>> for SeriesWrap<ChunkedArray<T>> {48fn from(ca: ChunkedArray<T>) -> Self {49SeriesWrap(ca)50}51}5253impl<T: PolarsDataType> Deref for SeriesWrap<ChunkedArray<T>> {54type Target = ChunkedArray<T>;5556fn deref(&self) -> &Self::Target {57&self.058}59}6061unsafe impl<T: PolarsPhysicalType> IntoSeries for ChunkedArray<T> {62fn into_series(self) -> Series {63T::ca_into_series(self)64}65}6667macro_rules! impl_dyn_series {68($ca: ident, $pdt:ty) => {69impl private::PrivateSeries for SeriesWrap<$ca> {70fn compute_len(&mut self) {71self.0.compute_len()72}7374fn _field(&self) -> Cow<'_, Field> {75Cow::Borrowed(self.0.ref_field())76}7778fn _dtype(&self) -> &DataType {79self.0.ref_field().dtype()80}8182fn _get_flags(&self) -> StatisticsFlags {83self.0.get_flags()84}8586fn _set_flags(&mut self, flags: StatisticsFlags) {87self.0.set_flags(flags)88}8990unsafe fn equal_element(91&self,92idx_self: usize,93idx_other: usize,94other: &Series,95) -> bool {96self.0.equal_element(idx_self, idx_other, other)97}9899#[cfg(feature = "zip_with")]100fn zip_with_same_type(101&self,102mask: &BooleanChunked,103other: &Series,104) -> PolarsResult<Series> {105ChunkZip::zip_with(&self.0, mask, other.as_ref().as_ref())106.map(|ca| ca.into_series())107}108fn into_total_eq_inner<'a>(&'a self) -> Box<dyn TotalEqInner + 'a> {109(&self.0).into_total_eq_inner()110}111fn into_total_ord_inner<'a>(&'a self) -> Box<dyn TotalOrdInner + 'a> {112(&self.0).into_total_ord_inner()113}114115fn vec_hash(116&self,117random_state: PlSeedableRandomStateQuality,118buf: &mut Vec<u64>,119) -> PolarsResult<()> {120self.0.vec_hash(random_state, buf)?;121Ok(())122}123124fn vec_hash_combine(125&self,126build_hasher: PlSeedableRandomStateQuality,127hashes: &mut [u64],128) -> PolarsResult<()> {129self.0.vec_hash_combine(build_hasher, hashes)?;130Ok(())131}132133#[cfg(feature = "algorithm_group_by")]134unsafe fn agg_min(&self, groups: &GroupsType) -> Series {135self.0.agg_min(groups)136}137138#[cfg(feature = "algorithm_group_by")]139unsafe fn agg_max(&self, groups: &GroupsType) -> Series {140self.0.agg_max(groups)141}142143#[cfg(feature = "algorithm_group_by")]144unsafe fn agg_arg_min(&self, groups: &GroupsType) -> Series {145self.0.agg_arg_min(groups)146}147148#[cfg(feature = "algorithm_group_by")]149unsafe fn agg_arg_max(&self, groups: &GroupsType) -> Series {150self.0.agg_arg_max(groups)151}152153#[cfg(feature = "algorithm_group_by")]154unsafe fn agg_sum(&self, groups: &GroupsType) -> Series {155use DataType::*;156match self.dtype() {157Int8 | UInt8 | Int16 | UInt16 => self158.cast(&Int64, CastOptions::Overflowing)159.unwrap()160.agg_sum(groups),161_ => self.0.agg_sum(groups),162}163}164165#[cfg(feature = "algorithm_group_by")]166unsafe fn agg_std(&self, groups: &GroupsType, ddof: u8) -> Series {167self.0.agg_std(groups, ddof)168}169170#[cfg(feature = "algorithm_group_by")]171unsafe fn agg_var(&self, groups: &GroupsType, ddof: u8) -> Series {172self.0.agg_var(groups, ddof)173}174175#[cfg(feature = "algorithm_group_by")]176unsafe fn agg_list(&self, groups: &GroupsType) -> Series {177self.0.agg_list(groups)178}179180#[cfg(feature = "bitwise")]181unsafe fn agg_and(&self, groups: &GroupsType) -> Series {182self.0.agg_and(groups)183}184#[cfg(feature = "bitwise")]185unsafe fn agg_or(&self, groups: &GroupsType) -> Series {186self.0.agg_or(groups)187}188#[cfg(feature = "bitwise")]189unsafe fn agg_xor(&self, groups: &GroupsType) -> Series {190self.0.agg_xor(groups)191}192193fn subtract(&self, rhs: &Series) -> PolarsResult<Series> {194NumOpsDispatch::subtract(&self.0, rhs)195}196fn add_to(&self, rhs: &Series) -> PolarsResult<Series> {197NumOpsDispatch::add_to(&self.0, rhs)198}199fn multiply(&self, rhs: &Series) -> PolarsResult<Series> {200NumOpsDispatch::multiply(&self.0, rhs)201}202fn divide(&self, rhs: &Series) -> PolarsResult<Series> {203NumOpsDispatch::divide(&self.0, rhs)204}205fn remainder(&self, rhs: &Series) -> PolarsResult<Series> {206NumOpsDispatch::remainder(&self.0, rhs)207}208#[cfg(feature = "algorithm_group_by")]209fn group_tuples(&self, multithreaded: bool, sorted: bool) -> PolarsResult<GroupsType> {210IntoGroupsType::group_tuples(&self.0, multithreaded, sorted)211}212213fn arg_sort_multiple(214&self,215by: &[Column],216options: &SortMultipleOptions,217) -> PolarsResult<IdxCa> {218self.0.arg_sort_multiple(by, options)219}220}221222impl SeriesTrait for SeriesWrap<$ca> {223#[cfg(feature = "rolling_window")]224fn rolling_map(225&self,226_f: &dyn Fn(&Series) -> PolarsResult<Series>,227_options: RollingOptionsFixedWindow,228) -> PolarsResult<Series> {229ChunkRollApply::rolling_map(&self.0, _f, _options).map(|ca| ca.into_series())230}231232fn rename(&mut self, name: PlSmallStr) {233self.0.rename(name);234}235236fn chunk_lengths(&self) -> ChunkLenIter<'_> {237self.0.chunk_lengths()238}239fn name(&self) -> &PlSmallStr {240self.0.name()241}242243fn chunks(&self) -> &Vec<ArrayRef> {244self.0.chunks()245}246unsafe fn chunks_mut(&mut self) -> &mut Vec<ArrayRef> {247self.0.chunks_mut()248}249fn shrink_to_fit(&mut self) {250self.0.shrink_to_fit()251}252253fn slice(&self, offset: i64, length: usize) -> Series {254self.0.slice(offset, length).into_series()255}256257fn split_at(&self, offset: i64) -> (Series, Series) {258let (a, b) = self.0.split_at(offset);259(a.into_series(), b.into_series())260}261262fn append(&mut self, other: &Series) -> PolarsResult<()> {263polars_ensure!(self.0.dtype() == other.dtype(), append);264self.0.append(other.as_ref().as_ref())?;265Ok(())266}267fn append_owned(&mut self, other: Series) -> PolarsResult<()> {268polars_ensure!(self.0.dtype() == other.dtype(), append);269self.0.append_owned(other.take_inner())270}271272fn extend(&mut self, other: &Series) -> PolarsResult<()> {273polars_ensure!(self.0.dtype() == other.dtype(), extend);274self.0.extend(other.as_ref().as_ref())?;275Ok(())276}277278fn filter(&self, filter: &BooleanChunked) -> PolarsResult<Series> {279ChunkFilter::filter(&self.0, filter).map(|ca| ca.into_series())280}281282fn _sum_as_f64(&self) -> f64 {283self.0._sum_as_f64()284}285286fn mean(&self) -> Option<f64> {287self.0.mean()288}289290fn median(&self) -> Option<f64> {291self.0.median()292}293294fn std(&self, ddof: u8) -> Option<f64> {295self.0.std(ddof)296}297298fn var(&self, ddof: u8) -> Option<f64> {299self.0.var(ddof)300}301302fn take(&self, indices: &IdxCa) -> PolarsResult<Series> {303Ok(self.0.take(indices)?.into_series())304}305306unsafe fn take_unchecked(&self, indices: &IdxCa) -> Series {307self.0.take_unchecked(indices).into_series()308}309310fn take_slice(&self, indices: &[IdxSize]) -> PolarsResult<Series> {311Ok(self.0.take(indices)?.into_series())312}313314unsafe fn take_slice_unchecked(&self, indices: &[IdxSize]) -> Series {315self.0.take_unchecked(indices).into_series()316}317318fn deposit(&self, validity: &Bitmap) -> Series {319self.0.deposit(validity).into_series()320}321322fn len(&self) -> usize {323self.0.len()324}325326fn rechunk(&self) -> Series {327self.0.rechunk().into_owned().into_series()328}329330fn new_from_index(&self, index: usize, length: usize) -> Series {331ChunkExpandAtIndex::new_from_index(&self.0, index, length).into_series()332}333334fn cast(&self, dtype: &DataType, options: CastOptions) -> PolarsResult<Series> {335self.0.cast_with_options(dtype, options)336}337338#[inline]339unsafe fn get_unchecked(&self, index: usize) -> AnyValue<'_> {340self.0.get_any_value_unchecked(index)341}342343fn sort_with(&self, options: SortOptions) -> PolarsResult<Series> {344Ok(ChunkSort::sort_with(&self.0, options).into_series())345}346347fn arg_sort(&self, options: SortOptions) -> IdxCa {348ChunkSort::arg_sort(&self.0, options)349}350351fn null_count(&self) -> usize {352self.0.null_count()353}354355fn has_nulls(&self) -> bool {356self.0.has_nulls()357}358359#[cfg(feature = "algorithm_group_by")]360fn unique(&self) -> PolarsResult<Series> {361ChunkUnique::unique(&self.0).map(|ca| ca.into_series())362}363364#[cfg(feature = "algorithm_group_by")]365fn n_unique(&self) -> PolarsResult<usize> {366ChunkUnique::n_unique(&self.0)367}368369#[cfg(feature = "algorithm_group_by")]370fn arg_unique(&self) -> PolarsResult<IdxCa> {371ChunkUnique::arg_unique(&self.0)372}373374fn unique_id(&self) -> PolarsResult<(IdxSize, Vec<IdxSize>)> {375ChunkUnique::unique_id(&self.0)376}377378fn is_null(&self) -> BooleanChunked {379self.0.is_null()380}381382fn is_not_null(&self) -> BooleanChunked {383self.0.is_not_null()384}385386fn reverse(&self) -> Series {387ChunkReverse::reverse(&self.0).into_series()388}389390fn as_single_ptr(&mut self) -> PolarsResult<usize> {391self.0.as_single_ptr()392}393394fn shift(&self, periods: i64) -> Series {395ChunkShift::shift(&self.0, periods).into_series()396}397398fn sum_reduce(&self) -> PolarsResult<Scalar> {399Ok(ChunkAggSeries::sum_reduce(&self.0))400}401fn max_reduce(&self) -> PolarsResult<Scalar> {402Ok(ChunkAggSeries::max_reduce(&self.0))403}404fn min_reduce(&self) -> PolarsResult<Scalar> {405Ok(ChunkAggSeries::min_reduce(&self.0))406}407fn mean_reduce(&self) -> PolarsResult<Scalar> {408Ok(Scalar::new(DataType::Float64, self.mean().into()))409}410fn median_reduce(&self) -> PolarsResult<Scalar> {411Ok(QuantileAggSeries::median_reduce(&self.0))412}413fn var_reduce(&self, ddof: u8) -> PolarsResult<Scalar> {414Ok(VarAggSeries::var_reduce(&self.0, ddof))415}416fn std_reduce(&self, ddof: u8) -> PolarsResult<Scalar> {417Ok(VarAggSeries::std_reduce(&self.0, ddof))418}419420fn quantile_reduce(421&self,422quantile: f64,423method: QuantileMethod,424) -> PolarsResult<Scalar> {425QuantileAggSeries::quantile_reduce(&self.0, quantile, method)426}427428fn quantiles_reduce(429&self,430quantiles: &[f64],431method: QuantileMethod,432) -> PolarsResult<Scalar> {433QuantileAggSeries::quantiles_reduce(&self.0, quantiles, method)434}435436#[cfg(feature = "bitwise")]437fn and_reduce(&self) -> PolarsResult<Scalar> {438let dt = <$pdt as PolarsDataType>::get_static_dtype();439let av = self.0.and_reduce().map_or(AnyValue::Null, Into::into);440441Ok(Scalar::new(dt, av))442}443444#[cfg(feature = "bitwise")]445fn or_reduce(&self) -> PolarsResult<Scalar> {446let dt = <$pdt as PolarsDataType>::get_static_dtype();447let av = self.0.or_reduce().map_or(AnyValue::Null, Into::into);448449Ok(Scalar::new(dt, av))450}451452#[cfg(feature = "bitwise")]453fn xor_reduce(&self) -> PolarsResult<Scalar> {454let dt = <$pdt as PolarsDataType>::get_static_dtype();455let av = self.0.xor_reduce().map_or(AnyValue::Null, Into::into);456457Ok(Scalar::new(dt, av))458}459460#[cfg(feature = "approx_unique")]461fn approx_n_unique(&self) -> PolarsResult<IdxSize> {462Ok(ChunkApproxNUnique::approx_n_unique(&self.0))463}464465fn clone_inner(&self) -> Arc<dyn SeriesTrait> {466Arc::new(SeriesWrap(Clone::clone(&self.0)))467}468469fn find_validity_mismatch(&self, other: &Series, idxs: &mut Vec<IdxSize>) {470self.0.find_validity_mismatch(other, idxs)471}472473#[cfg(feature = "checked_arithmetic")]474fn checked_div(&self, rhs: &Series) -> PolarsResult<Series> {475self.0.checked_div(rhs)476}477478fn as_any(&self) -> &dyn Any {479&self.0480}481482fn as_any_mut(&mut self) -> &mut dyn Any {483&mut self.0484}485486fn as_phys_any(&self) -> &dyn Any {487&self.0488}489490fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {491self as _492}493}494};495}496497#[cfg(feature = "dtype-u8")]498impl_dyn_series!(UInt8Chunked, UInt8Type);499#[cfg(feature = "dtype-u16")]500impl_dyn_series!(UInt16Chunked, UInt16Type);501impl_dyn_series!(UInt32Chunked, UInt32Type);502impl_dyn_series!(UInt64Chunked, UInt64Type);503#[cfg(feature = "dtype-u128")]504impl_dyn_series!(UInt128Chunked, UInt128Type);505#[cfg(feature = "dtype-i8")]506impl_dyn_series!(Int8Chunked, Int8Type);507#[cfg(feature = "dtype-i16")]508impl_dyn_series!(Int16Chunked, Int16Type);509impl_dyn_series!(Int32Chunked, Int32Type);510impl_dyn_series!(Int64Chunked, Int64Type);511#[cfg(feature = "dtype-i128")]512impl_dyn_series!(Int128Chunked, Int128Type);513514impl<T: PolarsNumericType> private::PrivateSeriesNumeric for SeriesWrap<ChunkedArray<T>> {515fn bit_repr(&self) -> Option<BitRepr> {516Some(self.0.to_bit_repr())517}518}519520impl private::PrivateSeriesNumeric for SeriesWrap<StringChunked> {521fn bit_repr(&self) -> Option<BitRepr> {522None523}524}525impl private::PrivateSeriesNumeric for SeriesWrap<BinaryChunked> {526fn bit_repr(&self) -> Option<BitRepr> {527None528}529}530impl private::PrivateSeriesNumeric for SeriesWrap<BinaryOffsetChunked> {531fn bit_repr(&self) -> Option<BitRepr> {532None533}534}535impl private::PrivateSeriesNumeric for SeriesWrap<ListChunked> {536fn bit_repr(&self) -> Option<BitRepr> {537None538}539}540#[cfg(feature = "dtype-array")]541impl private::PrivateSeriesNumeric for SeriesWrap<ArrayChunked> {542fn bit_repr(&self) -> Option<BitRepr> {543None544}545}546impl private::PrivateSeriesNumeric for SeriesWrap<BooleanChunked> {547fn bit_repr(&self) -> Option<BitRepr> {548let repr = self549.0550.cast_with_options(&DataType::UInt32, CastOptions::NonStrict)551.unwrap()552.u32()553.unwrap()554.clone();555556Some(BitRepr::U32(repr))557}558}559560561