Skip to main content

polars_core/series/implementations/
array.rs

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