polars_core/series/implementations/
mod.rs

1#![allow(unsafe_op_in_unsafe_fn)]
2#[cfg(feature = "dtype-array")]
3mod array;
4mod binary;
5mod binary_offset;
6mod boolean;
7#[cfg(feature = "dtype-categorical")]
8mod categorical;
9#[cfg(feature = "dtype-date")]
10mod date;
11#[cfg(feature = "dtype-datetime")]
12mod datetime;
13#[cfg(feature = "dtype-decimal")]
14mod decimal;
15#[cfg(feature = "dtype-duration")]
16mod duration;
17mod floats;
18mod list;
19pub(crate) mod null;
20#[cfg(feature = "object")]
21mod object;
22mod string;
23#[cfg(feature = "dtype-struct")]
24mod struct_;
25#[cfg(feature = "dtype-time")]
26mod time;
27
28use std::any::Any;
29use std::borrow::Cow;
30
31use arrow::bitmap::Bitmap;
32use polars_compute::rolling::QuantileMethod;
33use polars_utils::aliases::PlSeedableRandomStateQuality;
34
35use super::*;
36use crate::chunked_array::AsSinglePtr;
37use crate::chunked_array::comparison::*;
38use crate::chunked_array::ops::compare_inner::{
39    IntoTotalEqInner, IntoTotalOrdInner, TotalEqInner, TotalOrdInner,
40};
41
42// Utility wrapper struct
43pub(crate) struct SeriesWrap<T>(pub T);
44
45impl<T: PolarsDataType> From<ChunkedArray<T>> for SeriesWrap<ChunkedArray<T>> {
46    fn from(ca: ChunkedArray<T>) -> Self {
47        SeriesWrap(ca)
48    }
49}
50
51impl<T: PolarsDataType> Deref for SeriesWrap<ChunkedArray<T>> {
52    type Target = ChunkedArray<T>;
53
54    fn deref(&self) -> &Self::Target {
55        &self.0
56    }
57}
58
59unsafe impl<T: PolarsPhysicalType> IntoSeries for ChunkedArray<T> {
60    fn into_series(self) -> Series {
61        T::ca_into_series(self)
62    }
63}
64
65macro_rules! impl_dyn_series {
66    ($ca: ident, $pdt:ty) => {
67        impl private::PrivateSeries for SeriesWrap<$ca> {
68            fn compute_len(&mut self) {
69                self.0.compute_len()
70            }
71
72            fn _field(&self) -> Cow<'_, Field> {
73                Cow::Borrowed(self.0.ref_field())
74            }
75
76            fn _dtype(&self) -> &DataType {
77                self.0.ref_field().dtype()
78            }
79
80            fn _get_flags(&self) -> StatisticsFlags {
81                self.0.get_flags()
82            }
83
84            fn _set_flags(&mut self, flags: StatisticsFlags) {
85                self.0.set_flags(flags)
86            }
87
88            unsafe fn equal_element(
89                &self,
90                idx_self: usize,
91                idx_other: usize,
92                other: &Series,
93            ) -> bool {
94                self.0.equal_element(idx_self, idx_other, other)
95            }
96
97            #[cfg(feature = "zip_with")]
98            fn zip_with_same_type(
99                &self,
100                mask: &BooleanChunked,
101                other: &Series,
102            ) -> PolarsResult<Series> {
103                ChunkZip::zip_with(&self.0, mask, other.as_ref().as_ref())
104                    .map(|ca| ca.into_series())
105            }
106            fn into_total_eq_inner<'a>(&'a self) -> Box<dyn TotalEqInner + 'a> {
107                (&self.0).into_total_eq_inner()
108            }
109            fn into_total_ord_inner<'a>(&'a self) -> Box<dyn TotalOrdInner + 'a> {
110                (&self.0).into_total_ord_inner()
111            }
112
113            fn vec_hash(
114                &self,
115                random_state: PlSeedableRandomStateQuality,
116                buf: &mut Vec<u64>,
117            ) -> PolarsResult<()> {
118                self.0.vec_hash(random_state, buf)?;
119                Ok(())
120            }
121
122            fn vec_hash_combine(
123                &self,
124                build_hasher: PlSeedableRandomStateQuality,
125                hashes: &mut [u64],
126            ) -> PolarsResult<()> {
127                self.0.vec_hash_combine(build_hasher, hashes)?;
128                Ok(())
129            }
130
131            #[cfg(feature = "algorithm_group_by")]
132            unsafe fn agg_min(&self, groups: &GroupsType) -> Series {
133                self.0.agg_min(groups)
134            }
135
136            #[cfg(feature = "algorithm_group_by")]
137            unsafe fn agg_max(&self, groups: &GroupsType) -> Series {
138                self.0.agg_max(groups)
139            }
140
141            #[cfg(feature = "algorithm_group_by")]
142            unsafe fn agg_sum(&self, groups: &GroupsType) -> Series {
143                use DataType::*;
144                match self.dtype() {
145                    Int8 | UInt8 | Int16 | UInt16 => self
146                        .cast(&Int64, CastOptions::Overflowing)
147                        .unwrap()
148                        .agg_sum(groups),
149                    _ => self.0.agg_sum(groups),
150                }
151            }
152
153            #[cfg(feature = "algorithm_group_by")]
154            unsafe fn agg_std(&self, groups: &GroupsType, ddof: u8) -> Series {
155                self.0.agg_std(groups, ddof)
156            }
157
158            #[cfg(feature = "algorithm_group_by")]
159            unsafe fn agg_var(&self, groups: &GroupsType, ddof: u8) -> Series {
160                self.0.agg_var(groups, ddof)
161            }
162
163            #[cfg(feature = "algorithm_group_by")]
164            unsafe fn agg_list(&self, groups: &GroupsType) -> Series {
165                self.0.agg_list(groups)
166            }
167
168            #[cfg(feature = "bitwise")]
169            unsafe fn agg_and(&self, groups: &GroupsType) -> Series {
170                self.0.agg_and(groups)
171            }
172            #[cfg(feature = "bitwise")]
173            unsafe fn agg_or(&self, groups: &GroupsType) -> Series {
174                self.0.agg_or(groups)
175            }
176            #[cfg(feature = "bitwise")]
177            unsafe fn agg_xor(&self, groups: &GroupsType) -> Series {
178                self.0.agg_xor(groups)
179            }
180
181            fn subtract(&self, rhs: &Series) -> PolarsResult<Series> {
182                NumOpsDispatch::subtract(&self.0, rhs)
183            }
184            fn add_to(&self, rhs: &Series) -> PolarsResult<Series> {
185                NumOpsDispatch::add_to(&self.0, rhs)
186            }
187            fn multiply(&self, rhs: &Series) -> PolarsResult<Series> {
188                NumOpsDispatch::multiply(&self.0, rhs)
189            }
190            fn divide(&self, rhs: &Series) -> PolarsResult<Series> {
191                NumOpsDispatch::divide(&self.0, rhs)
192            }
193            fn remainder(&self, rhs: &Series) -> PolarsResult<Series> {
194                NumOpsDispatch::remainder(&self.0, rhs)
195            }
196            #[cfg(feature = "algorithm_group_by")]
197            fn group_tuples(&self, multithreaded: bool, sorted: bool) -> PolarsResult<GroupsType> {
198                IntoGroupsType::group_tuples(&self.0, multithreaded, sorted)
199            }
200
201            fn arg_sort_multiple(
202                &self,
203                by: &[Column],
204                options: &SortMultipleOptions,
205            ) -> PolarsResult<IdxCa> {
206                self.0.arg_sort_multiple(by, options)
207            }
208        }
209
210        impl SeriesTrait for SeriesWrap<$ca> {
211            #[cfg(feature = "rolling_window")]
212            fn rolling_map(
213                &self,
214                _f: &dyn Fn(&Series) -> PolarsResult<Series>,
215                _options: RollingOptionsFixedWindow,
216            ) -> PolarsResult<Series> {
217                ChunkRollApply::rolling_map(&self.0, _f, _options).map(|ca| ca.into_series())
218            }
219
220            fn rename(&mut self, name: PlSmallStr) {
221                self.0.rename(name);
222            }
223
224            fn chunk_lengths(&self) -> ChunkLenIter<'_> {
225                self.0.chunk_lengths()
226            }
227            fn name(&self) -> &PlSmallStr {
228                self.0.name()
229            }
230
231            fn chunks(&self) -> &Vec<ArrayRef> {
232                self.0.chunks()
233            }
234            unsafe fn chunks_mut(&mut self) -> &mut Vec<ArrayRef> {
235                self.0.chunks_mut()
236            }
237            fn shrink_to_fit(&mut self) {
238                self.0.shrink_to_fit()
239            }
240
241            fn slice(&self, offset: i64, length: usize) -> Series {
242                self.0.slice(offset, length).into_series()
243            }
244
245            fn split_at(&self, offset: i64) -> (Series, Series) {
246                let (a, b) = self.0.split_at(offset);
247                (a.into_series(), b.into_series())
248            }
249
250            fn append(&mut self, other: &Series) -> PolarsResult<()> {
251                polars_ensure!(self.0.dtype() == other.dtype(), append);
252                self.0.append(other.as_ref().as_ref())?;
253                Ok(())
254            }
255            fn append_owned(&mut self, other: Series) -> PolarsResult<()> {
256                polars_ensure!(self.0.dtype() == other.dtype(), append);
257                self.0.append_owned(other.take_inner())
258            }
259
260            fn extend(&mut self, other: &Series) -> PolarsResult<()> {
261                polars_ensure!(self.0.dtype() == other.dtype(), extend);
262                self.0.extend(other.as_ref().as_ref())?;
263                Ok(())
264            }
265
266            fn filter(&self, filter: &BooleanChunked) -> PolarsResult<Series> {
267                ChunkFilter::filter(&self.0, filter).map(|ca| ca.into_series())
268            }
269
270            fn _sum_as_f64(&self) -> f64 {
271                self.0._sum_as_f64()
272            }
273
274            fn mean(&self) -> Option<f64> {
275                self.0.mean()
276            }
277
278            fn median(&self) -> Option<f64> {
279                self.0.median()
280            }
281
282            fn std(&self, ddof: u8) -> Option<f64> {
283                self.0.std(ddof)
284            }
285
286            fn var(&self, ddof: u8) -> Option<f64> {
287                self.0.var(ddof)
288            }
289
290            fn take(&self, indices: &IdxCa) -> PolarsResult<Series> {
291                Ok(self.0.take(indices)?.into_series())
292            }
293
294            unsafe fn take_unchecked(&self, indices: &IdxCa) -> Series {
295                self.0.take_unchecked(indices).into_series()
296            }
297
298            fn take_slice(&self, indices: &[IdxSize]) -> PolarsResult<Series> {
299                Ok(self.0.take(indices)?.into_series())
300            }
301
302            unsafe fn take_slice_unchecked(&self, indices: &[IdxSize]) -> Series {
303                self.0.take_unchecked(indices).into_series()
304            }
305
306            fn deposit(&self, validity: &Bitmap) -> Series {
307                self.0.deposit(validity).into_series()
308            }
309
310            fn len(&self) -> usize {
311                self.0.len()
312            }
313
314            fn rechunk(&self) -> Series {
315                self.0.rechunk().into_owned().into_series()
316            }
317
318            fn new_from_index(&self, index: usize, length: usize) -> Series {
319                ChunkExpandAtIndex::new_from_index(&self.0, index, length).into_series()
320            }
321
322            fn cast(&self, dtype: &DataType, options: CastOptions) -> PolarsResult<Series> {
323                self.0.cast_with_options(dtype, options)
324            }
325
326            #[inline]
327            unsafe fn get_unchecked(&self, index: usize) -> AnyValue<'_> {
328                self.0.get_any_value_unchecked(index)
329            }
330
331            fn sort_with(&self, options: SortOptions) -> PolarsResult<Series> {
332                Ok(ChunkSort::sort_with(&self.0, options).into_series())
333            }
334
335            fn arg_sort(&self, options: SortOptions) -> IdxCa {
336                ChunkSort::arg_sort(&self.0, options)
337            }
338
339            fn null_count(&self) -> usize {
340                self.0.null_count()
341            }
342
343            fn has_nulls(&self) -> bool {
344                self.0.has_nulls()
345            }
346
347            #[cfg(feature = "algorithm_group_by")]
348            fn unique(&self) -> PolarsResult<Series> {
349                ChunkUnique::unique(&self.0).map(|ca| ca.into_series())
350            }
351
352            #[cfg(feature = "algorithm_group_by")]
353            fn n_unique(&self) -> PolarsResult<usize> {
354                ChunkUnique::n_unique(&self.0)
355            }
356
357            #[cfg(feature = "algorithm_group_by")]
358            fn arg_unique(&self) -> PolarsResult<IdxCa> {
359                ChunkUnique::arg_unique(&self.0)
360            }
361
362            fn is_null(&self) -> BooleanChunked {
363                self.0.is_null()
364            }
365
366            fn is_not_null(&self) -> BooleanChunked {
367                self.0.is_not_null()
368            }
369
370            fn reverse(&self) -> Series {
371                ChunkReverse::reverse(&self.0).into_series()
372            }
373
374            fn as_single_ptr(&mut self) -> PolarsResult<usize> {
375                self.0.as_single_ptr()
376            }
377
378            fn shift(&self, periods: i64) -> Series {
379                ChunkShift::shift(&self.0, periods).into_series()
380            }
381
382            fn sum_reduce(&self) -> PolarsResult<Scalar> {
383                Ok(ChunkAggSeries::sum_reduce(&self.0))
384            }
385            fn max_reduce(&self) -> PolarsResult<Scalar> {
386                Ok(ChunkAggSeries::max_reduce(&self.0))
387            }
388            fn min_reduce(&self) -> PolarsResult<Scalar> {
389                Ok(ChunkAggSeries::min_reduce(&self.0))
390            }
391            fn mean_reduce(&self) -> PolarsResult<Scalar> {
392                Ok(Scalar::new(DataType::Float64, self.mean().into()))
393            }
394            fn median_reduce(&self) -> PolarsResult<Scalar> {
395                Ok(QuantileAggSeries::median_reduce(&self.0))
396            }
397            fn var_reduce(&self, ddof: u8) -> PolarsResult<Scalar> {
398                Ok(VarAggSeries::var_reduce(&self.0, ddof))
399            }
400            fn std_reduce(&self, ddof: u8) -> PolarsResult<Scalar> {
401                Ok(VarAggSeries::std_reduce(&self.0, ddof))
402            }
403            fn quantile_reduce(
404                &self,
405                quantile: f64,
406                method: QuantileMethod,
407            ) -> PolarsResult<Scalar> {
408                QuantileAggSeries::quantile_reduce(&self.0, quantile, method)
409            }
410
411            #[cfg(feature = "bitwise")]
412            fn and_reduce(&self) -> PolarsResult<Scalar> {
413                let dt = <$pdt as PolarsDataType>::get_static_dtype();
414                let av = self.0.and_reduce().map_or(AnyValue::Null, Into::into);
415
416                Ok(Scalar::new(dt, av))
417            }
418
419            #[cfg(feature = "bitwise")]
420            fn or_reduce(&self) -> PolarsResult<Scalar> {
421                let dt = <$pdt as PolarsDataType>::get_static_dtype();
422                let av = self.0.or_reduce().map_or(AnyValue::Null, Into::into);
423
424                Ok(Scalar::new(dt, av))
425            }
426
427            #[cfg(feature = "bitwise")]
428            fn xor_reduce(&self) -> PolarsResult<Scalar> {
429                let dt = <$pdt as PolarsDataType>::get_static_dtype();
430                let av = self.0.xor_reduce().map_or(AnyValue::Null, Into::into);
431
432                Ok(Scalar::new(dt, av))
433            }
434
435            #[cfg(feature = "approx_unique")]
436            fn approx_n_unique(&self) -> PolarsResult<IdxSize> {
437                Ok(ChunkApproxNUnique::approx_n_unique(&self.0))
438            }
439
440            fn clone_inner(&self) -> Arc<dyn SeriesTrait> {
441                Arc::new(SeriesWrap(Clone::clone(&self.0)))
442            }
443
444            fn find_validity_mismatch(&self, other: &Series, idxs: &mut Vec<IdxSize>) {
445                self.0.find_validity_mismatch(other, idxs)
446            }
447
448            #[cfg(feature = "checked_arithmetic")]
449            fn checked_div(&self, rhs: &Series) -> PolarsResult<Series> {
450                self.0.checked_div(rhs)
451            }
452
453            fn as_any(&self) -> &dyn Any {
454                &self.0
455            }
456
457            fn as_any_mut(&mut self) -> &mut dyn Any {
458                &mut self.0
459            }
460
461            fn as_phys_any(&self) -> &dyn Any {
462                &self.0
463            }
464
465            fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
466                self as _
467            }
468        }
469    };
470}
471
472#[cfg(feature = "dtype-u8")]
473impl_dyn_series!(UInt8Chunked, UInt8Type);
474#[cfg(feature = "dtype-u16")]
475impl_dyn_series!(UInt16Chunked, UInt16Type);
476impl_dyn_series!(UInt32Chunked, UInt32Type);
477impl_dyn_series!(UInt64Chunked, UInt64Type);
478#[cfg(feature = "dtype-u128")]
479impl_dyn_series!(UInt128Chunked, UInt128Type);
480#[cfg(feature = "dtype-i8")]
481impl_dyn_series!(Int8Chunked, Int8Type);
482#[cfg(feature = "dtype-i16")]
483impl_dyn_series!(Int16Chunked, Int16Type);
484impl_dyn_series!(Int32Chunked, Int32Type);
485impl_dyn_series!(Int64Chunked, Int64Type);
486#[cfg(feature = "dtype-i128")]
487impl_dyn_series!(Int128Chunked, Int128Type);
488
489impl<T: PolarsNumericType> private::PrivateSeriesNumeric for SeriesWrap<ChunkedArray<T>> {
490    fn bit_repr(&self) -> Option<BitRepr> {
491        Some(self.0.to_bit_repr())
492    }
493}
494
495impl private::PrivateSeriesNumeric for SeriesWrap<StringChunked> {
496    fn bit_repr(&self) -> Option<BitRepr> {
497        None
498    }
499}
500impl private::PrivateSeriesNumeric for SeriesWrap<BinaryChunked> {
501    fn bit_repr(&self) -> Option<BitRepr> {
502        None
503    }
504}
505impl private::PrivateSeriesNumeric for SeriesWrap<BinaryOffsetChunked> {
506    fn bit_repr(&self) -> Option<BitRepr> {
507        None
508    }
509}
510impl private::PrivateSeriesNumeric for SeriesWrap<ListChunked> {
511    fn bit_repr(&self) -> Option<BitRepr> {
512        None
513    }
514}
515#[cfg(feature = "dtype-array")]
516impl private::PrivateSeriesNumeric for SeriesWrap<ArrayChunked> {
517    fn bit_repr(&self) -> Option<BitRepr> {
518        None
519    }
520}
521impl private::PrivateSeriesNumeric for SeriesWrap<BooleanChunked> {
522    fn bit_repr(&self) -> Option<BitRepr> {
523        let repr = self
524            .0
525            .cast_with_options(&DataType::UInt32, CastOptions::NonStrict)
526            .unwrap()
527            .u32()
528            .unwrap()
529            .clone();
530
531        Some(BitRepr::U32(repr))
532    }
533}