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
37use arrow::compute::aggregate::estimated_bytes_size;
38pub use from::*;
39pub use iterator::{SeriesIter, SeriesPhysIter};
40use num_traits::NumCast;
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> {
537 debug_assert!(!self.dtype().is_logical(), "{:?}", self.dtype());
538
539 if self.dtype() == dtype {
540 return Ok(self.clone());
541 }
542
543 use DataType as D;
544 match (self.dtype(), dtype) {
545 #[cfg(feature = "dtype-decimal")]
546 (D::Int128, D::Decimal(precision, scale)) => {
547 let ca = self.i128().unwrap();
548 Ok(ca
549 .clone()
550 .into_decimal_unchecked(*precision, *scale)
551 .into_series())
552 },
553
554 #[cfg(feature = "dtype-categorical")]
555 (phys, D::Categorical(cats, _)) if &cats.physical().dtype() == phys => {
556 with_match_categorical_physical_type!(cats.physical(), |$C| {
557 type CA = ChunkedArray<<$C as PolarsCategoricalType>::PolarsPhysical>;
558 let ca = self.as_ref().as_any().downcast_ref::<CA>().unwrap();
559 Ok(CategoricalChunked::<$C>::from_cats_and_dtype_unchecked(
560 ca.clone(),
561 dtype.clone(),
562 )
563 .into_series())
564 })
565 },
566 #[cfg(feature = "dtype-categorical")]
567 (phys, D::Enum(fcats, _)) if &fcats.physical().dtype() == phys => {
568 with_match_categorical_physical_type!(fcats.physical(), |$C| {
569 type CA = ChunkedArray<<$C as PolarsCategoricalType>::PolarsPhysical>;
570 let ca = self.as_ref().as_any().downcast_ref::<CA>().unwrap();
571 Ok(CategoricalChunked::<$C>::from_cats_and_dtype_unchecked(
572 ca.clone(),
573 dtype.clone(),
574 )
575 .into_series())
576 })
577 },
578
579 (D::Int32, D::Date) => feature_gated!("dtype-time", Ok(self.clone().into_date())),
580 (D::Int64, D::Datetime(tu, tz)) => feature_gated!(
581 "dtype-datetime",
582 Ok(self.clone().into_datetime(*tu, tz.clone()))
583 ),
584 (D::Int64, D::Duration(tu)) => {
585 feature_gated!("dtype-duration", Ok(self.clone().into_duration(*tu)))
586 },
587 (D::Int64, D::Time) => feature_gated!("dtype-time", Ok(self.clone().into_time())),
588
589 (D::List(_), D::List(to)) => unsafe {
590 self.list()
591 .unwrap()
592 .from_physical_unchecked(to.as_ref().clone())
593 .map(|ca| ca.into_series())
594 },
595 #[cfg(feature = "dtype-array")]
596 (D::Array(_, lw), D::Array(to, rw)) if lw == rw => unsafe {
597 self.array()
598 .unwrap()
599 .from_physical_unchecked(to.as_ref().clone())
600 .map(|ca| ca.into_series())
601 },
602 #[cfg(feature = "dtype-struct")]
603 (D::Struct(_), D::Struct(to)) => unsafe {
604 self.struct_()
605 .unwrap()
606 .from_physical_unchecked(to.as_slice())
607 .map(|ca| ca.into_series())
608 },
609
610 #[cfg(feature = "dtype-map")]
611 (D::List(_), D::Map(_, _)) => {
612 let storage = self.from_physical_unchecked(&dtype.map_storage_dtype().unwrap())?;
613 Ok(MapChunked::from_storage_unchecked(dtype.clone(), storage).into_series())
614 },
615 #[cfg(feature = "dtype-extension")]
616 (_, D::Extension(typ, storage)) => {
617 let storage_series = self.from_physical_unchecked(storage.as_ref())?;
618 let ext = ExtensionChunked::from_storage(typ.clone(), storage_series);
619 Ok(ext.into_series())
620 },
621
622 _ => panic!("invalid from_physical({dtype:?}) for {:?}", self.dtype()),
623 }
624 }
625
626 #[cfg(feature = "dtype-extension")]
627 pub fn into_extension(self, typ: ExtensionTypeInstance) -> Series {
628 assert!(!self.dtype().is_extension());
629 let ext = ExtensionChunked::from_storage(typ, self);
630 ext.into_series()
631 }
632
633 pub fn to_float(&self) -> PolarsResult<Series> {
635 match self.dtype() {
636 DataType::Float32 | DataType::Float64 => Ok(self.clone()),
637 _ => self.cast_with_options(&DataType::Float64, CastOptions::Overflowing),
638 }
639 }
640
641 pub fn sum<T>(&self) -> PolarsResult<T>
648 where
649 T: NumCast + IsFloat,
650 {
651 let sum = self.sum_reduce()?;
652 let sum = sum.value().extract().unwrap();
653 Ok(sum)
654 }
655
656 pub fn min<T>(&self) -> PolarsResult<Option<T>>
659 where
660 T: NumCast + IsFloat,
661 {
662 let min = self.min_reduce()?;
663 let min = min.value().extract::<T>();
664 Ok(min)
665 }
666
667 pub fn max<T>(&self) -> PolarsResult<Option<T>>
670 where
671 T: NumCast + IsFloat,
672 {
673 let max = self.max_reduce()?;
674 let max = max.value().extract::<T>();
675 Ok(max)
676 }
677
678 pub fn explode(&self, options: ExplodeOptions) -> PolarsResult<Series> {
680 match self.dtype() {
681 DataType::List(_) => self.list().unwrap().explode(options),
682 #[cfg(feature = "dtype-array")]
683 DataType::Array(_, _) => self.array().unwrap().explode(options),
684 _ => Ok(self.clone()),
685 }
686 }
687
688 pub fn is_nan(&self) -> PolarsResult<BooleanChunked> {
690 match self.dtype() {
691 #[cfg(feature = "dtype-f16")]
692 DataType::Float16 => Ok(self.f16().unwrap().is_nan()),
693 DataType::Float32 => Ok(self.f32().unwrap().is_nan()),
694 DataType::Float64 => Ok(self.f64().unwrap().is_nan()),
695 DataType::Null => Ok(BooleanChunked::full_null(self.name().clone(), self.len())),
696 dt if dt.is_primitive_numeric() => {
697 let arr = BooleanArray::full(self.len(), false, ArrowDataType::Boolean)
698 .with_validity(self.rechunk_validity());
699 Ok(BooleanChunked::with_chunk(self.name().clone(), arr))
700 },
701 _ => polars_bail!(opq = is_nan, self.dtype()),
702 }
703 }
704
705 pub fn is_not_nan(&self) -> PolarsResult<BooleanChunked> {
707 match self.dtype() {
708 #[cfg(feature = "dtype-f16")]
709 DataType::Float16 => Ok(self.f16().unwrap().is_not_nan()),
710 DataType::Float32 => Ok(self.f32().unwrap().is_not_nan()),
711 DataType::Float64 => Ok(self.f64().unwrap().is_not_nan()),
712 DataType::Null => Ok(BooleanChunked::full_null(self.name().clone(), self.len())),
713 dt if dt.is_primitive_numeric() => {
714 let arr = BooleanArray::full(self.len(), true, ArrowDataType::Boolean)
715 .with_validity(self.rechunk_validity());
716 Ok(BooleanChunked::with_chunk(self.name().clone(), arr))
717 },
718 _ => polars_bail!(opq = is_not_nan, self.dtype()),
719 }
720 }
721
722 pub fn is_finite(&self) -> PolarsResult<BooleanChunked> {
724 match self.dtype() {
725 #[cfg(feature = "dtype-f16")]
726 DataType::Float16 => Ok(self.f16().unwrap().is_finite()),
727 DataType::Float32 => Ok(self.f32().unwrap().is_finite()),
728 DataType::Float64 => Ok(self.f64().unwrap().is_finite()),
729 DataType::Null => Ok(BooleanChunked::full_null(self.name().clone(), self.len())),
730 dt if dt.is_primitive_numeric() => {
731 let arr = BooleanArray::full(self.len(), true, ArrowDataType::Boolean)
732 .with_validity(self.rechunk_validity());
733 Ok(BooleanChunked::with_chunk(self.name().clone(), arr))
734 },
735 _ => polars_bail!(opq = is_finite, self.dtype()),
736 }
737 }
738
739 pub fn is_infinite(&self) -> PolarsResult<BooleanChunked> {
741 match self.dtype() {
742 #[cfg(feature = "dtype-f16")]
743 DataType::Float16 => Ok(self.f16().unwrap().is_infinite()),
744 DataType::Float32 => Ok(self.f32().unwrap().is_infinite()),
745 DataType::Float64 => Ok(self.f64().unwrap().is_infinite()),
746 DataType::Null => Ok(BooleanChunked::full_null(self.name().clone(), self.len())),
747 dt if dt.is_primitive_numeric() => {
748 let arr = BooleanArray::full(self.len(), false, ArrowDataType::Boolean)
749 .with_validity(self.rechunk_validity());
750 Ok(BooleanChunked::with_chunk(self.name().clone(), arr))
751 },
752 _ => polars_bail!(opq = is_infinite, self.dtype()),
753 }
754 }
755
756 #[cfg(feature = "zip_with")]
760 pub fn zip_with(&self, mask: &BooleanChunked, other: &Series) -> PolarsResult<Series> {
761 let (lhs, rhs) = coerce_lhs_rhs(self, other)?;
762 lhs.zip_with_same_type(mask, rhs.as_ref())
763 }
764
765 pub fn to_physical_repr(&self) -> Cow<'_, Series> {
779 use DataType::*;
780 match self.dtype() {
781 #[cfg(feature = "dtype-date")]
784 Date => Cow::Owned(self.date().unwrap().phys.clone().into_series()),
785 #[cfg(feature = "dtype-datetime")]
786 Datetime(_, _) => Cow::Owned(self.datetime().unwrap().phys.clone().into_series()),
787 #[cfg(feature = "dtype-duration")]
788 Duration(_) => Cow::Owned(self.duration().unwrap().phys.clone().into_series()),
789 #[cfg(feature = "dtype-time")]
790 Time => Cow::Owned(self.time().unwrap().phys.clone().into_series()),
791 #[cfg(feature = "dtype-categorical")]
792 dt @ (Categorical(_, _) | Enum(_, _)) => {
793 with_match_categorical_physical_type!(dt.cat_physical().unwrap(), |$C| {
794 let ca = self.cat::<$C>().unwrap();
795 Cow::Owned(ca.physical().clone().into_series())
796 })
797 },
798 #[cfg(feature = "dtype-decimal")]
799 Decimal(_, _) => Cow::Owned(self.decimal().unwrap().phys.clone().into_series()),
800 List(_) => match self.list().unwrap().to_physical_repr() {
801 Cow::Borrowed(_) => Cow::Borrowed(self),
802 Cow::Owned(ca) => Cow::Owned(ca.into_series()),
803 },
804 #[cfg(feature = "dtype-array")]
805 Array(_, _) => match self.array().unwrap().to_physical_repr() {
806 Cow::Borrowed(_) => Cow::Borrowed(self),
807 Cow::Owned(ca) => Cow::Owned(ca.into_series()),
808 },
809 #[cfg(feature = "dtype-struct")]
810 Struct(_) => match self.struct_().unwrap().to_physical_repr() {
811 Cow::Borrowed(_) => Cow::Borrowed(self),
812 Cow::Owned(ca) => Cow::Owned(ca.into_series()),
813 },
814 #[cfg(feature = "dtype-map")]
815 Map(_, _) => match self.map().unwrap().storage().to_physical_repr() {
816 Cow::Borrowed(storage) => Cow::Owned(storage.clone()),
817 Cow::Owned(storage) => Cow::Owned(storage),
818 },
819 #[cfg(feature = "dtype-extension")]
820 Extension(_, _) => self.ext().unwrap().storage().to_physical_repr(),
821 _ => Cow::Borrowed(self),
822 }
823 }
824
825 pub fn to_storage(&self) -> &Series {
828 #[cfg(feature = "dtype-extension")]
829 {
830 if let DataType::Extension(_, _) = self.dtype() {
831 return self.ext().unwrap().storage();
832 }
833 }
834 self
835 }
836
837 pub fn gather_every(&self, n: usize, offset: usize) -> PolarsResult<Series> {
839 polars_ensure!(n > 0, ComputeError: "cannot perform gather every for `n=0`");
840 let idx = ((offset as IdxSize)..self.len() as IdxSize)
841 .step_by(n)
842 .collect_ca(PlSmallStr::EMPTY);
843 Ok(unsafe { self.take_unchecked(&idx) })
845 }
846
847 #[cfg(feature = "dot_product")]
848 pub fn dot(&self, other: &Series) -> PolarsResult<f64> {
849 std::ops::Mul::mul(self, other)?.sum::<f64>()
850 }
851
852 pub fn sum_reduce(&self) -> PolarsResult<Scalar> {
859 self.0.sum_reduce()
860 }
861
862 pub fn mean_reduce(&self) -> PolarsResult<Scalar> {
865 self.0.mean_reduce()
866 }
867
868 pub fn product(&self) -> PolarsResult<Scalar> {
873 #[cfg(feature = "product")]
874 {
875 use DataType::*;
876 match self.dtype() {
877 Boolean => self.cast(&DataType::Int64).unwrap().product(),
878 Int8 | UInt8 | Int16 | UInt16 | Int32 | UInt32 => {
879 let s = self.cast(&Int64).unwrap();
880 s.product()
881 },
882 Int64 => Ok(self.i64().unwrap().prod_reduce()),
883 UInt64 => Ok(self.u64().unwrap().prod_reduce()),
884 #[cfg(feature = "dtype-i128")]
885 Int128 => Ok(self.i128().unwrap().prod_reduce()),
886 #[cfg(feature = "dtype-u128")]
887 UInt128 => Ok(self.u128().unwrap().prod_reduce()),
888 #[cfg(feature = "dtype-f16")]
889 Float16 => Ok(self.f16().unwrap().prod_reduce()),
890 Float32 => Ok(self.f32().unwrap().prod_reduce()),
891 Float64 => Ok(self.f64().unwrap().prod_reduce()),
892 #[cfg(feature = "dtype-decimal")]
893 Decimal(..) => Ok(self.decimal().unwrap().prod_reduce()),
894 dt => {
895 polars_bail!(InvalidOperation: "`product` operation not supported for dtype `{dt}`")
896 },
897 }
898 }
899 #[cfg(not(feature = "product"))]
900 {
901 panic!("activate 'product' feature")
902 }
903 }
904
905 pub fn strict_cast(&self, dtype: &DataType) -> PolarsResult<Series> {
907 self.cast_with_options(dtype, CastOptions::Strict)
908 }
909
910 #[cfg(feature = "dtype-decimal")]
911 pub fn into_decimal(self, precision: usize, scale: usize) -> PolarsResult<Series> {
912 match self.dtype() {
913 DataType::Int128 => Ok(self
914 .i128()
915 .unwrap()
916 .clone()
917 .into_decimal(precision, scale)?
918 .into_series()),
919 DataType::Decimal(cur_prec, cur_scale)
920 if scale == *cur_scale && precision >= *cur_prec =>
921 {
922 Ok(self)
923 },
924 dt => panic!("into_decimal({precision:?}, {scale}) not implemented for {dt:?}"),
925 }
926 }
927
928 #[cfg(feature = "dtype-time")]
929 pub fn into_time(self) -> Series {
930 match self.dtype() {
931 DataType::Int64 => self.i64().unwrap().clone().into_time().into_series(),
932 DataType::Time => self
933 .time()
934 .unwrap()
935 .physical()
936 .clone()
937 .into_time()
938 .into_series(),
939 dt => panic!("date not implemented for {dt:?}"),
940 }
941 }
942
943 pub fn into_date(self) -> Series {
944 #[cfg(not(feature = "dtype-date"))]
945 {
946 panic!("activate feature dtype-date")
947 }
948 #[cfg(feature = "dtype-date")]
949 match self.dtype() {
950 DataType::Int32 => self.i32().unwrap().clone().into_date().into_series(),
951 DataType::Date => self
952 .date()
953 .unwrap()
954 .physical()
955 .clone()
956 .into_date()
957 .into_series(),
958 dt => panic!("date not implemented for {dt:?}"),
959 }
960 }
961
962 #[allow(unused_variables)]
963 pub fn into_datetime(self, timeunit: TimeUnit, tz: Option<TimeZone>) -> Series {
964 #[cfg(not(feature = "dtype-datetime"))]
965 {
966 panic!("activate feature dtype-datetime")
967 }
968
969 #[cfg(feature = "dtype-datetime")]
970 match self.dtype() {
971 DataType::Int64 => self
972 .i64()
973 .unwrap()
974 .clone()
975 .into_datetime(timeunit, tz)
976 .into_series(),
977 DataType::Datetime(_, _) => self
978 .datetime()
979 .unwrap()
980 .physical()
981 .clone()
982 .into_datetime(timeunit, tz)
983 .into_series(),
984 dt => panic!("into_datetime not implemented for {dt:?}"),
985 }
986 }
987
988 #[allow(unused_variables)]
989 pub fn into_duration(self, timeunit: TimeUnit) -> Series {
990 #[cfg(not(feature = "dtype-duration"))]
991 {
992 panic!("activate feature dtype-duration")
993 }
994 #[cfg(feature = "dtype-duration")]
995 match self.dtype() {
996 DataType::Int64 => self
997 .i64()
998 .unwrap()
999 .clone()
1000 .into_duration(timeunit)
1001 .into_series(),
1002 DataType::Duration(_) => self
1003 .duration()
1004 .unwrap()
1005 .physical()
1006 .clone()
1007 .into_duration(timeunit)
1008 .into_series(),
1009 dt => panic!("into_duration not implemented for {dt:?}"),
1010 }
1011 }
1012
1013 pub fn str_value(&self, index: usize) -> PolarsResult<Cow<'_, str>> {
1015 Ok(self.0.get(index)?.str_value())
1016 }
1017 pub fn head(&self, length: Option<usize>) -> Series {
1019 let len = length.unwrap_or(HEAD_DEFAULT_LENGTH);
1020 self.slice(0, std::cmp::min(len, self.len()))
1021 }
1022
1023 pub fn tail(&self, length: Option<usize>) -> Series {
1025 let len = length.unwrap_or(TAIL_DEFAULT_LENGTH);
1026 let len = std::cmp::min(len, self.len());
1027 self.slice(-(len as i64), len)
1028 }
1029
1030 pub fn unique_stable(&self) -> PolarsResult<Series> {
1033 let idx = self.arg_unique()?;
1034 unsafe { Ok(self.take_unchecked(&idx)) }
1036 }
1037
1038 pub fn try_idx(&self) -> Option<&IdxCa> {
1039 #[cfg(feature = "bigidx")]
1040 {
1041 self.try_u64()
1042 }
1043 #[cfg(not(feature = "bigidx"))]
1044 {
1045 self.try_u32()
1046 }
1047 }
1048
1049 pub fn idx(&self) -> PolarsResult<&IdxCa> {
1050 #[cfg(feature = "bigidx")]
1051 {
1052 self.u64()
1053 }
1054 #[cfg(not(feature = "bigidx"))]
1055 {
1056 self.u32()
1057 }
1058 }
1059
1060 pub fn estimated_size(&self) -> usize {
1073 let mut size = 0;
1074 match self.dtype() {
1075 #[cfg(feature = "object")]
1077 DataType::Object(_) => {
1078 let ArrowDataType::FixedSizeBinary(size) = self.chunks()[0].dtype() else {
1079 unreachable!()
1080 };
1081 return self.len() * *size;
1083 },
1084 _ => {},
1085 }
1086
1087 size += self
1088 .chunks()
1089 .iter()
1090 .map(|arr| estimated_bytes_size(&**arr))
1091 .sum::<usize>();
1092
1093 size
1094 }
1095
1096 pub fn row_encode_unordered(&self) -> PolarsResult<BinaryOffsetChunked> {
1097 row_encode::_get_rows_encoded_ca_unordered(
1098 self.name().clone(),
1099 &[self.clone().into_column()],
1100 )
1101 }
1102
1103 pub fn row_encode_ordered(
1104 &self,
1105 descending: bool,
1106 nulls_last: bool,
1107 ) -> PolarsResult<BinaryOffsetChunked> {
1108 row_encode::_get_rows_encoded_ca(
1109 self.name().clone(),
1110 &[self.clone().into_column()],
1111 &[descending],
1112 &[nulls_last],
1113 false,
1114 )
1115 }
1116}
1117
1118impl Default for Series {
1119 fn default() -> Self {
1120 NullChunked::new(PlSmallStr::EMPTY, 0).into_series()
1121 }
1122}
1123
1124impl Deref for Series {
1125 type Target = dyn SeriesTrait;
1126
1127 fn deref(&self) -> &Self::Target {
1128 self.0.as_ref()
1129 }
1130}
1131
1132impl<'a> AsRef<dyn SeriesTrait + 'a> for Series {
1133 fn as_ref(&self) -> &(dyn SeriesTrait + 'a) {
1134 self.0.as_ref()
1135 }
1136}
1137
1138impl<T: PolarsPhysicalType> AsRef<ChunkedArray<T>> for dyn SeriesTrait + '_ {
1139 fn as_ref(&self) -> &ChunkedArray<T> {
1140 let Some(ca) = self.as_any().downcast_ref::<ChunkedArray<T>>() else {
1143 panic!(
1144 "implementation error, cannot get ref {:?} from {:?}",
1145 T::get_static_dtype(),
1146 self.dtype()
1147 );
1148 };
1149
1150 ca
1151 }
1152}
1153
1154impl<T: PolarsPhysicalType> AsMut<ChunkedArray<T>> for dyn SeriesTrait + '_ {
1155 fn as_mut(&mut self) -> &mut ChunkedArray<T> {
1156 if !self.as_any_mut().is::<ChunkedArray<T>>() {
1157 panic!(
1158 "implementation error, cannot get ref {:?} from {:?}",
1159 T::get_static_dtype(),
1160 self.dtype()
1161 );
1162 }
1163
1164 self.as_any_mut().downcast_mut::<ChunkedArray<T>>().unwrap()
1167 }
1168}
1169
1170impl BroadcastLength for Series {
1171 fn _broadcast_len(&self) -> usize {
1172 self.len()
1173 }
1174
1175 fn _column_name(&self) -> Option<&str> {
1176 Some(self.name())
1177 }
1178}
1179
1180#[cfg(test)]
1181mod test {
1182 use crate::series::*;
1183
1184 #[test]
1185 fn cast() {
1186 let ar = UInt32Chunked::new("a".into(), &[1, 2]);
1187 let s = ar.into_series();
1188 let s2 = s.cast(&DataType::Int64).unwrap();
1189
1190 assert!(s2.i64().is_ok());
1191 let s2 = s.cast(&DataType::Float32).unwrap();
1192 assert!(s2.f32().is_ok());
1193 }
1194
1195 #[test]
1196 fn new_series() {
1197 let _ = Series::new("boolean series".into(), &vec![true, false, true]);
1198 let _ = Series::new("int series".into(), &[1, 2, 3]);
1199 let ca = Int32Chunked::new("a".into(), &[1, 2, 3]);
1200 let _ = ca.into_series();
1201 }
1202
1203 #[test]
1204 #[cfg(feature = "dtype-date")]
1205 fn roundtrip_list_logical_20311() {
1206 let list = ListChunked::from_chunk_iter(
1207 PlSmallStr::from_static("a"),
1208 [ListArray::new(
1209 ArrowDataType::LargeList(Box::new(ArrowField::new(
1210 LIST_VALUES_NAME,
1211 ArrowDataType::Int32,
1212 true,
1213 ))),
1214 unsafe { arrow::offset::Offsets::new_unchecked(vec![0, 1]) }.into(),
1215 PrimitiveArray::new(ArrowDataType::Int32, vec![1i32].into(), None).to_boxed(),
1216 None,
1217 )],
1218 );
1219 let list = unsafe { list.from_physical_unchecked(DataType::Date) }.unwrap();
1220 assert_eq!(list.dtype(), &DataType::List(Box::new(DataType::Date)));
1221 }
1222
1223 #[test]
1224 #[cfg(feature = "dtype-struct")]
1225 fn new_series_from_empty_structs() {
1226 let dtype = DataType::Struct(vec![]);
1227 let empties = vec![AnyValue::StructOwned(Box::new((vec![], vec![]))); 3];
1228 let s = Series::from_any_values_and_dtype("".into(), &empties, &dtype, false).unwrap();
1229 assert_eq!(s.len(), 3);
1230 }
1231 #[test]
1232 fn new_series_from_arrow_primitive_array() {
1233 let array = UInt32Array::from_slice([1, 2, 3, 4, 5]);
1234 let array_ref: ArrayRef = Box::new(array);
1235
1236 let _ = Series::try_new("foo".into(), array_ref).unwrap();
1237 }
1238
1239 #[test]
1240 fn series_append() {
1241 let mut s1 = Series::new("a".into(), &[1, 2]);
1242 let s2 = Series::new("b".into(), &[3]);
1243 s1.append(&s2).unwrap();
1244 assert_eq!(s1.len(), 3);
1245
1246 let s2 = Series::new("b".into(), &[3.0]);
1248 assert!(s1.append(&s2).is_err())
1249 }
1250
1251 #[test]
1252 #[cfg(feature = "dtype-decimal")]
1253 fn series_append_decimal() {
1254 let s1 = Series::new("a".into(), &[1.1, 2.3])
1255 .cast(&DataType::Decimal(38, 2))
1256 .unwrap();
1257 let s2 = Series::new("b".into(), &[3])
1258 .cast(&DataType::Decimal(38, 0))
1259 .unwrap();
1260
1261 {
1262 let mut s1 = s1.clone();
1263 s1.append(&s2).unwrap();
1264 assert_eq!(s1.len(), 3);
1265 assert_eq!(s1.get(2).unwrap(), AnyValue::Decimal(300, 38, 2));
1266 }
1267
1268 {
1269 let mut s2 = s2;
1270 s2.extend(&s1).unwrap();
1271 assert_eq!(s2.get(2).unwrap(), AnyValue::Decimal(2, 38, 0));
1272 }
1273 }
1274
1275 #[test]
1276 fn series_slice_works() {
1277 let series = Series::new("a".into(), &[1i64, 2, 3, 4, 5]);
1278
1279 let slice_1 = series.slice(-3, 3);
1280 let slice_2 = series.slice(-5, 5);
1281 let slice_3 = series.slice(0, 5);
1282
1283 assert_eq!(slice_1.get(0).unwrap(), AnyValue::Int64(3));
1284 assert_eq!(slice_2.get(0).unwrap(), AnyValue::Int64(1));
1285 assert_eq!(slice_3.get(0).unwrap(), AnyValue::Int64(1));
1286 }
1287
1288 #[test]
1289 fn out_of_range_slice_does_not_panic() {
1290 let series = Series::new("a".into(), &[1i64, 2, 3, 4, 5]);
1291
1292 let _ = series.slice(-3, 4);
1293 let _ = series.slice(-6, 2);
1294 let _ = series.slice(4, 2);
1295 }
1296}