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