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