Skip to main content

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