polars_ops/series/ops/
arg_min_max.rs1use polars_core::chunked_array::arg_min_max::{
2 arg_max_binary, arg_max_binary_offset, arg_max_bool, arg_max_numeric, arg_max_str,
3 arg_min_binary, arg_min_binary_offset, arg_min_bool, arg_min_max_row_encoded, arg_min_numeric,
4 arg_min_str,
5};
6#[cfg(feature = "dtype-categorical")]
7use polars_core::chunked_array::arg_min_max::{arg_max_cat, arg_min_cat};
8#[cfg(feature = "dtype-categorical")]
9use polars_core::with_match_categorical_physical_type;
10
11use super::*;
12
13pub trait ArgAgg {
15 fn arg_min(&self) -> Option<usize>;
17 fn arg_max(&self) -> Option<usize>;
19}
20
21macro_rules! with_match_physical_numeric_polars_type {(
22 $key_type:expr, | $_:tt $T:ident | $($body:tt)*
23) => ({
24 macro_rules! __with_ty__ {( $_ $T:ident ) => ( $($body)* )}
25 use DataType::*;
26 match $key_type {
27 #[cfg(feature = "dtype-i8")]
28 Int8 => __with_ty__! { Int8Type },
29 #[cfg(feature = "dtype-i16")]
30 Int16 => __with_ty__! { Int16Type },
31 Int32 => __with_ty__! { Int32Type },
32 Int64 => __with_ty__! { Int64Type },
33 #[cfg(feature = "dtype-i128")]
34 Int128 => __with_ty__! { Int128Type },
35 #[cfg(feature = "dtype-u8")]
36 UInt8 => __with_ty__! { UInt8Type },
37 #[cfg(feature = "dtype-u16")]
38 UInt16 => __with_ty__! { UInt16Type },
39 UInt32 => __with_ty__! { UInt32Type },
40 UInt64 => __with_ty__! { UInt64Type },
41 #[cfg(feature = "dtype-u128")]
42 UInt128 => __with_ty__! { UInt128Type },
43 #[cfg(feature = "dtype-f16")]
44 Float16 => __with_ty__! { Float16Type },
45 Float32 => __with_ty__! { Float32Type },
46 Float64 => __with_ty__! { Float64Type },
47 dt => panic!("not implemented for dtype {:?}", dt),
48 }
49})}
50
51impl ArgAgg for Series {
52 fn arg_min(&self) -> Option<usize> {
53 use DataType::*;
54 let phys_s = self.to_physical_repr();
55 match self.dtype() {
56 #[cfg(feature = "dtype-extension")]
57 Extension(_, _) => self.to_storage().arg_min(),
58 #[cfg(feature = "dtype-categorical")]
59 Categorical(cats, _) => {
60 with_match_categorical_physical_type!(cats.physical(), |$C| {
61 arg_min_cat(self.cat::<$C>().unwrap())
62 })
63 },
64 #[cfg(feature = "dtype-categorical")]
65 Enum(_, _) => phys_s.arg_min(),
66 #[cfg(feature = "dtype-decimal")]
67 Decimal(_, _) => phys_s.arg_min(),
68 Date | Datetime(_, _) | Duration(_) | Time => phys_s.arg_min(),
69 String => arg_min_str(self.str().unwrap()),
70 Binary => arg_min_binary(self.binary().unwrap()),
71 BinaryOffset => arg_min_binary_offset(self.binary_offset().unwrap()),
72 Boolean => arg_min_bool(self.bool().unwrap()),
73 dt if dt.is_primitive_numeric() => {
74 with_match_physical_numeric_polars_type!(phys_s.dtype(), |$T| {
75 let ca: &ChunkedArray<$T> = phys_s.as_ref().as_ref().as_ref();
76 arg_min_numeric(ca)
77 })
78 },
79 dt if dt.is_nested() => arg_min_max_row_encoded(self, false),
80 _ => None,
81 }
82 }
83
84 fn arg_max(&self) -> Option<usize> {
85 use DataType::*;
86 let phys_s = self.to_physical_repr();
87 match self.dtype() {
88 #[cfg(feature = "dtype-extension")]
89 Extension(_, _) => self.to_storage().arg_max(),
90 #[cfg(feature = "dtype-categorical")]
91 Categorical(cats, _) => {
92 with_match_categorical_physical_type!(cats.physical(), |$C| {
93 arg_max_cat(self.cat::<$C>().unwrap())
94 })
95 },
96 #[cfg(feature = "dtype-categorical")]
97 Enum(_, _) => phys_s.arg_max(),
98 #[cfg(feature = "dtype-decimal")]
99 Decimal(_, _) => phys_s.arg_max(),
100 Date | Datetime(_, _) | Duration(_) | Time => phys_s.arg_max(),
101 String => arg_max_str(self.str().unwrap()),
102 Binary => arg_max_binary(self.binary().unwrap()),
103 BinaryOffset => arg_max_binary_offset(self.binary_offset().unwrap()),
104 Boolean => arg_max_bool(self.bool().unwrap()),
105 dt if dt.is_primitive_numeric() => {
106 with_match_physical_numeric_polars_type!(phys_s.dtype(), |$T| {
107 let ca: &ChunkedArray<$T> = phys_s.as_ref().as_ref().as_ref();
108 arg_max_numeric(ca)
109 })
110 },
111 dt if dt.is_nested() => arg_min_max_row_encoded(self, true),
112 _ => None,
113 }
114 }
115}