polars_core/series/implementations/
floats.rs1use num_traits::AsPrimitive;
2use polars_compute::rolling::QuantileMethod;
3
4use super::*;
5use crate::chunked_array::comparison::*;
6#[cfg(feature = "algorithm_group_by")]
7use crate::frame::group_by::*;
8use crate::prelude::*;
9
10macro_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_sum(&self, groups: &GroupsType) -> Series {
84 self.0.agg_sum(groups)
85 }
86
87 #[cfg(feature = "algorithm_group_by")]
88 unsafe fn agg_std(&self, groups: &GroupsType, ddof: u8) -> Series {
89 SeriesWrap::agg_std(self, groups, ddof)
90 }
91
92 #[cfg(feature = "algorithm_group_by")]
93 unsafe fn agg_var(&self, groups: &GroupsType, ddof: u8) -> Series {
94 SeriesWrap::agg_var(self, groups, ddof)
95 }
96
97 #[cfg(feature = "algorithm_group_by")]
98 unsafe fn agg_list(&self, groups: &GroupsType) -> Series {
99 self.0.agg_list(groups)
100 }
101
102 #[cfg(feature = "bitwise")]
103 unsafe fn agg_and(&self, groups: &GroupsType) -> Series {
104 self.0.agg_and(groups)
105 }
106 #[cfg(feature = "bitwise")]
107 unsafe fn agg_or(&self, groups: &GroupsType) -> Series {
108 self.0.agg_or(groups)
109 }
110 #[cfg(feature = "bitwise")]
111 unsafe fn agg_xor(&self, groups: &GroupsType) -> Series {
112 self.0.agg_xor(groups)
113 }
114
115 fn subtract(&self, rhs: &Series) -> PolarsResult<Series> {
116 NumOpsDispatch::subtract(&self.0, rhs)
117 }
118 fn add_to(&self, rhs: &Series) -> PolarsResult<Series> {
119 NumOpsDispatch::add_to(&self.0, rhs)
120 }
121 fn multiply(&self, rhs: &Series) -> PolarsResult<Series> {
122 NumOpsDispatch::multiply(&self.0, rhs)
123 }
124 fn divide(&self, rhs: &Series) -> PolarsResult<Series> {
125 NumOpsDispatch::divide(&self.0, rhs)
126 }
127 fn remainder(&self, rhs: &Series) -> PolarsResult<Series> {
128 NumOpsDispatch::remainder(&self.0, rhs)
129 }
130 #[cfg(feature = "algorithm_group_by")]
131 fn group_tuples(&self, multithreaded: bool, sorted: bool) -> PolarsResult<GroupsType> {
132 IntoGroupsType::group_tuples(&self.0, multithreaded, sorted)
133 }
134
135 fn arg_sort_multiple(
136 &self,
137 by: &[Column],
138 options: &SortMultipleOptions,
139 ) -> PolarsResult<IdxCa> {
140 self.0.arg_sort_multiple(by, options)
141 }
142 }
143
144 impl SeriesTrait for SeriesWrap<$ca> {
145 #[cfg(feature = "rolling_window")]
146 fn rolling_map(
147 &self,
148 _f: &dyn Fn(&Series) -> PolarsResult<Series>,
149 _options: RollingOptionsFixedWindow,
150 ) -> PolarsResult<Series> {
151 ChunkRollApply::rolling_map(&self.0, _f, _options).map(|ca| ca.into_series())
152 }
153
154 fn rename(&mut self, name: PlSmallStr) {
155 self.0.rename(name);
156 }
157
158 fn chunk_lengths(&self) -> ChunkLenIter<'_> {
159 self.0.chunk_lengths()
160 }
161 fn name(&self) -> &PlSmallStr {
162 self.0.name()
163 }
164
165 fn chunks(&self) -> &Vec<ArrayRef> {
166 self.0.chunks()
167 }
168 unsafe fn chunks_mut(&mut self) -> &mut Vec<ArrayRef> {
169 self.0.chunks_mut()
170 }
171 fn shrink_to_fit(&mut self) {
172 self.0.shrink_to_fit()
173 }
174
175 fn slice(&self, offset: i64, length: usize) -> Series {
176 return self.0.slice(offset, length).into_series();
177 }
178
179 fn split_at(&self, offset: i64) -> (Series, Series) {
180 let (a, b) = self.0.split_at(offset);
181 (a.into_series(), b.into_series())
182 }
183
184 fn append(&mut self, other: &Series) -> PolarsResult<()> {
185 polars_ensure!(self.0.dtype() == other.dtype(), append);
186 self.0.append(other.as_ref().as_ref())?;
187 Ok(())
188 }
189 fn append_owned(&mut self, other: Series) -> PolarsResult<()> {
190 polars_ensure!(self.0.dtype() == other.dtype(), append);
191 self.0.append_owned(other.take_inner())
192 }
193
194 fn extend(&mut self, other: &Series) -> PolarsResult<()> {
195 polars_ensure!(self.0.dtype() == other.dtype(), extend);
196 self.0.extend(other.as_ref().as_ref())?;
197 Ok(())
198 }
199
200 fn filter(&self, filter: &BooleanChunked) -> PolarsResult<Series> {
201 ChunkFilter::filter(&self.0, filter).map(|ca| ca.into_series())
202 }
203
204 fn _sum_as_f64(&self) -> f64 {
205 self.0._sum_as_f64()
206 }
207
208 fn mean(&self) -> Option<f64> {
209 self.0.mean()
210 }
211
212 fn median(&self) -> Option<f64> {
213 self.0.median().map(|v| v.as_())
214 }
215
216 fn std(&self, ddof: u8) -> Option<f64> {
217 self.0.std(ddof)
218 }
219
220 fn var(&self, ddof: u8) -> Option<f64> {
221 self.0.var(ddof)
222 }
223
224 fn take(&self, indices: &IdxCa) -> PolarsResult<Series> {
225 Ok(self.0.take(indices)?.into_series())
226 }
227
228 unsafe fn take_unchecked(&self, indices: &IdxCa) -> Series {
229 self.0.take_unchecked(indices).into_series()
230 }
231
232 fn take_slice(&self, indices: &[IdxSize]) -> PolarsResult<Series> {
233 Ok(self.0.take(indices)?.into_series())
234 }
235
236 unsafe fn take_slice_unchecked(&self, indices: &[IdxSize]) -> Series {
237 self.0.take_unchecked(indices).into_series()
238 }
239
240 fn deposit(&self, validity: &Bitmap) -> Series {
241 self.0.deposit(validity).into_series()
242 }
243
244 fn len(&self) -> usize {
245 self.0.len()
246 }
247
248 fn rechunk(&self) -> Series {
249 self.0.rechunk().into_owned().into_series()
250 }
251
252 fn new_from_index(&self, index: usize, length: usize) -> Series {
253 ChunkExpandAtIndex::new_from_index(&self.0, index, length).into_series()
254 }
255
256 fn cast(&self, dtype: &DataType, cast_options: CastOptions) -> PolarsResult<Series> {
257 self.0.cast_with_options(dtype, cast_options)
258 }
259
260 #[inline]
261 unsafe fn get_unchecked(&self, index: usize) -> AnyValue<'_> {
262 self.0.get_any_value_unchecked(index)
263 }
264
265 fn sort_with(&self, options: SortOptions) -> PolarsResult<Series> {
266 Ok(ChunkSort::sort_with(&self.0, options).into_series())
267 }
268
269 fn arg_sort(&self, options: SortOptions) -> IdxCa {
270 ChunkSort::arg_sort(&self.0, options)
271 }
272
273 fn null_count(&self) -> usize {
274 self.0.null_count()
275 }
276
277 fn has_nulls(&self) -> bool {
278 self.0.has_nulls()
279 }
280
281 #[cfg(feature = "algorithm_group_by")]
282 fn unique(&self) -> PolarsResult<Series> {
283 ChunkUnique::unique(&self.0).map(|ca| ca.into_series())
284 }
285
286 #[cfg(feature = "algorithm_group_by")]
287 fn n_unique(&self) -> PolarsResult<usize> {
288 ChunkUnique::n_unique(&self.0)
289 }
290
291 #[cfg(feature = "algorithm_group_by")]
292 fn arg_unique(&self) -> PolarsResult<IdxCa> {
293 ChunkUnique::arg_unique(&self.0)
294 }
295
296 fn unique_id(&self) -> PolarsResult<(IdxSize, Vec<IdxSize>)> {
297 ChunkUnique::unique_id(&self.0)
298 }
299
300 fn is_null(&self) -> BooleanChunked {
301 self.0.is_null()
302 }
303
304 fn is_not_null(&self) -> BooleanChunked {
305 self.0.is_not_null()
306 }
307
308 fn reverse(&self) -> Series {
309 ChunkReverse::reverse(&self.0).into_series()
310 }
311
312 fn as_single_ptr(&mut self) -> PolarsResult<usize> {
313 self.0.as_single_ptr()
314 }
315
316 fn shift(&self, periods: i64) -> Series {
317 ChunkShift::shift(&self.0, periods).into_series()
318 }
319
320 fn sum_reduce(&self) -> PolarsResult<Scalar> {
321 Ok(ChunkAggSeries::sum_reduce(&self.0))
322 }
323 fn max_reduce(&self) -> PolarsResult<Scalar> {
324 Ok(ChunkAggSeries::max_reduce(&self.0))
325 }
326 fn min_reduce(&self) -> PolarsResult<Scalar> {
327 Ok(ChunkAggSeries::min_reduce(&self.0))
328 }
329 fn mean_reduce(&self) -> PolarsResult<Scalar> {
330 let mean = self
331 .mean()
332 .map(AsPrimitive::<<$pdt as PolarsDataType>::OwnedPhysical>::as_);
333 Ok(Scalar::new(self.dtype().clone(), mean.into()))
334 }
335 fn median_reduce(&self) -> PolarsResult<Scalar> {
336 Ok(QuantileAggSeries::median_reduce(&self.0))
337 }
338 fn var_reduce(&self, ddof: u8) -> PolarsResult<Scalar> {
339 Ok(VarAggSeries::var_reduce(&self.0, ddof))
340 }
341 fn std_reduce(&self, ddof: u8) -> PolarsResult<Scalar> {
342 Ok(VarAggSeries::std_reduce(&self.0, ddof))
343 }
344 fn quantile_reduce(
345 &self,
346 quantile: f64,
347 method: QuantileMethod,
348 ) -> PolarsResult<Scalar> {
349 QuantileAggSeries::quantile_reduce(&self.0, quantile, method)
350 }
351 #[cfg(feature = "bitwise")]
352 fn and_reduce(&self) -> PolarsResult<Scalar> {
353 let dt = <$pdt as PolarsDataType>::get_static_dtype();
354 let av = self.0.and_reduce().map_or(AnyValue::Null, Into::into);
355
356 Ok(Scalar::new(dt, av))
357 }
358 #[cfg(feature = "bitwise")]
359 fn or_reduce(&self) -> PolarsResult<Scalar> {
360 let dt = <$pdt as PolarsDataType>::get_static_dtype();
361 let av = self.0.or_reduce().map_or(AnyValue::Null, Into::into);
362
363 Ok(Scalar::new(dt, av))
364 }
365 #[cfg(feature = "bitwise")]
366 fn xor_reduce(&self) -> PolarsResult<Scalar> {
367 let dt = <$pdt as PolarsDataType>::get_static_dtype();
368 let av = self.0.xor_reduce().map_or(AnyValue::Null, Into::into);
369
370 Ok(Scalar::new(dt, av))
371 }
372
373 #[cfg(feature = "approx_unique")]
374 fn approx_n_unique(&self) -> PolarsResult<IdxSize> {
375 Ok(ChunkApproxNUnique::approx_n_unique(&self.0))
376 }
377
378 fn clone_inner(&self) -> Arc<dyn SeriesTrait> {
379 Arc::new(SeriesWrap(Clone::clone(&self.0)))
380 }
381
382 fn find_validity_mismatch(&self, other: &Series, idxs: &mut Vec<IdxSize>) {
383 self.0.find_validity_mismatch(other, idxs)
384 }
385
386 #[cfg(feature = "checked_arithmetic")]
387 fn checked_div(&self, rhs: &Series) -> PolarsResult<Series> {
388 self.0.checked_div(rhs)
389 }
390
391 fn as_any(&self) -> &dyn Any {
392 &self.0
393 }
394
395 fn as_any_mut(&mut self) -> &mut dyn Any {
396 &mut self.0
397 }
398
399 fn as_phys_any(&self) -> &dyn Any {
400 &self.0
401 }
402
403 fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
404 self as _
405 }
406 }
407 };
408}
409
410#[cfg(feature = "dtype-f16")]
411impl_dyn_series!(Float16Chunked, Float16Type);
412impl_dyn_series!(Float32Chunked, Float32Type);
413impl_dyn_series!(Float64Chunked, Float64Type);