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
8416 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).into_series()
94
}
95
#[cfg(feature = "bitwise")]
96
unsafe fn agg_or(&self, groups: &GroupsType) -> Series {
97
self.0.agg_or(groups).into_series()
98
}
99
#[cfg(feature = "bitwise")]
100
unsafe fn agg_xor(&self, groups: &GroupsType) -> Series {
101
self.0.agg_xor(groups).into_series()
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 deposit(&self, validity: &Bitmap) -> Series {
196
self.0.deposit(validity).into_series()
197
}
198
199
fn len(&self) -> usize {
200
self.0.len()
201
}
202
203
fn rechunk(&self) -> Series {
204
self.0.rechunk().into_owned().into_series()
205
}
206
207
fn new_from_index(&self, index: usize, length: usize) -> Series {
208
ChunkExpandAtIndex::new_from_index(&self.0, index, length).into_series()
209
}
210
211
fn cast(&self, dtype: &DataType, options: CastOptions) -> PolarsResult<Series> {
212
self.0.cast_with_options(dtype, options)
213
}
214
215
#[inline]
216
unsafe fn get_unchecked(&self, index: usize) -> AnyValue<'_> {
217
self.0.get_any_value_unchecked(index)
218
}
219
220
fn sort_with(&self, options: SortOptions) -> PolarsResult<Series> {
221
Ok(ChunkSort::sort_with(&self.0, options).into_series())
222
}
223
224
fn arg_sort(&self, options: SortOptions) -> IdxCa {
225
ChunkSort::arg_sort(&self.0, options)
226
}
227
228
fn null_count(&self) -> usize {
229
self.0.null_count()
230
}
231
232
fn has_nulls(&self) -> bool {
233
self.0.has_nulls()
234
}
235
236
#[cfg(feature = "algorithm_group_by")]
237
fn unique(&self) -> PolarsResult<Series> {
238
ChunkUnique::unique(&self.0).map(|ca| ca.into_series())
239
}
240
241
#[cfg(feature = "algorithm_group_by")]
242
fn n_unique(&self) -> PolarsResult<usize> {
243
ChunkUnique::n_unique(&self.0)
244
}
245
246
#[cfg(feature = "algorithm_group_by")]
247
fn arg_unique(&self) -> PolarsResult<IdxCa> {
248
ChunkUnique::arg_unique(&self.0)
249
}
250
251
fn unique_id(&self) -> PolarsResult<(IdxSize, Vec<IdxSize>)> {
252
ChunkUnique::unique_id(&self.0)
253
}
254
255
fn is_null(&self) -> BooleanChunked {
256
self.0.is_null()
257
}
258
259
fn is_not_null(&self) -> BooleanChunked {
260
self.0.is_not_null()
261
}
262
263
fn reverse(&self) -> Series {
264
ChunkReverse::reverse(&self.0).into_series()
265
}
266
267
fn as_single_ptr(&mut self) -> PolarsResult<usize> {
268
self.0.as_single_ptr()
269
}
270
271
fn shift(&self, periods: i64) -> Series {
272
ChunkShift::shift(&self.0, periods).into_series()
273
}
274
275
fn sum_reduce(&self) -> PolarsResult<Scalar> {
276
Ok(ChunkAggSeries::sum_reduce(&self.0))
277
}
278
fn max_reduce(&self) -> PolarsResult<Scalar> {
279
Ok(ChunkAggSeries::max_reduce(&self.0))
280
}
281
fn min_reduce(&self) -> PolarsResult<Scalar> {
282
Ok(ChunkAggSeries::min_reduce(&self.0))
283
}
284
fn mean_reduce(&self) -> PolarsResult<Scalar> {
285
Ok(Scalar::new(DataType::Float64, self.mean().into()))
286
}
287
fn median_reduce(&self) -> PolarsResult<Scalar> {
288
let ca = self
289
.0
290
.cast_with_options(&DataType::Int8, CastOptions::Overflowing)
291
.unwrap();
292
let sc = ca.median_reduce()?;
293
let v = sc.value().cast(&DataType::Float64);
294
Ok(Scalar::new(DataType::Float64, v))
295
}
296
/// Get the variance of the Series as a new Series of length 1.
297
fn var_reduce(&self, _ddof: u8) -> PolarsResult<Scalar> {
298
let ca = self
299
.0
300
.cast_with_options(&DataType::Int8, CastOptions::Overflowing)
301
.unwrap();
302
let sc = ca.var_reduce(_ddof)?;
303
let v = sc.value().cast(&DataType::Float64);
304
Ok(Scalar::new(DataType::Float64, v))
305
}
306
/// Get the standard deviation of the Series as a new Series of length 1.
307
fn std_reduce(&self, _ddof: u8) -> PolarsResult<Scalar> {
308
let ca = self
309
.0
310
.cast_with_options(&DataType::Int8, CastOptions::Overflowing)
311
.unwrap();
312
let sc = ca.std_reduce(_ddof)?;
313
let v = sc.value().cast(&DataType::Float64);
314
Ok(Scalar::new(DataType::Float64, v))
315
}
316
fn and_reduce(&self) -> PolarsResult<Scalar> {
317
let dt = DataType::Boolean;
318
319
Ok(Scalar::new(
320
dt,
321
self.0
322
.downcast_iter()
323
.filter(|arr| !arr.is_empty())
324
.filter_map(polars_compute::bitwise::BitwiseKernel::reduce_and)
325
.reduce(|a, b| a & b)
326
.map_or(AnyValue::Null, Into::into),
327
))
328
}
329
fn or_reduce(&self) -> PolarsResult<Scalar> {
330
let dt = DataType::Boolean;
331
332
Ok(Scalar::new(
333
dt,
334
self.0
335
.downcast_iter()
336
.filter(|arr| !arr.is_empty())
337
.filter_map(polars_compute::bitwise::BitwiseKernel::reduce_or)
338
.reduce(|a, b| a | b)
339
.map_or(AnyValue::Null, Into::into),
340
))
341
}
342
fn xor_reduce(&self) -> PolarsResult<Scalar> {
343
let dt = DataType::Boolean;
344
345
Ok(Scalar::new(
346
dt,
347
self.0
348
.downcast_iter()
349
.filter(|arr| !arr.is_empty())
350
.filter_map(polars_compute::bitwise::BitwiseKernel::reduce_xor)
351
.reduce(|a, b| a ^ b)
352
.map_or(AnyValue::Null, Into::into),
353
))
354
}
355
356
#[cfg(feature = "approx_unique")]
357
fn approx_n_unique(&self) -> PolarsResult<IdxSize> {
358
Ok(ChunkApproxNUnique::approx_n_unique(&self.0))
359
}
360
361
fn clone_inner(&self) -> Arc<dyn SeriesTrait> {
362
Arc::new(SeriesWrap(Clone::clone(&self.0)))
363
}
364
365
fn find_validity_mismatch(&self, other: &Series, idxs: &mut Vec<IdxSize>) {
366
self.0.find_validity_mismatch(other, idxs)
367
}
368
369
fn as_any(&self) -> &dyn Any {
370
&self.0
371
}
372
373
fn as_any_mut(&mut self) -> &mut dyn Any {
374
&mut self.0
375
}
376
377
fn as_phys_any(&self) -> &dyn Any {
378
&self.0
379
}
380
381
fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
382
self as _
383
}
384
}
385
386