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