1#![allow(unsafe_op_in_unsafe_fn)]
2use crate::chunked_array::flags::StatisticsFlags;
4pub use crate::prelude::ChunkCompareEq;
5use crate::prelude::*;
6use crate::{HEAD_DEFAULT_LENGTH, TAIL_DEFAULT_LENGTH};
7
8macro_rules! invalid_operation_panic {
9 ($op:ident, $s:expr) => {
10 panic!(
11 "`{}` operation not supported for dtype `{}`",
12 stringify!($op),
13 $s._dtype()
14 )
15 };
16}
17
18pub mod amortized_iter;
19mod any_value;
20pub mod arithmetic;
21pub mod arrow_export;
22pub mod builder;
23
24mod comparison;
25mod from;
26pub mod implementations;
27pub(crate) mod iterator;
28pub mod ops;
29#[cfg(feature = "proptest")]
30pub mod proptest;
31mod series_trait;
32
33use std::borrow::Cow;
34use std::hash::{Hash, Hasher};
35use std::ops::Deref;
36
37pub use from::*;
38pub use iterator::SeriesIter;
39use num_traits::NumCast;
40use polars_arrow::compute::aggregate::estimated_bytes_size;
41use polars_error::feature_gated;
42use polars_utils::broadcast::BroadcastLength;
43use polars_utils::float::IsFloat;
44pub use series_trait::{IsSorted, *};
45
46use crate::chunked_array::cast::CastOptions;
47use crate::runtime::RAYON;
48#[cfg(feature = "zip_with")]
49use crate::series::arithmetic::coerce_lhs_rhs;
50use crate::utils::{Wrap, handle_casting_failures, materialize_dyn_int};
51
52#[derive(Clone)]
150#[must_use]
151pub struct Series(pub Arc<dyn SeriesTrait>);
152
153impl PartialEq for Wrap<Series> {
154 fn eq(&self, other: &Self) -> bool {
155 self.0.equals_missing(other)
156 }
157}
158
159impl Eq for Wrap<Series> {}
160
161impl Hash for Wrap<Series> {
162 fn hash<H: Hasher>(&self, state: &mut H) {
163 self.dtype().hash(state);
164 self.len().hash(state);
165
166 for av in self.iter() {
167 av.hash(state);
168 }
169 }
170}
171
172impl Series {
173 pub fn new_empty(name: PlSmallStr, dtype: &DataType) -> Series {
175 Series::full_null(name, 0, dtype)
176 }
177
178 pub fn clear(&self) -> Series {
179 if self.is_empty() {
180 self.clone()
181 } else {
182 match self.dtype() {
183 #[cfg(feature = "object")]
184 DataType::Object(_) => self
185 .take(&ChunkedArray::<IdxType>::new_vec(PlSmallStr::EMPTY, vec![]))
186 .unwrap(),
187 dt => Series::new_empty(self.name().clone(), dt),
188 }
189 }
190 }
191
192 #[doc(hidden)]
193 pub fn _get_inner_mut(&mut self) -> &mut dyn SeriesTrait {
194 if Arc::weak_count(&self.0) + Arc::strong_count(&self.0) != 1 {
195 self.0 = self.0.clone_inner();
196 }
197 Arc::get_mut(&mut self.0).expect("implementation error")
198 }
199
200 pub fn take_inner<T: PolarsPhysicalType>(self) -> ChunkedArray<T> {
202 let arc_any = self.0.as_arc_any();
203 let downcast = arc_any
204 .downcast::<implementations::SeriesWrap<ChunkedArray<T>>>()
205 .unwrap();
206
207 match Arc::try_unwrap(downcast) {
208 Ok(ca) => ca.0,
209 Err(ca) => ca.as_ref().as_ref().clone(),
210 }
211 }
212
213 #[inline]
215 pub fn array_ref(&self, chunk_idx: usize) -> &ArrayRef {
216 &self.chunks()[chunk_idx] as &ArrayRef
217 }
218
219 pub unsafe fn chunks_mut(&mut self) -> &mut Vec<ArrayRef> {
223 #[allow(unused_mut)]
224 let mut ca = self._get_inner_mut();
225 ca.chunks_mut()
226 }
227
228 pub fn into_chunks(mut self) -> Vec<ArrayRef> {
229 let ca = self._get_inner_mut();
230 let chunks = std::mem::take(unsafe { ca.chunks_mut() });
231 ca.compute_len();
232 chunks
233 }
234
235 pub fn select_chunk(&self, i: usize) -> Self {
237 let mut new = self.clear();
238 let mut flags = self.get_flags();
239
240 use StatisticsFlags as F;
241 flags &= F::IS_SORTED_ANY | F::CAN_FAST_EXPLODE_LIST;
242
243 let mut_new = new._get_inner_mut();
245 let chunks = unsafe { mut_new.chunks_mut() };
246 let chunk = self.chunks()[i].clone();
247 chunks.clear();
248 chunks.push(chunk);
249 mut_new.compute_len();
250 mut_new._set_flags(flags);
251 new
252 }
253
254 pub fn is_sorted_flag(&self) -> IsSorted {
255 if self.len() <= 1 {
256 return IsSorted::Ascending;
257 }
258 self.get_flags().is_sorted()
259 }
260
261 pub fn set_sorted_flag(&mut self, sorted: IsSorted) {
262 let mut flags = self.get_flags();
263 flags.set_sorted(sorted);
264 self.set_flags(flags);
265 }
266
267 pub(crate) fn clear_flags(&mut self) {
268 self.set_flags(StatisticsFlags::empty());
269 }
270 pub fn get_flags(&self) -> StatisticsFlags {
271 self.0._get_flags()
272 }
273
274 pub(crate) fn set_flags(&mut self, flags: StatisticsFlags) {
275 self._get_inner_mut()._set_flags(flags)
276 }
277
278 pub fn into_frame(self) -> DataFrame {
279 unsafe { DataFrame::new_unchecked(self.len(), vec![self.into()]) }
281 }
282
283 pub fn rename(&mut self, name: PlSmallStr) -> &mut Series {
285 self._get_inner_mut().rename(name);
286 self
287 }
288
289 pub fn with_name(mut self, name: PlSmallStr) -> Series {
291 self.rename(name);
292 self
293 }
294
295 pub fn from_arrow_chunks(name: PlSmallStr, arrays: Vec<ArrayRef>) -> PolarsResult<Series> {
296 Self::try_from((name, arrays))
297 }
298
299 pub fn from_arrow(name: PlSmallStr, array: ArrayRef) -> PolarsResult<Series> {
300 Self::try_from((name, array))
301 }
302
303 pub fn shrink_to_fit(&mut self) {
305 self._get_inner_mut().shrink_to_fit()
306 }
307
308 pub fn append(&mut self, other: &Series) -> PolarsResult<&mut Self> {
312 let must_cast = other.dtype().matches_schema_type(self.dtype())?;
313 if must_cast {
314 let other = other.cast(self.dtype())?;
315 self.append_owned(other)?;
316 } else {
317 self._get_inner_mut().append(other)?;
318 }
319 Ok(self)
320 }
321
322 pub fn append_owned(&mut self, other: Series) -> PolarsResult<&mut Self> {
326 let must_cast = other.dtype().matches_schema_type(self.dtype())?;
327 if must_cast {
328 let other = other.cast(self.dtype())?;
329 self._get_inner_mut().append_owned(other)?;
330 } else {
331 self._get_inner_mut().append_owned(other)?;
332 }
333 Ok(self)
334 }
335
336 pub fn compute_len(&mut self) {
338 self._get_inner_mut().compute_len()
339 }
340
341 pub fn extend(&mut self, other: &Series) -> PolarsResult<&mut Self> {
345 let must_cast = other.dtype().matches_schema_type(self.dtype())?;
346 if must_cast {
347 let other = other.cast(self.dtype())?;
348 self._get_inner_mut().extend(&other)?;
349 } else {
350 self._get_inner_mut().extend(other)?;
351 }
352 Ok(self)
353 }
354
355 pub fn broadcast_to(&self, length: usize) -> PolarsResult<Cow<'_, Self>> {
359 let len = self.len();
360 if len == length {
361 Ok(Cow::Borrowed(self))
362 } else if len == 1 {
363 Ok(Cow::Owned(self.new_from_index(0, length)))
364 } else {
365 polars_bail!(
366 ShapeMismatch: "can't broadcast Series '{}' of length {len} to length {length}",
367 self.name()
368 );
369 }
370 }
371
372 pub fn broadcast_in_place_to(&mut self, length: usize) -> PolarsResult<()> {
374 if let Cow::Owned(new) = self.broadcast_to(length)? {
375 *self = new;
376 }
377 Ok(())
378 }
379
380 pub fn broadcast_owned_to(mut self, length: usize) -> PolarsResult<Self> {
382 self.broadcast_in_place_to(length)?;
383 Ok(self)
384 }
385
386 pub fn sort(&self, sort_options: SortOptions) -> PolarsResult<Self> {
402 self.sort_with(sort_options)
403 }
404
405 pub fn as_single_ptr(&mut self) -> PolarsResult<usize> {
407 self._get_inner_mut().as_single_ptr()
408 }
409
410 pub fn cast(&self, dtype: &DataType) -> PolarsResult<Self> {
411 self.cast_with_options(dtype, CastOptions::NonStrict)
412 }
413
414 pub fn cast_with_options(&self, dtype: &DataType, options: CastOptions) -> PolarsResult<Self> {
416 let slf = self
417 .trim_lists_to_normalized_offsets()
418 .map_or(Cow::Borrowed(self), Cow::Owned);
419 let slf = slf.propagate_nulls().map_or(slf, Cow::Owned);
420
421 use DataType as D;
422 let do_clone = match dtype {
423 D::Unknown(UnknownKind::Any) => true,
424 D::Unknown(UnknownKind::Int(_)) if slf.dtype().is_integer() => true,
425 D::Unknown(UnknownKind::Float) if slf.dtype().is_float() => true,
426 D::Unknown(UnknownKind::Str)
427 if slf.dtype().is_string() | slf.dtype().is_categorical() =>
428 {
429 true
430 },
431 dt if (dt.is_primitive() || dt.is_extension()) && dt == slf.dtype() => true,
432 _ => false,
433 };
434
435 if do_clone {
436 return Ok(slf.into_owned());
437 }
438
439 pub fn cast_dtype(dtype: &DataType) -> Option<DataType> {
440 match dtype {
441 D::Unknown(UnknownKind::Int(v)) => Some(materialize_dyn_int(*v).dtype()),
442 D::Unknown(UnknownKind::Float) => Some(DataType::Float64),
443 D::Unknown(UnknownKind::Str) => Some(DataType::String),
444 D::List(inner) => cast_dtype(inner.as_ref()).map(Box::new).map(D::List),
446 #[cfg(feature = "dtype-struct")]
447 D::Struct(fields) => {
448 let mut field_iter = fields.iter().enumerate();
451 let mut new_fields = loop {
452 let (i, field) = field_iter.next()?;
453
454 if let Some(dtype) = cast_dtype(&field.dtype) {
455 let mut new_fields = Vec::with_capacity(fields.len());
456 new_fields.extend(fields.iter().take(i).cloned());
457 new_fields.push(Field {
458 name: field.name.clone(),
459 dtype,
460 });
461 break new_fields;
462 }
463 };
464
465 new_fields.extend(fields.iter().skip(new_fields.len()).cloned().map(|field| {
466 let dtype = cast_dtype(&field.dtype).unwrap_or(field.dtype);
467 Field {
468 name: field.name,
469 dtype,
470 }
471 }));
472
473 Some(D::Struct(new_fields))
474 },
475 _ => None,
476 }
477 }
478
479 let mut casted = cast_dtype(dtype);
480 if dtype.is_list() && dtype.inner_dtype().is_some_and(|dt| dt.is_null()) {
481 if let Some(from_inner_dtype) = slf.dtype().inner_dtype() {
482 casted = Some(DataType::List(Box::new(from_inner_dtype.clone())));
483 }
484 }
485 let dtype = match casted {
486 None => dtype,
487 Some(ref dtype) => dtype,
488 };
489
490 let len = slf.len();
492 if slf.null_count() == len {
493 return Ok(Series::full_null(slf.name().clone(), len, dtype));
494 }
495
496 let new_options = match options {
497 CastOptions::Strict if !dtype.is_nested() => CastOptions::NonStrict,
500 opt => opt,
501 };
502
503 let out = slf.0.cast(dtype, new_options)?;
504 if options.is_strict() {
505 handle_casting_failures(slf.as_ref(), &out)?;
506 }
507 Ok(out)
508 }
509
510 pub unsafe fn cast_unchecked(&self, dtype: &DataType) -> PolarsResult<Self> {
516 match self.dtype() {
517 #[cfg(feature = "dtype-struct")]
518 DataType::Struct(_) => self.struct_().unwrap().cast_unchecked(dtype),
519 DataType::List(_) => self.list().unwrap().cast_unchecked(dtype),
520 dt if dt.is_primitive_numeric() => {
521 with_match_physical_numeric_polars_type!(dt, |$T| {
522 let ca: &ChunkedArray<$T> = self.as_ref().as_ref().as_ref();
523 ca.cast_unchecked(dtype)
524 })
525 },
526 DataType::Binary => self.binary().unwrap().cast_unchecked(dtype),
527 _ => self.cast_with_options(dtype, CastOptions::Overflowing),
528 }
529 }
530
531 pub unsafe fn from_physical_unchecked(&self, dtype: &DataType) -> PolarsResult<Self> {
545 debug_assert!(!self.dtype().is_logical(), "{:?}", self.dtype());
546
547 if self.dtype() == dtype {
548 return Ok(self.clone());
549 }
550
551 use DataType as D;
552 match (self.dtype(), dtype) {
553 #[cfg(feature = "dtype-decimal")]
554 (D::Int128, D::Decimal(precision, scale)) => {
555 let ca = self.i128().unwrap();
556 Ok(ca
557 .clone()
558 .into_decimal_unchecked(*precision, *scale)
559 .into_series())
560 },
561
562 #[cfg(feature = "dtype-categorical")]
563 (phys, D::Categorical(cats, _)) if &cats.physical().dtype() == phys => {
564 with_match_categorical_physical_type!(cats.physical(), |$C| {
565 type CA = ChunkedArray<<$C as PolarsCategoricalType>::PolarsPhysical>;
566 let ca = self.as_ref().as_any().downcast_ref::<CA>().unwrap();
567 Ok(CategoricalChunked::<$C>::from_cats_and_dtype_unchecked(
568 ca.clone(),
569 dtype.clone(),
570 )
571 .into_series())
572 })
573 },
574 #[cfg(feature = "dtype-categorical")]
575 (phys, D::Enum(fcats, _)) if &fcats.physical().dtype() == phys => {
576 with_match_categorical_physical_type!(fcats.physical(), |$C| {
577 type CA = ChunkedArray<<$C as PolarsCategoricalType>::PolarsPhysical>;
578 let ca = self.as_ref().as_any().downcast_ref::<CA>().unwrap();
579 Ok(CategoricalChunked::<$C>::from_cats_and_dtype_unchecked(
580 ca.clone(),
581 dtype.clone(),
582 )
583 .into_series())
584 })
585 },
586
587 (D::Int32, D::Date) => feature_gated!("dtype-date", Ok(self.clone().into_date())),
588 (D::Int64, D::Datetime(tu, tz)) => feature_gated!(
589 "dtype-datetime",
590 Ok(self.clone().into_datetime(*tu, tz.clone()))
591 ),
592 (D::Int64, D::Duration(tu)) => {
593 feature_gated!("dtype-duration", Ok(self.clone().into_duration(*tu)))
594 },
595 (D::Int64, D::Time) => feature_gated!("dtype-time", Ok(self.clone().into_time())),
596
597 (D::List(_), D::List(to)) => unsafe {
598 self.list()
599 .unwrap()
600 .from_physical_unchecked(to.as_ref().clone())
601 .map(|ca| ca.into_series())
602 },
603 #[cfg(feature = "dtype-array")]
604 (D::Array(_, lw), D::Array(to, rw)) if lw == rw => unsafe {
605 self.array()
606 .unwrap()
607 .from_physical_unchecked(to.as_ref().clone())
608 .map(|ca| ca.into_series())
609 },
610 #[cfg(feature = "dtype-struct")]
611 (D::Struct(_), D::Struct(to)) => unsafe {
612 self.struct_()
613 .unwrap()
614 .from_physical_unchecked(to.as_slice())
615 .map(|ca| ca.into_series())
616 },
617
618 #[cfg(feature = "dtype-map")]
619 (D::List(_), D::Map(_, _)) => {
620 use crate::chunked_array::logical::ensure_live_entries_non_null;
621
622 let storage = self.from_physical_unchecked(&dtype.map_storage_dtype().unwrap())?;
623 ensure_live_entries_non_null(storage.list().unwrap())?;
626 Ok(MapChunked::from_storage_unchecked(dtype.clone(), storage).into_series())
627 },
628 #[cfg(feature = "dtype-extension")]
629 (_, D::Extension(typ, storage)) => {
630 let storage_series = self.from_physical_unchecked(storage.as_ref())?;
631 let ext = ExtensionChunked::from_storage(typ.clone(), storage_series);
632 Ok(ext.into_series())
633 },
634
635 _ => panic!("invalid from_physical({dtype:?}) for {:?}", self.dtype()),
636 }
637 }
638
639 #[cfg(feature = "dtype-extension")]
640 pub fn into_extension(self, typ: ExtensionTypeInstance) -> Series {
641 assert!(!self.dtype().is_extension());
642 let ext = ExtensionChunked::from_storage(typ, self);
643 ext.into_series()
644 }
645
646 pub fn to_float(&self) -> PolarsResult<Series> {
648 match self.dtype() {
649 DataType::Float32 | DataType::Float64 => Ok(self.clone()),
650 _ => self.cast_with_options(&DataType::Float64, CastOptions::Overflowing),
651 }
652 }
653
654 pub fn sum<T>(&self) -> PolarsResult<T>
661 where
662 T: NumCast + IsFloat,
663 {
664 let sum = self.sum_reduce()?;
665 let sum = sum.value().extract().unwrap();
666 Ok(sum)
667 }
668
669 pub fn min<T>(&self) -> PolarsResult<Option<T>>
672 where
673 T: NumCast + IsFloat,
674 {
675 let min = self.min_reduce()?;
676 let min = min.value().extract::<T>();
677 Ok(min)
678 }
679
680 pub fn max<T>(&self) -> PolarsResult<Option<T>>
683 where
684 T: NumCast + IsFloat,
685 {
686 let max = self.max_reduce()?;
687 let max = max.value().extract::<T>();
688 Ok(max)
689 }
690
691 pub fn explode(&self, options: ExplodeOptions) -> PolarsResult<Series> {
693 match self.dtype() {
694 DataType::List(_) => self.list().unwrap().explode(options),
695 #[cfg(feature = "dtype-array")]
696 DataType::Array(_, _) => self.array().unwrap().explode(options),
697 _ => Ok(self.clone()),
698 }
699 }
700
701 pub fn is_nan(&self) -> PolarsResult<BooleanChunked> {
703 match self.dtype() {
704 #[cfg(feature = "dtype-f16")]
705 DataType::Float16 => Ok(self.f16().unwrap().is_nan()),
706 DataType::Float32 => Ok(self.f32().unwrap().is_nan()),
707 DataType::Float64 => Ok(self.f64().unwrap().is_nan()),
708 DataType::Null => Ok(BooleanChunked::full_null(self.name().clone(), self.len())),
709 dt if dt.is_primitive_numeric() => {
710 let arr = BooleanArray::full(self.len(), false, ArrowDataType::Boolean)
711 .with_validity(self.rechunk_validity());
712 Ok(BooleanChunked::with_chunk(self.name().clone(), arr))
713 },
714 _ => polars_bail!(opq = is_nan, self.dtype()),
715 }
716 }
717
718 pub fn is_not_nan(&self) -> PolarsResult<BooleanChunked> {
720 match self.dtype() {
721 #[cfg(feature = "dtype-f16")]
722 DataType::Float16 => Ok(self.f16().unwrap().is_not_nan()),
723 DataType::Float32 => Ok(self.f32().unwrap().is_not_nan()),
724 DataType::Float64 => Ok(self.f64().unwrap().is_not_nan()),
725 DataType::Null => Ok(BooleanChunked::full_null(self.name().clone(), self.len())),
726 dt if dt.is_primitive_numeric() => {
727 let arr = BooleanArray::full(self.len(), true, ArrowDataType::Boolean)
728 .with_validity(self.rechunk_validity());
729 Ok(BooleanChunked::with_chunk(self.name().clone(), arr))
730 },
731 _ => polars_bail!(opq = is_not_nan, self.dtype()),
732 }
733 }
734
735 pub fn is_finite(&self) -> PolarsResult<BooleanChunked> {
737 match self.dtype() {
738 #[cfg(feature = "dtype-f16")]
739 DataType::Float16 => Ok(self.f16().unwrap().is_finite()),
740 DataType::Float32 => Ok(self.f32().unwrap().is_finite()),
741 DataType::Float64 => Ok(self.f64().unwrap().is_finite()),
742 DataType::Null => Ok(BooleanChunked::full_null(self.name().clone(), self.len())),
743 dt if dt.is_primitive_numeric() => {
744 let arr = BooleanArray::full(self.len(), true, ArrowDataType::Boolean)
745 .with_validity(self.rechunk_validity());
746 Ok(BooleanChunked::with_chunk(self.name().clone(), arr))
747 },
748 _ => polars_bail!(opq = is_finite, self.dtype()),
749 }
750 }
751
752 pub fn is_infinite(&self) -> PolarsResult<BooleanChunked> {
754 match self.dtype() {
755 #[cfg(feature = "dtype-f16")]
756 DataType::Float16 => Ok(self.f16().unwrap().is_infinite()),
757 DataType::Float32 => Ok(self.f32().unwrap().is_infinite()),
758 DataType::Float64 => Ok(self.f64().unwrap().is_infinite()),
759 DataType::Null => Ok(BooleanChunked::full_null(self.name().clone(), self.len())),
760 dt if dt.is_primitive_numeric() => {
761 let arr = BooleanArray::full(self.len(), false, ArrowDataType::Boolean)
762 .with_validity(self.rechunk_validity());
763 Ok(BooleanChunked::with_chunk(self.name().clone(), arr))
764 },
765 _ => polars_bail!(opq = is_infinite, self.dtype()),
766 }
767 }
768
769 #[cfg(feature = "zip_with")]
773 pub fn zip_with(&self, mask: &BooleanChunked, other: &Series) -> PolarsResult<Series> {
774 let (lhs, rhs) = coerce_lhs_rhs(self, other)?;
775 lhs.zip_with_same_type(mask, rhs.as_ref())
776 }
777
778 pub fn to_physical_repr(&self) -> Cow<'_, Series> {
792 use DataType::*;
793 match self.dtype() {
794 #[cfg(feature = "dtype-date")]
797 Date => Cow::Owned(self.date().unwrap().phys.clone().into_series()),
798 #[cfg(feature = "dtype-datetime")]
799 Datetime(_, _) => Cow::Owned(self.datetime().unwrap().phys.clone().into_series()),
800 #[cfg(feature = "dtype-duration")]
801 Duration(_) => Cow::Owned(self.duration().unwrap().phys.clone().into_series()),
802 #[cfg(feature = "dtype-time")]
803 Time => Cow::Owned(self.time().unwrap().phys.clone().into_series()),
804 #[cfg(feature = "dtype-categorical")]
805 dt @ (Categorical(_, _) | Enum(_, _)) => {
806 with_match_categorical_physical_type!(dt.cat_physical().unwrap(), |$C| {
807 let ca = self.cat::<$C>().unwrap();
808 Cow::Owned(ca.physical().clone().into_series())
809 })
810 },
811 #[cfg(feature = "dtype-decimal")]
812 Decimal(_, _) => Cow::Owned(self.decimal().unwrap().phys.clone().into_series()),
813 List(_) => match self.list().unwrap().to_physical_repr() {
814 Cow::Borrowed(_) => Cow::Borrowed(self),
815 Cow::Owned(ca) => Cow::Owned(ca.into_series()),
816 },
817 #[cfg(feature = "dtype-array")]
818 Array(_, _) => match self.array().unwrap().to_physical_repr() {
819 Cow::Borrowed(_) => Cow::Borrowed(self),
820 Cow::Owned(ca) => Cow::Owned(ca.into_series()),
821 },
822 #[cfg(feature = "dtype-struct")]
823 Struct(_) => match self.struct_().unwrap().to_physical_repr() {
824 Cow::Borrowed(_) => Cow::Borrowed(self),
825 Cow::Owned(ca) => Cow::Owned(ca.into_series()),
826 },
827 #[cfg(feature = "dtype-map")]
828 Map(_, _) => match self.map().unwrap().storage().to_physical_repr() {
829 Cow::Borrowed(storage) => Cow::Owned(storage.clone()),
830 Cow::Owned(storage) => Cow::Owned(storage),
831 },
832 #[cfg(feature = "dtype-extension")]
833 Extension(_, _) => self.ext().unwrap().storage().to_physical_repr(),
834 _ => Cow::Borrowed(self),
835 }
836 }
837
838 pub fn to_storage(&self) -> &Series {
841 #[cfg(feature = "dtype-extension")]
842 {
843 if let DataType::Extension(_, _) = self.dtype() {
844 return self.ext().unwrap().storage();
845 }
846 }
847 self
848 }
849
850 pub fn gather_every(&self, n: usize, offset: usize) -> PolarsResult<Series> {
852 polars_ensure!(n > 0, ComputeError: "cannot perform gather every for `n=0`");
853 let idx = ((offset as IdxSize)..self.len() as IdxSize)
854 .step_by(n)
855 .collect_ca(PlSmallStr::EMPTY);
856 Ok(unsafe { self.take_unchecked(&idx) })
858 }
859
860 #[cfg(feature = "dot_product")]
861 pub fn dot(&self, other: &Series) -> PolarsResult<f64> {
862 std::ops::Mul::mul(self, other)?.sum::<f64>()
863 }
864
865 pub fn sum_reduce(&self) -> PolarsResult<Scalar> {
872 self.0.sum_reduce()
873 }
874
875 pub fn mean_reduce(&self) -> PolarsResult<Scalar> {
878 self.0.mean_reduce()
879 }
880
881 pub fn product(&self) -> PolarsResult<Scalar> {
886 #[cfg(feature = "product")]
887 {
888 use DataType::*;
889 match self.dtype() {
890 Boolean => self.cast(&DataType::Int64).unwrap().product(),
891 Int8 | UInt8 | Int16 | UInt16 | Int32 | UInt32 => {
892 let s = self.cast(&Int64).unwrap();
893 s.product()
894 },
895 Int64 => Ok(self.i64().unwrap().prod_reduce()),
896 UInt64 => Ok(self.u64().unwrap().prod_reduce()),
897 #[cfg(feature = "dtype-i128")]
898 Int128 => Ok(self.i128().unwrap().prod_reduce()),
899 #[cfg(feature = "dtype-u128")]
900 UInt128 => Ok(self.u128().unwrap().prod_reduce()),
901 #[cfg(feature = "dtype-f16")]
902 Float16 => Ok(self.f16().unwrap().prod_reduce()),
903 Float32 => Ok(self.f32().unwrap().prod_reduce()),
904 Float64 => Ok(self.f64().unwrap().prod_reduce()),
905 #[cfg(feature = "dtype-decimal")]
906 Decimal(..) => Ok(self.decimal().unwrap().prod_reduce()),
907 dt => {
908 polars_bail!(InvalidOperation: "`product` operation not supported for dtype `{dt}`")
909 },
910 }
911 }
912 #[cfg(not(feature = "product"))]
913 {
914 panic!("activate 'product' feature")
915 }
916 }
917
918 pub fn strict_cast(&self, dtype: &DataType) -> PolarsResult<Series> {
920 self.cast_with_options(dtype, CastOptions::Strict)
921 }
922
923 #[cfg(feature = "dtype-decimal")]
924 pub fn into_decimal(self, precision: usize, scale: usize) -> PolarsResult<Series> {
925 match self.dtype() {
926 DataType::Int128 => Ok(self
927 .i128()
928 .unwrap()
929 .clone()
930 .into_decimal(precision, scale)?
931 .into_series()),
932 DataType::Decimal(cur_prec, cur_scale)
933 if scale == *cur_scale && precision >= *cur_prec =>
934 {
935 Ok(self)
936 },
937 dt => panic!("into_decimal({precision:?}, {scale}) not implemented for {dt:?}"),
938 }
939 }
940
941 #[cfg(feature = "dtype-time")]
942 pub fn into_time(self) -> Series {
943 match self.dtype() {
944 DataType::Int64 => self.i64().unwrap().clone().into_time().into_series(),
945 DataType::Time => self
946 .time()
947 .unwrap()
948 .physical()
949 .clone()
950 .into_time()
951 .into_series(),
952 dt => panic!("date not implemented for {dt:?}"),
953 }
954 }
955
956 pub fn into_date(self) -> Series {
957 #[cfg(not(feature = "dtype-date"))]
958 {
959 panic!("activate feature dtype-date")
960 }
961 #[cfg(feature = "dtype-date")]
962 match self.dtype() {
963 DataType::Int32 => self.i32().unwrap().clone().into_date().into_series(),
964 DataType::Date => self
965 .date()
966 .unwrap()
967 .physical()
968 .clone()
969 .into_date()
970 .into_series(),
971 dt => panic!("date not implemented for {dt:?}"),
972 }
973 }
974
975 #[allow(unused_variables)]
976 pub fn into_datetime(self, timeunit: TimeUnit, tz: Option<TimeZone>) -> Series {
977 #[cfg(not(feature = "dtype-datetime"))]
978 {
979 panic!("activate feature dtype-datetime")
980 }
981
982 #[cfg(feature = "dtype-datetime")]
983 match self.dtype() {
984 DataType::Int64 => self
985 .i64()
986 .unwrap()
987 .clone()
988 .into_datetime(timeunit, tz)
989 .into_series(),
990 DataType::Datetime(_, _) => self
991 .datetime()
992 .unwrap()
993 .physical()
994 .clone()
995 .into_datetime(timeunit, tz)
996 .into_series(),
997 dt => panic!("into_datetime not implemented for {dt:?}"),
998 }
999 }
1000
1001 #[allow(unused_variables)]
1002 pub fn into_duration(self, timeunit: TimeUnit) -> Series {
1003 #[cfg(not(feature = "dtype-duration"))]
1004 {
1005 panic!("activate feature dtype-duration")
1006 }
1007 #[cfg(feature = "dtype-duration")]
1008 match self.dtype() {
1009 DataType::Int64 => self
1010 .i64()
1011 .unwrap()
1012 .clone()
1013 .into_duration(timeunit)
1014 .into_series(),
1015 DataType::Duration(_) => self
1016 .duration()
1017 .unwrap()
1018 .physical()
1019 .clone()
1020 .into_duration(timeunit)
1021 .into_series(),
1022 dt => panic!("into_duration not implemented for {dt:?}"),
1023 }
1024 }
1025
1026 pub fn str_value(&self, index: usize) -> PolarsResult<Cow<'_, str>> {
1028 Ok(self.0.get(index)?.str_value())
1029 }
1030 pub fn head(&self, length: Option<usize>) -> Series {
1032 let len = length.unwrap_or(HEAD_DEFAULT_LENGTH);
1033 self.slice(0, std::cmp::min(len, self.len()))
1034 }
1035
1036 pub fn tail(&self, length: Option<usize>) -> Series {
1038 let len = length.unwrap_or(TAIL_DEFAULT_LENGTH);
1039 let len = std::cmp::min(len, self.len());
1040 self.slice(-(len as i64), len)
1041 }
1042
1043 pub fn unique_stable(&self) -> PolarsResult<Series> {
1046 let idx = self.arg_unique()?;
1047 unsafe { Ok(self.take_unchecked(&idx)) }
1049 }
1050
1051 pub fn try_idx(&self) -> Option<&IdxCa> {
1052 #[cfg(feature = "bigidx")]
1053 {
1054 self.try_u64()
1055 }
1056 #[cfg(not(feature = "bigidx"))]
1057 {
1058 self.try_u32()
1059 }
1060 }
1061
1062 pub fn idx(&self) -> PolarsResult<&IdxCa> {
1063 #[cfg(feature = "bigidx")]
1064 {
1065 self.u64()
1066 }
1067 #[cfg(not(feature = "bigidx"))]
1068 {
1069 self.u32()
1070 }
1071 }
1072
1073 pub fn estimated_size(&self) -> usize {
1086 let mut size = 0;
1087 match self.dtype() {
1088 #[cfg(feature = "object")]
1090 DataType::Object(_) => {
1091 let ArrowDataType::FixedSizeBinary(size) = self.chunks()[0].dtype() else {
1092 unreachable!()
1093 };
1094 return self.len() * *size;
1096 },
1097 _ => {},
1098 }
1099
1100 size += self
1101 .chunks()
1102 .iter()
1103 .map(|arr| estimated_bytes_size(&**arr))
1104 .sum::<usize>();
1105
1106 size
1107 }
1108
1109 pub fn row_encode_unordered(&self) -> PolarsResult<BinaryOffsetChunked> {
1110 row_encode::_get_rows_encoded_ca_unordered(
1111 self.name().clone(),
1112 &[self.clone().into_column()],
1113 )
1114 }
1115
1116 pub fn row_encode_ordered(
1117 &self,
1118 descending: bool,
1119 nulls_last: bool,
1120 ) -> PolarsResult<BinaryOffsetChunked> {
1121 row_encode::_get_rows_encoded_ca(
1122 self.name().clone(),
1123 &[self.clone().into_column()],
1124 &[descending],
1125 &[nulls_last],
1126 false,
1127 )
1128 }
1129}
1130
1131impl Default for Series {
1132 fn default() -> Self {
1133 NullChunked::new(PlSmallStr::EMPTY, 0).into_series()
1134 }
1135}
1136
1137impl Deref for Series {
1138 type Target = dyn SeriesTrait;
1139
1140 #[inline(always)]
1141 fn deref(&self) -> &Self::Target {
1142 self.0.as_ref()
1143 }
1144}
1145
1146impl<'a> AsRef<dyn SeriesTrait + 'a> for Series {
1147 fn as_ref(&self) -> &(dyn SeriesTrait + 'a) {
1148 self.0.as_ref()
1149 }
1150}
1151
1152impl<T: PolarsPhysicalType> AsRef<ChunkedArray<T>> for dyn SeriesTrait + '_ {
1153 fn as_ref(&self) -> &ChunkedArray<T> {
1154 let Some(ca) = self.as_any().downcast_ref::<ChunkedArray<T>>() else {
1157 panic!(
1158 "implementation error, cannot get ref {:?} from {:?}",
1159 T::get_static_dtype(),
1160 self.dtype()
1161 );
1162 };
1163
1164 ca
1165 }
1166}
1167
1168impl<T: PolarsPhysicalType> AsMut<ChunkedArray<T>> for dyn SeriesTrait + '_ {
1169 fn as_mut(&mut self) -> &mut ChunkedArray<T> {
1170 if !self.as_any_mut().is::<ChunkedArray<T>>() {
1171 panic!(
1172 "implementation error, cannot get ref {:?} from {:?}",
1173 T::get_static_dtype(),
1174 self.dtype()
1175 );
1176 }
1177
1178 self.as_any_mut().downcast_mut::<ChunkedArray<T>>().unwrap()
1181 }
1182}
1183
1184impl BroadcastLength for Series {
1185 fn _broadcast_len(&self) -> usize {
1186 self.len()
1187 }
1188
1189 fn _column_name(&self) -> Option<&str> {
1190 Some(self.name())
1191 }
1192}
1193
1194#[cfg(test)]
1195mod test {
1196 use crate::series::*;
1197
1198 #[test]
1199 fn cast() {
1200 let ar = UInt32Chunked::new("a".into(), &[1, 2]);
1201 let s = ar.into_series();
1202 let s2 = s.cast(&DataType::Int64).unwrap();
1203
1204 assert!(s2.i64().is_ok());
1205 let s2 = s.cast(&DataType::Float32).unwrap();
1206 assert!(s2.f32().is_ok());
1207 }
1208
1209 #[test]
1210 fn new_series() {
1211 let _ = Series::new("boolean series".into(), &vec![true, false, true]);
1212 let _ = Series::new("int series".into(), &[1, 2, 3]);
1213 let ca = Int32Chunked::new("a".into(), &[1, 2, 3]);
1214 let _ = ca.into_series();
1215 }
1216
1217 #[test]
1218 #[cfg(feature = "dtype-date")]
1219 fn roundtrip_list_logical_20311() {
1220 let list = ListChunked::from_chunk_iter(
1221 PlSmallStr::from_static("a"),
1222 [ListArray::new(
1223 ArrowDataType::LargeList(Box::new(ArrowField::new(
1224 LIST_VALUES_NAME,
1225 ArrowDataType::Int32,
1226 true,
1227 ))),
1228 unsafe { polars_arrow::offset::Offsets::new_unchecked(vec![0, 1]) }.into(),
1229 PrimitiveArray::new(ArrowDataType::Int32, vec![1i32].into(), None).to_boxed(),
1230 None,
1231 )],
1232 );
1233 let list = unsafe { list.from_physical_unchecked(DataType::Date) }.unwrap();
1234 assert_eq!(list.dtype(), &DataType::List(Box::new(DataType::Date)));
1235 }
1236
1237 #[test]
1238 #[cfg(feature = "dtype-struct")]
1239 fn new_series_from_empty_structs() {
1240 let dtype = DataType::Struct(vec![]);
1241 let empties = vec![AnyValue::StructOwned(Box::new((vec![], vec![]))); 3];
1242 let s = Series::from_any_values_and_dtype("".into(), &empties, &dtype, false).unwrap();
1243 assert_eq!(s.len(), 3);
1244 }
1245 #[test]
1246 fn new_series_from_arrow_primitive_array() {
1247 let array = UInt32Array::from_slice([1, 2, 3, 4, 5]);
1248 let array_ref: ArrayRef = Box::new(array);
1249
1250 let _ = Series::try_new("foo".into(), array_ref).unwrap();
1251 }
1252
1253 #[test]
1254 fn series_append() {
1255 let mut s1 = Series::new("a".into(), &[1, 2]);
1256 let s2 = Series::new("b".into(), &[3]);
1257 s1.append(&s2).unwrap();
1258 assert_eq!(s1.len(), 3);
1259
1260 let s2 = Series::new("b".into(), &[3.0]);
1262 assert!(s1.append(&s2).is_err())
1263 }
1264
1265 #[test]
1266 #[cfg(feature = "dtype-decimal")]
1267 fn series_append_decimal() {
1268 let s1 = Series::new("a".into(), &[1.1, 2.3])
1269 .cast(&DataType::Decimal(38, 2))
1270 .unwrap();
1271 let s2 = Series::new("b".into(), &[3])
1272 .cast(&DataType::Decimal(38, 0))
1273 .unwrap();
1274
1275 {
1276 let mut s1 = s1.clone();
1277 s1.append(&s2).unwrap();
1278 assert_eq!(s1.len(), 3);
1279 assert_eq!(s1.get(2).unwrap(), AnyValue::Decimal(300, 38, 2));
1280 }
1281
1282 {
1283 let mut s2 = s2;
1284 s2.extend(&s1).unwrap();
1285 assert_eq!(s2.get(2).unwrap(), AnyValue::Decimal(2, 38, 0));
1286 }
1287 }
1288
1289 #[test]
1290 fn series_slice_works() {
1291 let series = Series::new("a".into(), &[1i64, 2, 3, 4, 5]);
1292
1293 let slice_1 = series.slice(-3, 3);
1294 let slice_2 = series.slice(-5, 5);
1295 let slice_3 = series.slice(0, 5);
1296
1297 assert_eq!(slice_1.get(0).unwrap(), AnyValue::Int64(3));
1298 assert_eq!(slice_2.get(0).unwrap(), AnyValue::Int64(1));
1299 assert_eq!(slice_3.get(0).unwrap(), AnyValue::Int64(1));
1300 }
1301
1302 #[test]
1303 fn out_of_range_slice_does_not_panic() {
1304 let series = Series::new("a".into(), &[1i64, 2, 3, 4, 5]);
1305
1306 let _ = series.slice(-3, 4);
1307 let _ = series.slice(-6, 2);
1308 let _ = series.slice(4, 2);
1309 }
1310}