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