Skip to main content

polars_core/series/implementations/
list.rs

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