polars_core/series/implementations/
mod.rs

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