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