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/floats.rs
8327 views
1
use num_traits::AsPrimitive;
2
use polars_compute::rolling::QuantileMethod;
3
4
use super::*;
5
use crate::chunked_array::comparison::*;
6
#[cfg(feature = "algorithm_group_by")]
7
use crate::frame::group_by::*;
8
use crate::prelude::*;
9
10
macro_rules! impl_dyn_series {
11
($ca: ident, $pdt:ident) => {
12
impl private::PrivateSeries for SeriesWrap<$ca> {
13
fn compute_len(&mut self) {
14
self.0.compute_len()
15
}
16
fn _field(&self) -> Cow<'_, Field> {
17
Cow::Borrowed(self.0.ref_field())
18
}
19
fn _dtype(&self) -> &DataType {
20
self.0.ref_field().dtype()
21
}
22
23
fn _set_flags(&mut self, flags: StatisticsFlags) {
24
self.0.set_flags(flags)
25
}
26
fn _get_flags(&self) -> StatisticsFlags {
27
self.0.get_flags()
28
}
29
unsafe fn equal_element(
30
&self,
31
idx_self: usize,
32
idx_other: usize,
33
other: &Series,
34
) -> bool {
35
self.0.equal_element(idx_self, idx_other, other)
36
}
37
38
#[cfg(feature = "zip_with")]
39
fn zip_with_same_type(
40
&self,
41
mask: &BooleanChunked,
42
other: &Series,
43
) -> PolarsResult<Series> {
44
ChunkZip::zip_with(&self.0, mask, other.as_ref().as_ref())
45
.map(|ca| ca.into_series())
46
}
47
fn into_total_eq_inner<'a>(&'a self) -> Box<dyn TotalEqInner + 'a> {
48
(&self.0).into_total_eq_inner()
49
}
50
fn into_total_ord_inner<'a>(&'a self) -> Box<dyn TotalOrdInner + 'a> {
51
(&self.0).into_total_ord_inner()
52
}
53
54
fn vec_hash(
55
&self,
56
random_state: PlSeedableRandomStateQuality,
57
buf: &mut Vec<u64>,
58
) -> PolarsResult<()> {
59
self.0.vec_hash(random_state, buf)?;
60
Ok(())
61
}
62
63
fn vec_hash_combine(
64
&self,
65
build_hasher: PlSeedableRandomStateQuality,
66
hashes: &mut [u64],
67
) -> PolarsResult<()> {
68
self.0.vec_hash_combine(build_hasher, hashes)?;
69
Ok(())
70
}
71
72
#[cfg(feature = "algorithm_group_by")]
73
unsafe fn agg_min(&self, groups: &GroupsType) -> Series {
74
self.0.agg_min(groups)
75
}
76
77
#[cfg(feature = "algorithm_group_by")]
78
unsafe fn agg_max(&self, groups: &GroupsType) -> Series {
79
self.0.agg_max(groups)
80
}
81
82
#[cfg(feature = "algorithm_group_by")]
83
unsafe fn agg_arg_min(&self, groups: &GroupsType) -> Series {
84
self.0.agg_arg_min(groups)
85
}
86
87
#[cfg(feature = "algorithm_group_by")]
88
unsafe fn agg_arg_max(&self, groups: &GroupsType) -> Series {
89
self.0.agg_arg_max(groups)
90
}
91
92
#[cfg(feature = "algorithm_group_by")]
93
unsafe fn agg_sum(&self, groups: &GroupsType) -> Series {
94
self.0.agg_sum(groups)
95
}
96
97
#[cfg(feature = "algorithm_group_by")]
98
unsafe fn agg_std(&self, groups: &GroupsType, ddof: u8) -> Series {
99
SeriesWrap::agg_std(self, groups, ddof)
100
}
101
102
#[cfg(feature = "algorithm_group_by")]
103
unsafe fn agg_var(&self, groups: &GroupsType, ddof: u8) -> Series {
104
SeriesWrap::agg_var(self, groups, ddof)
105
}
106
107
#[cfg(feature = "algorithm_group_by")]
108
unsafe fn agg_list(&self, groups: &GroupsType) -> Series {
109
self.0.agg_list(groups)
110
}
111
112
#[cfg(feature = "bitwise")]
113
unsafe fn agg_and(&self, groups: &GroupsType) -> Series {
114
self.0.agg_and(groups)
115
}
116
#[cfg(feature = "bitwise")]
117
unsafe fn agg_or(&self, groups: &GroupsType) -> Series {
118
self.0.agg_or(groups)
119
}
120
#[cfg(feature = "bitwise")]
121
unsafe fn agg_xor(&self, groups: &GroupsType) -> Series {
122
self.0.agg_xor(groups)
123
}
124
125
fn subtract(&self, rhs: &Series) -> PolarsResult<Series> {
126
NumOpsDispatch::subtract(&self.0, rhs)
127
}
128
fn add_to(&self, rhs: &Series) -> PolarsResult<Series> {
129
NumOpsDispatch::add_to(&self.0, rhs)
130
}
131
fn multiply(&self, rhs: &Series) -> PolarsResult<Series> {
132
NumOpsDispatch::multiply(&self.0, rhs)
133
}
134
fn divide(&self, rhs: &Series) -> PolarsResult<Series> {
135
NumOpsDispatch::divide(&self.0, rhs)
136
}
137
fn remainder(&self, rhs: &Series) -> PolarsResult<Series> {
138
NumOpsDispatch::remainder(&self.0, rhs)
139
}
140
#[cfg(feature = "algorithm_group_by")]
141
fn group_tuples(&self, multithreaded: bool, sorted: bool) -> PolarsResult<GroupsType> {
142
IntoGroupsType::group_tuples(&self.0, multithreaded, sorted)
143
}
144
145
fn arg_sort_multiple(
146
&self,
147
by: &[Column],
148
options: &SortMultipleOptions,
149
) -> PolarsResult<IdxCa> {
150
self.0.arg_sort_multiple(by, options)
151
}
152
}
153
154
impl SeriesTrait for SeriesWrap<$ca> {
155
#[cfg(feature = "rolling_window")]
156
fn rolling_map(
157
&self,
158
_f: &dyn Fn(&Series) -> PolarsResult<Series>,
159
_options: RollingOptionsFixedWindow,
160
) -> PolarsResult<Series> {
161
ChunkRollApply::rolling_map(&self.0, _f, _options).map(|ca| ca.into_series())
162
}
163
164
fn rename(&mut self, name: PlSmallStr) {
165
self.0.rename(name);
166
}
167
168
fn chunk_lengths(&self) -> ChunkLenIter<'_> {
169
self.0.chunk_lengths()
170
}
171
fn name(&self) -> &PlSmallStr {
172
self.0.name()
173
}
174
175
fn chunks(&self) -> &Vec<ArrayRef> {
176
self.0.chunks()
177
}
178
unsafe fn chunks_mut(&mut self) -> &mut Vec<ArrayRef> {
179
self.0.chunks_mut()
180
}
181
fn shrink_to_fit(&mut self) {
182
self.0.shrink_to_fit()
183
}
184
185
fn slice(&self, offset: i64, length: usize) -> Series {
186
return self.0.slice(offset, length).into_series();
187
}
188
189
fn split_at(&self, offset: i64) -> (Series, Series) {
190
let (a, b) = self.0.split_at(offset);
191
(a.into_series(), b.into_series())
192
}
193
194
fn append(&mut self, other: &Series) -> PolarsResult<()> {
195
polars_ensure!(self.0.dtype() == other.dtype(), append);
196
self.0.append(other.as_ref().as_ref())?;
197
Ok(())
198
}
199
fn append_owned(&mut self, other: Series) -> PolarsResult<()> {
200
polars_ensure!(self.0.dtype() == other.dtype(), append);
201
self.0.append_owned(other.take_inner())
202
}
203
204
fn extend(&mut self, other: &Series) -> PolarsResult<()> {
205
polars_ensure!(self.0.dtype() == other.dtype(), extend);
206
self.0.extend(other.as_ref().as_ref())?;
207
Ok(())
208
}
209
210
fn filter(&self, filter: &BooleanChunked) -> PolarsResult<Series> {
211
ChunkFilter::filter(&self.0, filter).map(|ca| ca.into_series())
212
}
213
214
fn _sum_as_f64(&self) -> f64 {
215
self.0._sum_as_f64()
216
}
217
218
fn mean(&self) -> Option<f64> {
219
self.0.mean()
220
}
221
222
fn median(&self) -> Option<f64> {
223
self.0.median().map(|v| v.as_())
224
}
225
226
fn std(&self, ddof: u8) -> Option<f64> {
227
self.0.std(ddof)
228
}
229
230
fn var(&self, ddof: u8) -> Option<f64> {
231
self.0.var(ddof)
232
}
233
234
fn take(&self, indices: &IdxCa) -> PolarsResult<Series> {
235
Ok(self.0.take(indices)?.into_series())
236
}
237
238
unsafe fn take_unchecked(&self, indices: &IdxCa) -> Series {
239
self.0.take_unchecked(indices).into_series()
240
}
241
242
fn take_slice(&self, indices: &[IdxSize]) -> PolarsResult<Series> {
243
Ok(self.0.take(indices)?.into_series())
244
}
245
246
unsafe fn take_slice_unchecked(&self, indices: &[IdxSize]) -> Series {
247
self.0.take_unchecked(indices).into_series()
248
}
249
250
fn deposit(&self, validity: &Bitmap) -> Series {
251
self.0.deposit(validity).into_series()
252
}
253
254
fn len(&self) -> usize {
255
self.0.len()
256
}
257
258
fn rechunk(&self) -> Series {
259
self.0.rechunk().into_owned().into_series()
260
}
261
262
fn new_from_index(&self, index: usize, length: usize) -> Series {
263
ChunkExpandAtIndex::new_from_index(&self.0, index, length).into_series()
264
}
265
266
fn cast(&self, dtype: &DataType, cast_options: CastOptions) -> PolarsResult<Series> {
267
self.0.cast_with_options(dtype, cast_options)
268
}
269
270
#[inline]
271
unsafe fn get_unchecked(&self, index: usize) -> AnyValue<'_> {
272
self.0.get_any_value_unchecked(index)
273
}
274
275
fn sort_with(&self, options: SortOptions) -> PolarsResult<Series> {
276
Ok(ChunkSort::sort_with(&self.0, options).into_series())
277
}
278
279
fn arg_sort(&self, options: SortOptions) -> IdxCa {
280
ChunkSort::arg_sort(&self.0, options)
281
}
282
283
fn null_count(&self) -> usize {
284
self.0.null_count()
285
}
286
287
fn has_nulls(&self) -> bool {
288
self.0.has_nulls()
289
}
290
291
#[cfg(feature = "algorithm_group_by")]
292
fn unique(&self) -> PolarsResult<Series> {
293
ChunkUnique::unique(&self.0).map(|ca| ca.into_series())
294
}
295
296
#[cfg(feature = "algorithm_group_by")]
297
fn n_unique(&self) -> PolarsResult<usize> {
298
ChunkUnique::n_unique(&self.0)
299
}
300
301
#[cfg(feature = "algorithm_group_by")]
302
fn arg_unique(&self) -> PolarsResult<IdxCa> {
303
ChunkUnique::arg_unique(&self.0)
304
}
305
306
fn unique_id(&self) -> PolarsResult<(IdxSize, Vec<IdxSize>)> {
307
ChunkUnique::unique_id(&self.0)
308
}
309
310
fn is_null(&self) -> BooleanChunked {
311
self.0.is_null()
312
}
313
314
fn is_not_null(&self) -> BooleanChunked {
315
self.0.is_not_null()
316
}
317
318
fn reverse(&self) -> Series {
319
ChunkReverse::reverse(&self.0).into_series()
320
}
321
322
fn as_single_ptr(&mut self) -> PolarsResult<usize> {
323
self.0.as_single_ptr()
324
}
325
326
fn shift(&self, periods: i64) -> Series {
327
ChunkShift::shift(&self.0, periods).into_series()
328
}
329
330
fn sum_reduce(&self) -> PolarsResult<Scalar> {
331
Ok(ChunkAggSeries::sum_reduce(&self.0))
332
}
333
fn max_reduce(&self) -> PolarsResult<Scalar> {
334
Ok(ChunkAggSeries::max_reduce(&self.0))
335
}
336
fn min_reduce(&self) -> PolarsResult<Scalar> {
337
Ok(ChunkAggSeries::min_reduce(&self.0))
338
}
339
fn mean_reduce(&self) -> PolarsResult<Scalar> {
340
let mean = self
341
.mean()
342
.map(AsPrimitive::<<$pdt as PolarsDataType>::OwnedPhysical>::as_);
343
Ok(Scalar::new(self.dtype().clone(), mean.into()))
344
}
345
fn median_reduce(&self) -> PolarsResult<Scalar> {
346
Ok(QuantileAggSeries::median_reduce(&self.0))
347
}
348
fn var_reduce(&self, ddof: u8) -> PolarsResult<Scalar> {
349
Ok(VarAggSeries::var_reduce(&self.0, ddof))
350
}
351
fn std_reduce(&self, ddof: u8) -> PolarsResult<Scalar> {
352
Ok(VarAggSeries::std_reduce(&self.0, ddof))
353
}
354
355
fn quantile_reduce(
356
&self,
357
quantile: f64,
358
method: QuantileMethod,
359
) -> PolarsResult<Scalar> {
360
QuantileAggSeries::quantile_reduce(&self.0, quantile, method)
361
}
362
363
fn quantiles_reduce(
364
&self,
365
quantiles: &[f64],
366
method: QuantileMethod,
367
) -> PolarsResult<Scalar> {
368
QuantileAggSeries::quantiles_reduce(&self.0, quantiles, method)
369
}
370
371
#[cfg(feature = "bitwise")]
372
fn and_reduce(&self) -> PolarsResult<Scalar> {
373
let dt = <$pdt as PolarsDataType>::get_static_dtype();
374
let av = self.0.and_reduce().map_or(AnyValue::Null, Into::into);
375
376
Ok(Scalar::new(dt, av))
377
}
378
#[cfg(feature = "bitwise")]
379
fn or_reduce(&self) -> PolarsResult<Scalar> {
380
let dt = <$pdt as PolarsDataType>::get_static_dtype();
381
let av = self.0.or_reduce().map_or(AnyValue::Null, Into::into);
382
383
Ok(Scalar::new(dt, av))
384
}
385
#[cfg(feature = "bitwise")]
386
fn xor_reduce(&self) -> PolarsResult<Scalar> {
387
let dt = <$pdt as PolarsDataType>::get_static_dtype();
388
let av = self.0.xor_reduce().map_or(AnyValue::Null, Into::into);
389
390
Ok(Scalar::new(dt, av))
391
}
392
393
#[cfg(feature = "approx_unique")]
394
fn approx_n_unique(&self) -> PolarsResult<IdxSize> {
395
Ok(ChunkApproxNUnique::approx_n_unique(&self.0))
396
}
397
398
fn clone_inner(&self) -> Arc<dyn SeriesTrait> {
399
Arc::new(SeriesWrap(Clone::clone(&self.0)))
400
}
401
402
fn find_validity_mismatch(&self, other: &Series, idxs: &mut Vec<IdxSize>) {
403
self.0.find_validity_mismatch(other, idxs)
404
}
405
406
#[cfg(feature = "checked_arithmetic")]
407
fn checked_div(&self, rhs: &Series) -> PolarsResult<Series> {
408
self.0.checked_div(rhs)
409
}
410
411
fn as_any(&self) -> &dyn Any {
412
&self.0
413
}
414
415
fn as_any_mut(&mut self) -> &mut dyn Any {
416
&mut self.0
417
}
418
419
fn as_phys_any(&self) -> &dyn Any {
420
&self.0
421
}
422
423
fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
424
self as _
425
}
426
}
427
};
428
}
429
430
#[cfg(feature = "dtype-f16")]
431
impl_dyn_series!(Float16Chunked, Float16Type);
432
impl_dyn_series!(Float32Chunked, Float32Type);
433
impl_dyn_series!(Float64Chunked, Float64Type);
434
435