Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-ops/src/series/ops/floor_divide.rs
6939 views
1
use polars_compute::arithmetic::ArithmeticKernel;
2
use polars_core::chunked_array::ops::arity::apply_binary_kernel_broadcast;
3
use polars_core::prelude::*;
4
#[cfg(feature = "dtype-struct")]
5
use polars_core::series::arithmetic::_struct_arithmetic;
6
use polars_core::series::arithmetic::NumericListOp;
7
use polars_core::with_match_physical_numeric_polars_type;
8
9
fn floor_div_ca<T: PolarsNumericType>(
10
lhs: &ChunkedArray<T>,
11
rhs: &ChunkedArray<T>,
12
) -> ChunkedArray<T> {
13
apply_binary_kernel_broadcast(
14
lhs,
15
rhs,
16
|l, r| ArithmeticKernel::wrapping_floor_div(l.clone(), r.clone()),
17
|l, r| ArithmeticKernel::wrapping_floor_div_scalar_lhs(l, r.clone()),
18
|l, r| ArithmeticKernel::wrapping_floor_div_scalar(l.clone(), r),
19
)
20
}
21
22
pub fn floor_div_series(a: &Series, b: &Series) -> PolarsResult<Series> {
23
match (a.dtype(), b.dtype()) {
24
#[cfg(feature = "dtype-struct")]
25
(DataType::Struct(_), DataType::Struct(_)) => {
26
return _struct_arithmetic(a, b, floor_div_series);
27
},
28
(DataType::List(_), _) | (_, DataType::List(_)) => {
29
return NumericListOp::floor_div().execute(a, b);
30
},
31
#[cfg(feature = "dtype-array")]
32
(DataType::Array(..), _) | (_, DataType::Array(..)) => {
33
return polars_core::series::arithmetic::NumericFixedSizeListOp::floor_div()
34
.execute(a, b);
35
},
36
_ => {},
37
}
38
39
if !a.dtype().is_primitive_numeric() {
40
polars_bail!(op = "floor_div", a.dtype());
41
}
42
43
let logical_type = a.dtype();
44
45
let a = a.to_physical_repr();
46
let b = b.to_physical_repr();
47
48
let out = with_match_physical_numeric_polars_type!(a.dtype(), |$T| {
49
let a: &ChunkedArray<$T> = a.as_ref().as_ref().as_ref();
50
let b: &ChunkedArray<$T> = b.as_ref().as_ref().as_ref();
51
52
floor_div_ca(a, b).into_series()
53
});
54
55
unsafe { out.from_physical_unchecked(logical_type) }
56
}
57
58