1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use super::*;
use crate::chunked_array::arg_min_max::{arg_max_opt_iter, arg_min_opt_iter};
#[cfg(feature = "dtype-categorical")]
impl<T: PolarsCategoricalType> CategoricalChunked<T> {
/// # Safety
/// Groups must be in bounds of the array.
pub(crate) unsafe fn agg_min(&self, groups: &GroupsType) -> Series {
let mapping = self.get_mapping();
// Rechunk to a single array so that random index lookups are O(1) rather than O(chunks).
let phys = self.physical().rechunk();
let arr = phys.downcast_as_array();
let cats: ChunkedArray<T::PolarsPhysical> = match groups {
GroupsType::Idx(groups) => {
RAYON.install(|| {
groups
.into_par_iter()
.map(|(first, idx)| {
if idx.is_empty() {
None
} else if idx.len() == 1 {
// SAFETY: group indices are valid by GroupsType invariant
unsafe { arr.get_unchecked(first as usize) }
} else {
let min_pos = arg_min_opt_iter(idx.iter().map(|&i| {
// SAFETY: group indices are valid by GroupsType invariant
unsafe { arr.get_unchecked(i as usize) }.map(|cat| unsafe {
mapping.cat_to_str_unchecked(cat.as_cat())
})
}));
// SAFETY: min_pos points to a non-null element by arg_min_opt_iter contract
min_pos.map(|pos| {
unsafe { arr.get_unchecked(idx[pos] as usize) }.unwrap()
})
}
})
.collect()
})
},
GroupsType::Slice {
groups: groups_slice,
..
} => {
RAYON.install(|| {
groups_slice
.par_iter()
.copied()
.map(|[first, len]| {
let min_pos = arg_min_opt_iter(
(first as usize..first as usize + len as usize).map(|i| {
// SAFETY: slice bounds are valid by GroupsType invariant
unsafe { arr.get_unchecked(i) }.map(|cat| unsafe {
mapping.cat_to_str_unchecked(cat.as_cat())
})
}),
);
// SAFETY: min_pos is within [0, len), so first+min_pos is a valid non-null index
min_pos.map(|pos| {
unsafe { arr.get_unchecked(first as usize + pos) }.unwrap()
})
})
.collect()
})
},
};
let result: CategoricalChunked<T> = unsafe {
CategoricalChunked::from_cats_and_dtype_unchecked(cats, self.dtype().clone())
};
result.into_series()
}
/// # Safety
/// Groups must be in bounds of the array.
pub(crate) unsafe fn agg_max(&self, groups: &GroupsType) -> Series {
let mapping = self.get_mapping();
// Rechunk to a single array so that random index lookups are O(1) rather than O(chunks).
let phys = self.physical().rechunk();
let arr = phys.downcast_as_array();
let cats: ChunkedArray<T::PolarsPhysical> = match groups {
GroupsType::Idx(groups) => {
RAYON.install(|| {
groups
.into_par_iter()
.map(|(first, idx)| {
if idx.is_empty() {
None
} else if idx.len() == 1 {
// SAFETY: group indices are valid by GroupsType invariant
unsafe { arr.get_unchecked(first as usize) }
} else {
let max_pos = arg_max_opt_iter(idx.iter().map(|&i| {
// SAFETY: group indices are valid by GroupsType invariant
unsafe { arr.get_unchecked(i as usize) }.map(|cat| unsafe {
mapping.cat_to_str_unchecked(cat.as_cat())
})
}));
// SAFETY: max_pos points to a non-null element by arg_max_opt_iter contract
max_pos.map(|pos| {
unsafe { arr.get_unchecked(idx[pos] as usize) }.unwrap()
})
}
})
.collect()
})
},
GroupsType::Slice {
groups: groups_slice,
..
} => {
RAYON.install(|| {
groups_slice
.par_iter()
.copied()
.map(|[first, len]| {
let max_pos = arg_max_opt_iter(
(first as usize..first as usize + len as usize).map(|i| {
// SAFETY: slice bounds are valid by GroupsType invariant
unsafe { arr.get_unchecked(i) }.map(|cat| unsafe {
mapping.cat_to_str_unchecked(cat.as_cat())
})
}),
);
// SAFETY: max_pos is within [0, len), so first+max_pos is a valid non-null index
max_pos.map(|pos| {
unsafe { arr.get_unchecked(first as usize + pos) }.unwrap()
})
})
.collect()
})
},
};
let result: CategoricalChunked<T> = unsafe {
CategoricalChunked::from_cats_and_dtype_unchecked(cats, self.dtype().clone())
};
result.into_series()
}
/// # Safety
/// Groups must be in bounds of the array.
pub(crate) unsafe fn agg_arg_min(&self, groups: &GroupsType) -> Series {
let mapping = self.get_mapping();
// Rechunk to a single array so that random index lookups are O(1) rather than O(chunks).
let phys = self.physical().rechunk();
let arr = phys.downcast_as_array();
match groups {
GroupsType::Idx(groups) => {
_agg_helper_idx_idx(groups, |(_, idx)| {
if idx.is_empty() {
None
} else {
arg_min_opt_iter(idx.iter().map(|&i| {
// SAFETY: group indices are valid by GroupsType invariant
unsafe { arr.get_unchecked(i as usize) }
.map(|cat| unsafe { mapping.cat_to_str_unchecked(cat.as_cat()) })
}))
.map(|pos| pos as IdxSize)
}
})
},
GroupsType::Slice {
groups: groups_slice,
..
} => {
_agg_helper_slice_idx(groups_slice, |[first, len]| {
if len == 0 {
None
} else {
arg_min_opt_iter((first as usize..first as usize + len as usize).map(|i| {
// SAFETY: slice bounds are valid by GroupsType invariant
unsafe { arr.get_unchecked(i) }
.map(|cat| unsafe { mapping.cat_to_str_unchecked(cat.as_cat()) })
}))
.map(|pos| pos as IdxSize)
}
})
},
}
}
/// # Safety
/// Groups must be in bounds of the array.
pub(crate) unsafe fn agg_arg_max(&self, groups: &GroupsType) -> Series {
let mapping = self.get_mapping();
// Rechunk to a single array so that random index lookups are O(1) rather than O(chunks).
let phys = self.physical().rechunk();
let arr = phys.downcast_as_array();
match groups {
GroupsType::Idx(groups) => {
_agg_helper_idx_idx(groups, |(_, idx)| {
if idx.is_empty() {
None
} else {
arg_max_opt_iter(idx.iter().map(|&i| {
// SAFETY: group indices are valid by GroupsType invariant
unsafe { arr.get_unchecked(i as usize) }
.map(|cat| unsafe { mapping.cat_to_str_unchecked(cat.as_cat()) })
}))
.map(|pos| pos as IdxSize)
}
})
},
GroupsType::Slice {
groups: groups_slice,
..
} => {
_agg_helper_slice_idx(groups_slice, |[first, len]| {
if len == 0 {
None
} else {
arg_max_opt_iter((first as usize..first as usize + len as usize).map(|i| {
// SAFETY: slice bounds are valid by GroupsType invariant
unsafe { arr.get_unchecked(i) }
.map(|cat| unsafe { mapping.cat_to_str_unchecked(cat.as_cat()) })
}))
.map(|pos| pos as IdxSize)
}
})
},
}
}
}