polars_core/series/implementations/
mod.rs1#![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::ops::compare_inner::{
40 IntoTotalEqInner, IntoTotalOrdInner, TotalEqInner, TotalOrdInner,
41};
42
43#[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 fn into_series(self) -> Series {
63 T::ca_into_series(self)
64 }
65}
66
67macro_rules! impl_dyn_series {
68 ($ca: ident, $pdt:ty) => {
69 impl private::PrivateSeries for SeriesWrap<$ca> {
70 fn compute_len(&mut self) {
71 self.0.compute_len()
72 }
73
74 fn _field(&self) -> Cow<'_, Field> {
75 Cow::Borrowed(self.0.ref_field())
76 }
77
78 fn _dtype(&self) -> &DataType {
79 self.0.ref_field().dtype()
80 }
81
82 fn _get_flags(&self) -> StatisticsFlags {
83 self.0.get_flags()
84 }
85
86 fn _set_flags(&mut self, flags: StatisticsFlags) {
87 self.0.set_flags(flags)
88 }
89
90 #[cfg(feature = "zip_with")]
91 fn zip_with_same_type(
92 &self,
93 mask: &BooleanChunked,
94 other: &Series,
95 ) -> PolarsResult<Series> {
96 ChunkZip::zip_with(&self.0, mask, other.as_ref().as_ref())
97 .map(|ca| ca.into_series())
98 }
99 fn into_total_eq_inner<'a>(&'a self) -> Box<dyn TotalEqInner + 'a> {
100 (&self.0).into_total_eq_inner()
101 }
102 fn into_total_ord_inner<'a>(&'a self) -> Box<dyn TotalOrdInner + 'a> {
103 (&self.0).into_total_ord_inner()
104 }
105
106 fn vec_hash(
107 &self,
108 random_state: PlSeedableRandomStateQuality,
109 buf: &mut Vec<u64>,
110 ) -> PolarsResult<()> {
111 self.0.vec_hash(random_state, buf)?;
112 Ok(())
113 }
114
115 fn vec_hash_combine(
116 &self,
117 build_hasher: PlSeedableRandomStateQuality,
118 hashes: &mut [u64],
119 ) -> PolarsResult<()> {
120 self.0.vec_hash_combine(build_hasher, hashes)?;
121 Ok(())
122 }
123
124 #[cfg(feature = "algorithm_group_by")]
125 unsafe fn agg_min(&self, groups: &GroupsType) -> Series {
126 self.0.agg_min(groups)
127 }
128
129 #[cfg(feature = "algorithm_group_by")]
130 unsafe fn agg_max(&self, groups: &GroupsType) -> Series {
131 self.0.agg_max(groups)
132 }
133
134 #[cfg(feature = "algorithm_group_by")]
135 unsafe fn agg_arg_min(&self, groups: &GroupsType) -> Series {
136 self.0.agg_arg_min(groups)
137 }
138
139 #[cfg(feature = "algorithm_group_by")]
140 unsafe fn agg_arg_max(&self, groups: &GroupsType) -> Series {
141 self.0.agg_arg_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 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 fn unique_id(&self) -> PolarsResult<(IdxSize, Vec<IdxSize>)> {
370 ChunkUnique::unique_id(&self.0)
371 }
372
373 fn is_null(&self) -> BooleanChunked {
374 self.0.is_null()
375 }
376
377 fn is_not_null(&self) -> BooleanChunked {
378 self.0.is_not_null()
379 }
380
381 fn reverse(&self) -> Series {
382 ChunkReverse::reverse(&self.0).into_series()
383 }
384
385 fn as_single_ptr(&mut self) -> PolarsResult<usize> {
386 self.0.as_single_ptr()
387 }
388
389 fn shift(&self, periods: i64) -> Series {
390 ChunkShift::shift(&self.0, periods).into_series()
391 }
392
393 fn sum_reduce(&self) -> PolarsResult<Scalar> {
394 Ok(ChunkAggSeries::sum_reduce(&self.0))
395 }
396 fn max_reduce(&self) -> PolarsResult<Scalar> {
397 Ok(ChunkAggSeries::max_reduce(&self.0))
398 }
399 fn min_reduce(&self) -> PolarsResult<Scalar> {
400 Ok(ChunkAggSeries::min_reduce(&self.0))
401 }
402 fn mean_reduce(&self) -> PolarsResult<Scalar> {
403 Ok(Scalar::new(DataType::Float64, self.mean().into()))
404 }
405 fn median_reduce(&self) -> PolarsResult<Scalar> {
406 Ok(QuantileAggSeries::median_reduce(&self.0))
407 }
408 fn var_reduce(&self, ddof: u8) -> PolarsResult<Scalar> {
409 Ok(VarAggSeries::var_reduce(&self.0, ddof))
410 }
411 fn std_reduce(&self, ddof: u8) -> PolarsResult<Scalar> {
412 Ok(VarAggSeries::std_reduce(&self.0, ddof))
413 }
414
415 fn quantile_reduce(
416 &self,
417 quantile: f64,
418 method: QuantileMethod,
419 ) -> PolarsResult<Scalar> {
420 QuantileAggSeries::quantile_reduce(&self.0, quantile, method)
421 }
422
423 fn quantiles_reduce(
424 &self,
425 quantiles: &[f64],
426 method: QuantileMethod,
427 ) -> PolarsResult<Scalar> {
428 QuantileAggSeries::quantiles_reduce(&self.0, quantiles, method)
429 }
430
431 #[cfg(feature = "bitwise")]
432 fn and_reduce(&self) -> PolarsResult<Scalar> {
433 let dt = <$pdt as PolarsDataType>::get_static_dtype();
434 let av = self.0.and_reduce().map_or(AnyValue::Null, Into::into);
435
436 Ok(Scalar::new(dt, av))
437 }
438
439 #[cfg(feature = "bitwise")]
440 fn or_reduce(&self) -> PolarsResult<Scalar> {
441 let dt = <$pdt as PolarsDataType>::get_static_dtype();
442 let av = self.0.or_reduce().map_or(AnyValue::Null, Into::into);
443
444 Ok(Scalar::new(dt, av))
445 }
446
447 #[cfg(feature = "bitwise")]
448 fn xor_reduce(&self) -> PolarsResult<Scalar> {
449 let dt = <$pdt as PolarsDataType>::get_static_dtype();
450 let av = self.0.xor_reduce().map_or(AnyValue::Null, Into::into);
451
452 Ok(Scalar::new(dt, av))
453 }
454
455 #[cfg(feature = "approx_unique")]
456 fn approx_n_unique(&self) -> PolarsResult<IdxSize> {
457 Ok(ChunkApproxNUnique::approx_n_unique(&self.0))
458 }
459
460 fn clone_inner(&self) -> Arc<dyn SeriesTrait> {
461 Arc::new(SeriesWrap(Clone::clone(&self.0)))
462 }
463
464 fn find_validity_mismatch(&self, other: &Series, idxs: &mut Vec<IdxSize>) {
465 self.0.find_validity_mismatch(other, idxs)
466 }
467
468 #[cfg(feature = "checked_arithmetic")]
469 fn checked_div(&self, rhs: &Series) -> PolarsResult<Series> {
470 self.0.checked_div(rhs)
471 }
472
473 fn as_any(&self) -> &dyn Any {
474 &self.0
475 }
476
477 fn as_any_mut(&mut self) -> &mut dyn Any {
478 &mut self.0
479 }
480
481 fn as_phys_any(&self) -> &dyn Any {
482 &self.0
483 }
484
485 fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
486 self as _
487 }
488 }
489 };
490}
491
492#[cfg(feature = "dtype-u8")]
493impl_dyn_series!(UInt8Chunked, UInt8Type);
494#[cfg(feature = "dtype-u16")]
495impl_dyn_series!(UInt16Chunked, UInt16Type);
496impl_dyn_series!(UInt32Chunked, UInt32Type);
497impl_dyn_series!(UInt64Chunked, UInt64Type);
498#[cfg(feature = "dtype-u128")]
499impl_dyn_series!(UInt128Chunked, UInt128Type);
500#[cfg(feature = "dtype-i8")]
501impl_dyn_series!(Int8Chunked, Int8Type);
502#[cfg(feature = "dtype-i16")]
503impl_dyn_series!(Int16Chunked, Int16Type);
504impl_dyn_series!(Int32Chunked, Int32Type);
505impl_dyn_series!(Int64Chunked, Int64Type);
506#[cfg(feature = "dtype-i128")]
507impl_dyn_series!(Int128Chunked, Int128Type);
508
509impl<T: PolarsNumericType> private::PrivateSeriesNumeric for SeriesWrap<ChunkedArray<T>> {
510 fn bit_repr(&self) -> Option<BitRepr> {
511 Some(self.0.to_bit_repr())
512 }
513}
514
515impl private::PrivateSeriesNumeric for SeriesWrap<StringChunked> {
516 fn bit_repr(&self) -> Option<BitRepr> {
517 None
518 }
519}
520impl private::PrivateSeriesNumeric for SeriesWrap<BinaryChunked> {
521 fn bit_repr(&self) -> Option<BitRepr> {
522 None
523 }
524}
525impl private::PrivateSeriesNumeric for SeriesWrap<BinaryOffsetChunked> {
526 fn bit_repr(&self) -> Option<BitRepr> {
527 None
528 }
529}
530impl private::PrivateSeriesNumeric for SeriesWrap<ListChunked> {
531 fn bit_repr(&self) -> Option<BitRepr> {
532 None
533 }
534}
535#[cfg(feature = "dtype-array")]
536impl private::PrivateSeriesNumeric for SeriesWrap<ArrayChunked> {
537 fn bit_repr(&self) -> Option<BitRepr> {
538 None
539 }
540}
541impl private::PrivateSeriesNumeric for SeriesWrap<BooleanChunked> {
542 fn bit_repr(&self) -> Option<BitRepr> {
543 let repr = self
544 .0
545 .cast_with_options(&DataType::UInt32, CastOptions::NonStrict)
546 .unwrap()
547 .u32()
548 .unwrap()
549 .clone();
550
551 Some(BitRepr::U32(repr))
552 }
553}