polars_core/series/implementations/
list.rs

1use super::*;
2use crate::chunked_array::comparison::*;
3#[cfg(feature = "algorithm_group_by")]
4use crate::frame::group_by::*;
5use crate::prelude::*;
6
7impl private::PrivateSeries for SeriesWrap<ListChunked> {
8    fn compute_len(&mut self) {
9        self.0.compute_len()
10    }
11    fn _field(&self) -> Cow<Field> {
12        Cow::Borrowed(self.0.ref_field())
13    }
14    fn _dtype(&self) -> &DataType {
15        self.0.ref_field().dtype()
16    }
17    fn _get_flags(&self) -> StatisticsFlags {
18        self.0.get_flags()
19    }
20    fn _set_flags(&mut self, flags: StatisticsFlags) {
21        self.0.set_flags(flags)
22    }
23
24    unsafe fn equal_element(&self, idx_self: usize, idx_other: usize, other: &Series) -> bool {
25        self.0.equal_element(idx_self, idx_other, other)
26    }
27
28    #[cfg(feature = "zip_with")]
29    fn zip_with_same_type(&self, mask: &BooleanChunked, other: &Series) -> PolarsResult<Series> {
30        ChunkZip::zip_with(&self.0, mask, other.as_ref().as_ref()).map(|ca| ca.into_series())
31    }
32
33    #[cfg(feature = "algorithm_group_by")]
34    unsafe fn agg_list(&self, groups: &GroupsType) -> Series {
35        self.0.agg_list(groups)
36    }
37
38    #[cfg(feature = "algorithm_group_by")]
39    fn group_tuples(&self, multithreaded: bool, sorted: bool) -> PolarsResult<GroupsType> {
40        IntoGroupsType::group_tuples(&self.0, multithreaded, sorted)
41    }
42
43    fn into_total_eq_inner<'a>(&'a self) -> Box<dyn TotalEqInner + 'a> {
44        (&self.0).into_total_eq_inner()
45    }
46    fn into_total_ord_inner<'a>(&'a self) -> Box<dyn TotalOrdInner + 'a> {
47        invalid_operation_panic!(into_total_ord_inner, self)
48    }
49
50    fn add_to(&self, rhs: &Series) -> PolarsResult<Series> {
51        self.0.add_to(rhs)
52    }
53
54    fn subtract(&self, rhs: &Series) -> PolarsResult<Series> {
55        self.0.subtract(rhs)
56    }
57
58    fn multiply(&self, rhs: &Series) -> PolarsResult<Series> {
59        self.0.multiply(rhs)
60    }
61    fn divide(&self, rhs: &Series) -> PolarsResult<Series> {
62        self.0.divide(rhs)
63    }
64    fn remainder(&self, rhs: &Series) -> PolarsResult<Series> {
65        self.0.remainder(rhs)
66    }
67}
68
69impl SeriesTrait for SeriesWrap<ListChunked> {
70    fn rename(&mut self, name: PlSmallStr) {
71        self.0.rename(name);
72    }
73
74    fn chunk_lengths(&self) -> ChunkLenIter {
75        self.0.chunk_lengths()
76    }
77    fn name(&self) -> &PlSmallStr {
78        self.0.name()
79    }
80
81    fn chunks(&self) -> &Vec<ArrayRef> {
82        self.0.chunks()
83    }
84    unsafe fn chunks_mut(&mut self) -> &mut Vec<ArrayRef> {
85        self.0.chunks_mut()
86    }
87    fn shrink_to_fit(&mut self) {
88        self.0.shrink_to_fit()
89    }
90
91    fn sum_reduce(&self) -> PolarsResult<Scalar> {
92        polars_bail!(
93            op = "`sum`",
94            self.dtype(),
95            hint = "you may mean to call `concat_list`"
96        );
97    }
98
99    fn arg_sort(&self, options: SortOptions) -> IdxCa {
100        self.0.arg_sort(options)
101    }
102
103    fn sort_with(&self, options: SortOptions) -> PolarsResult<Series> {
104        Ok(self.0.sort_with(options).into_series())
105    }
106
107    fn slice(&self, offset: i64, length: usize) -> Series {
108        self.0.slice(offset, length).into_series()
109    }
110
111    fn split_at(&self, offset: i64) -> (Series, Series) {
112        let (a, b) = self.0.split_at(offset);
113        (a.into_series(), b.into_series())
114    }
115
116    fn append(&mut self, other: &Series) -> PolarsResult<()> {
117        polars_ensure!(self.0.dtype() == other.dtype(), append);
118        self.0.append(other.as_ref().as_ref())
119    }
120    fn append_owned(&mut self, other: Series) -> PolarsResult<()> {
121        polars_ensure!(self.0.dtype() == other.dtype(), append);
122        self.0.append_owned(other.take_inner())
123    }
124
125    fn extend(&mut self, other: &Series) -> PolarsResult<()> {
126        polars_ensure!(self.0.dtype() == other.dtype(), extend);
127        self.0.extend(other.as_ref().as_ref())
128    }
129
130    fn filter(&self, filter: &BooleanChunked) -> PolarsResult<Series> {
131        ChunkFilter::filter(&self.0, filter).map(|ca| ca.into_series())
132    }
133
134    fn take(&self, indices: &IdxCa) -> PolarsResult<Series> {
135        Ok(self.0.take(indices)?.into_series())
136    }
137
138    unsafe fn take_unchecked(&self, indices: &IdxCa) -> Series {
139        self.0.take_unchecked(indices).into_series()
140    }
141
142    fn take_slice(&self, indices: &[IdxSize]) -> PolarsResult<Series> {
143        Ok(self.0.take(indices)?.into_series())
144    }
145
146    unsafe fn take_slice_unchecked(&self, indices: &[IdxSize]) -> Series {
147        self.0.take_unchecked(indices).into_series()
148    }
149
150    fn len(&self) -> usize {
151        self.0.len()
152    }
153
154    fn rechunk(&self) -> Series {
155        self.0.rechunk().into_owned().into_series()
156    }
157
158    fn new_from_index(&self, index: usize, length: usize) -> Series {
159        ChunkExpandAtIndex::new_from_index(&self.0, index, length).into_series()
160    }
161
162    fn cast(&self, dtype: &DataType, cast_options: CastOptions) -> PolarsResult<Series> {
163        self.0.cast_with_options(dtype, cast_options)
164    }
165
166    #[inline]
167    unsafe fn get_unchecked(&self, index: usize) -> AnyValue {
168        self.0.get_any_value_unchecked(index)
169    }
170
171    fn null_count(&self) -> usize {
172        self.0.null_count()
173    }
174
175    fn has_nulls(&self) -> bool {
176        self.0.has_nulls()
177    }
178
179    #[cfg(feature = "algorithm_group_by")]
180    fn unique(&self) -> PolarsResult<Series> {
181        if !self.inner_dtype().is_primitive_numeric() {
182            polars_bail!(opq = unique, self.dtype());
183        }
184        // this can be called in aggregation, so this fast path can be worth a lot
185        if self.len() < 2 {
186            return Ok(self.0.clone().into_series());
187        }
188        let main_thread = POOL.current_thread_index().is_none();
189        let groups = self.group_tuples(main_thread, false);
190        // SAFETY:
191        // groups are in bounds
192        Ok(unsafe { self.0.clone().into_series().agg_first(&groups?) })
193    }
194
195    #[cfg(feature = "algorithm_group_by")]
196    fn n_unique(&self) -> PolarsResult<usize> {
197        // this can be called in aggregation, so this fast path can be worth a lot
198        match self.len() {
199            0 => Ok(0),
200            1 => Ok(1),
201            _ => {
202                let main_thread = POOL.current_thread_index().is_none();
203                let groups = self.group_tuples(main_thread, false)?;
204                Ok(groups.len())
205            },
206        }
207    }
208
209    #[cfg(feature = "algorithm_group_by")]
210    fn arg_unique(&self) -> PolarsResult<IdxCa> {
211        if !self.inner_dtype().is_primitive_numeric() {
212            polars_bail!(opq = arg_unique, self.dtype());
213        }
214        // this can be called in aggregation, so this fast path can be worth a lot
215        if self.len() == 1 {
216            return Ok(IdxCa::new_vec(self.name().clone(), vec![0 as IdxSize]));
217        }
218        let main_thread = POOL.current_thread_index().is_none();
219        // arg_unique requires a stable order
220        let groups = self.group_tuples(main_thread, true)?;
221        let first = groups.take_group_firsts();
222        Ok(IdxCa::from_vec(self.name().clone(), first))
223    }
224
225    fn is_null(&self) -> BooleanChunked {
226        self.0.is_null()
227    }
228
229    fn is_not_null(&self) -> BooleanChunked {
230        self.0.is_not_null()
231    }
232
233    fn reverse(&self) -> Series {
234        ChunkReverse::reverse(&self.0).into_series()
235    }
236
237    fn as_single_ptr(&mut self) -> PolarsResult<usize> {
238        self.0.as_single_ptr()
239    }
240
241    fn shift(&self, periods: i64) -> Series {
242        ChunkShift::shift(&self.0, periods).into_series()
243    }
244
245    fn clone_inner(&self) -> Arc<dyn SeriesTrait> {
246        Arc::new(SeriesWrap(Clone::clone(&self.0)))
247    }
248
249    fn as_any(&self) -> &dyn Any {
250        &self.0
251    }
252
253    fn as_any_mut(&mut self) -> &mut dyn Any {
254        &mut self.0
255    }
256
257    fn as_phys_any(&self) -> &dyn Any {
258        &self.0
259    }
260
261    fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
262        self as _
263    }
264}