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/boolean.rs
6940 views
1
use super::*;
2
use crate::chunked_array::comparison::*;
3
#[cfg(feature = "algorithm_group_by")]
4
use crate::frame::group_by::*;
5
use crate::prelude::*;
6
7
impl private::PrivateSeries for SeriesWrap<BooleanChunked> {
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
fn into_total_eq_inner<'a>(&'a self) -> Box<dyn TotalEqInner + 'a> {
33
(&self.0).into_total_eq_inner()
34
}
35
fn into_total_ord_inner<'a>(&'a self) -> Box<dyn TotalOrdInner + 'a> {
36
(&self.0).into_total_ord_inner()
37
}
38
39
fn vec_hash(
40
&self,
41
random_state: PlSeedableRandomStateQuality,
42
buf: &mut Vec<u64>,
43
) -> PolarsResult<()> {
44
self.0.vec_hash(random_state, buf)?;
45
Ok(())
46
}
47
48
fn vec_hash_combine(
49
&self,
50
build_hasher: PlSeedableRandomStateQuality,
51
hashes: &mut [u64],
52
) -> PolarsResult<()> {
53
self.0.vec_hash_combine(build_hasher, hashes)?;
54
Ok(())
55
}
56
57
#[cfg(feature = "algorithm_group_by")]
58
unsafe fn agg_min(&self, groups: &GroupsType) -> Series {
59
self.0.agg_min(groups)
60
}
61
62
#[cfg(feature = "algorithm_group_by")]
63
unsafe fn agg_max(&self, groups: &GroupsType) -> Series {
64
self.0.agg_max(groups)
65
}
66
67
#[cfg(feature = "algorithm_group_by")]
68
unsafe fn agg_sum(&self, groups: &GroupsType) -> Series {
69
self.0.agg_sum(groups)
70
}
71
72
#[cfg(feature = "algorithm_group_by")]
73
unsafe fn agg_list(&self, groups: &GroupsType) -> Series {
74
self.0.agg_list(groups)
75
}
76
#[cfg(feature = "algorithm_group_by")]
77
unsafe fn agg_std(&self, groups: &GroupsType, _ddof: u8) -> Series {
78
self.0
79
.cast_with_options(&DataType::Float64, CastOptions::Overflowing)
80
.unwrap()
81
.agg_std(groups, _ddof)
82
}
83
#[cfg(feature = "algorithm_group_by")]
84
unsafe fn agg_var(&self, groups: &GroupsType, _ddof: u8) -> Series {
85
self.0
86
.cast_with_options(&DataType::Float64, CastOptions::Overflowing)
87
.unwrap()
88
.agg_var(groups, _ddof)
89
}
90
91
#[cfg(feature = "bitwise")]
92
unsafe fn agg_and(&self, groups: &GroupsType) -> Series {
93
self.0.agg_and(groups)
94
}
95
#[cfg(feature = "bitwise")]
96
unsafe fn agg_or(&self, groups: &GroupsType) -> Series {
97
self.0.agg_or(groups)
98
}
99
#[cfg(feature = "bitwise")]
100
unsafe fn agg_xor(&self, groups: &GroupsType) -> Series {
101
self.0.agg_xor(groups)
102
}
103
104
#[cfg(feature = "algorithm_group_by")]
105
fn group_tuples(&self, multithreaded: bool, sorted: bool) -> PolarsResult<GroupsType> {
106
IntoGroupsType::group_tuples(&self.0, multithreaded, sorted)
107
}
108
109
fn arg_sort_multiple(
110
&self,
111
by: &[Column],
112
options: &SortMultipleOptions,
113
) -> PolarsResult<IdxCa> {
114
self.0.arg_sort_multiple(by, options)
115
}
116
fn add_to(&self, rhs: &Series) -> PolarsResult<Series> {
117
NumOpsDispatch::add_to(&self.0, rhs)
118
}
119
}
120
121
impl SeriesTrait for SeriesWrap<BooleanChunked> {
122
fn rename(&mut self, name: PlSmallStr) {
123
self.0.rename(name);
124
}
125
126
fn chunk_lengths(&self) -> ChunkLenIter<'_> {
127
self.0.chunk_lengths()
128
}
129
fn name(&self) -> &PlSmallStr {
130
self.0.name()
131
}
132
133
fn chunks(&self) -> &Vec<ArrayRef> {
134
self.0.chunks()
135
}
136
unsafe fn chunks_mut(&mut self) -> &mut Vec<ArrayRef> {
137
self.0.chunks_mut()
138
}
139
fn shrink_to_fit(&mut self) {
140
self.0.shrink_to_fit()
141
}
142
143
fn slice(&self, offset: i64, length: usize) -> Series {
144
self.0.slice(offset, length).into_series()
145
}
146
fn split_at(&self, offset: i64) -> (Series, Series) {
147
let (a, b) = self.0.split_at(offset);
148
(a.into_series(), b.into_series())
149
}
150
151
fn append(&mut self, other: &Series) -> PolarsResult<()> {
152
polars_ensure!(self.0.dtype() == other.dtype(), append);
153
self.0.append(other.as_ref().as_ref())?;
154
Ok(())
155
}
156
fn append_owned(&mut self, other: Series) -> PolarsResult<()> {
157
polars_ensure!(self.0.dtype() == other.dtype(), append);
158
self.0.append_owned(other.take_inner())
159
}
160
161
fn extend(&mut self, other: &Series) -> PolarsResult<()> {
162
polars_ensure!(self.0.dtype() == other.dtype(), extend);
163
self.0.extend(other.as_ref().as_ref())?;
164
Ok(())
165
}
166
167
fn filter(&self, filter: &BooleanChunked) -> PolarsResult<Series> {
168
ChunkFilter::filter(&self.0, filter).map(|ca| ca.into_series())
169
}
170
171
fn _sum_as_f64(&self) -> f64 {
172
self.0.sum().unwrap() as f64
173
}
174
175
fn mean(&self) -> Option<f64> {
176
self.0.mean()
177
}
178
179
fn take(&self, indices: &IdxCa) -> PolarsResult<Series> {
180
Ok(self.0.take(indices)?.into_series())
181
}
182
183
unsafe fn take_unchecked(&self, indices: &IdxCa) -> Series {
184
self.0.take_unchecked(indices).into_series()
185
}
186
187
fn take_slice(&self, indices: &[IdxSize]) -> PolarsResult<Series> {
188
Ok(self.0.take(indices)?.into_series())
189
}
190
191
unsafe fn take_slice_unchecked(&self, indices: &[IdxSize]) -> Series {
192
self.0.take_unchecked(indices).into_series()
193
}
194
195
fn len(&self) -> usize {
196
self.0.len()
197
}
198
199
fn rechunk(&self) -> Series {
200
self.0.rechunk().into_owned().into_series()
201
}
202
203
fn new_from_index(&self, index: usize, length: usize) -> Series {
204
ChunkExpandAtIndex::new_from_index(&self.0, index, length).into_series()
205
}
206
207
fn cast(&self, dtype: &DataType, options: CastOptions) -> PolarsResult<Series> {
208
self.0.cast_with_options(dtype, options)
209
}
210
211
#[inline]
212
unsafe fn get_unchecked(&self, index: usize) -> AnyValue<'_> {
213
self.0.get_any_value_unchecked(index)
214
}
215
216
fn sort_with(&self, options: SortOptions) -> PolarsResult<Series> {
217
Ok(ChunkSort::sort_with(&self.0, options).into_series())
218
}
219
220
fn arg_sort(&self, options: SortOptions) -> IdxCa {
221
ChunkSort::arg_sort(&self.0, options)
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
ChunkUnique::unique(&self.0).map(|ca| ca.into_series())
235
}
236
237
#[cfg(feature = "algorithm_group_by")]
238
fn n_unique(&self) -> PolarsResult<usize> {
239
ChunkUnique::n_unique(&self.0)
240
}
241
242
#[cfg(feature = "algorithm_group_by")]
243
fn arg_unique(&self) -> PolarsResult<IdxCa> {
244
ChunkUnique::arg_unique(&self.0)
245
}
246
247
fn is_null(&self) -> BooleanChunked {
248
self.0.is_null()
249
}
250
251
fn is_not_null(&self) -> BooleanChunked {
252
self.0.is_not_null()
253
}
254
255
fn reverse(&self) -> Series {
256
ChunkReverse::reverse(&self.0).into_series()
257
}
258
259
fn as_single_ptr(&mut self) -> PolarsResult<usize> {
260
self.0.as_single_ptr()
261
}
262
263
fn shift(&self, periods: i64) -> Series {
264
ChunkShift::shift(&self.0, periods).into_series()
265
}
266
267
fn sum_reduce(&self) -> PolarsResult<Scalar> {
268
Ok(ChunkAggSeries::sum_reduce(&self.0))
269
}
270
fn max_reduce(&self) -> PolarsResult<Scalar> {
271
Ok(ChunkAggSeries::max_reduce(&self.0))
272
}
273
fn min_reduce(&self) -> PolarsResult<Scalar> {
274
Ok(ChunkAggSeries::min_reduce(&self.0))
275
}
276
fn median_reduce(&self) -> PolarsResult<Scalar> {
277
let ca = self
278
.0
279
.cast_with_options(&DataType::Int8, CastOptions::Overflowing)
280
.unwrap();
281
let sc = ca.median_reduce()?;
282
let v = sc.value().cast(&DataType::Float64);
283
Ok(Scalar::new(DataType::Float64, v))
284
}
285
/// Get the variance of the Series as a new Series of length 1.
286
fn var_reduce(&self, _ddof: u8) -> PolarsResult<Scalar> {
287
let ca = self
288
.0
289
.cast_with_options(&DataType::Int8, CastOptions::Overflowing)
290
.unwrap();
291
let sc = ca.var_reduce(_ddof)?;
292
let v = sc.value().cast(&DataType::Float64);
293
Ok(Scalar::new(DataType::Float64, v))
294
}
295
/// Get the standard deviation of the Series as a new Series of length 1.
296
fn std_reduce(&self, _ddof: u8) -> PolarsResult<Scalar> {
297
let ca = self
298
.0
299
.cast_with_options(&DataType::Int8, CastOptions::Overflowing)
300
.unwrap();
301
let sc = ca.std_reduce(_ddof)?;
302
let v = sc.value().cast(&DataType::Float64);
303
Ok(Scalar::new(DataType::Float64, v))
304
}
305
fn and_reduce(&self) -> PolarsResult<Scalar> {
306
let dt = DataType::Boolean;
307
308
Ok(Scalar::new(
309
dt,
310
self.0
311
.downcast_iter()
312
.filter(|arr| !arr.is_empty())
313
.filter_map(polars_compute::bitwise::BitwiseKernel::reduce_and)
314
.reduce(|a, b| a & b)
315
.map_or(AnyValue::Null, Into::into),
316
))
317
}
318
fn or_reduce(&self) -> PolarsResult<Scalar> {
319
let dt = DataType::Boolean;
320
321
Ok(Scalar::new(
322
dt,
323
self.0
324
.downcast_iter()
325
.filter(|arr| !arr.is_empty())
326
.filter_map(polars_compute::bitwise::BitwiseKernel::reduce_or)
327
.reduce(|a, b| a | b)
328
.map_or(AnyValue::Null, Into::into),
329
))
330
}
331
fn xor_reduce(&self) -> PolarsResult<Scalar> {
332
let dt = DataType::Boolean;
333
334
Ok(Scalar::new(
335
dt,
336
self.0
337
.downcast_iter()
338
.filter(|arr| !arr.is_empty())
339
.filter_map(polars_compute::bitwise::BitwiseKernel::reduce_xor)
340
.reduce(|a, b| a ^ b)
341
.map_or(AnyValue::Null, Into::into),
342
))
343
}
344
345
#[cfg(feature = "approx_unique")]
346
fn approx_n_unique(&self) -> PolarsResult<IdxSize> {
347
Ok(ChunkApproxNUnique::approx_n_unique(&self.0))
348
}
349
350
fn clone_inner(&self) -> Arc<dyn SeriesTrait> {
351
Arc::new(SeriesWrap(Clone::clone(&self.0)))
352
}
353
354
fn find_validity_mismatch(&self, other: &Series, idxs: &mut Vec<IdxSize>) {
355
self.0.find_validity_mismatch(other, idxs)
356
}
357
358
fn as_any(&self) -> &dyn Any {
359
&self.0
360
}
361
362
fn as_any_mut(&mut self) -> &mut dyn Any {
363
&mut self.0
364
}
365
366
fn as_phys_any(&self) -> &dyn Any {
367
&self.0
368
}
369
370
fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
371
self as _
372
}
373
}
374
375