-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
[WIP] ENH: Groupby mode #39867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] ENH: Groupby mode #39867
Changes from all commits
10d72e8
f318f24
290bea9
6418db7
284e024
4ca6748
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,177 @@ | ||||
{{py: | ||||
|
||||
# name | ||||
cimported_types = [#'complex64', | ||||
#'complex128', | ||||
'float32', | ||||
'float64', | ||||
'int8', | ||||
'int16', | ||||
'int32', | ||||
'int64', | ||||
'pymap', | ||||
'str', | ||||
'strbox', | ||||
'uint8', | ||||
'uint16', | ||||
'uint32', | ||||
'uint64'] | ||||
}} | ||||
|
||||
{{for name in cimported_types}} | ||||
from pandas._libs.khash cimport ( | ||||
kh_destroy_{{name}}, | ||||
kh_exist_{{name}}, | ||||
kh_get_{{name}}, | ||||
kh_init_{{name}}, | ||||
kh_put_{{name}}, | ||||
kh_resize_{{name}}, | ||||
kh_{{name}}_t, | ||||
) | ||||
|
||||
{{endfor}} | ||||
|
||||
from pandas._libs.khash cimport ( | ||||
#khcomplex64_t, | ||||
#khcomplex128_t, | ||||
khiter_t, | ||||
) | ||||
|
||||
from pandas._libs.hashtable import ( | ||||
# NaN checking | ||||
#is_nan_khcomplex128_t, | ||||
#is_nan_khcomplex64_t, | ||||
is_nan_float64_t, | ||||
is_nan_float32_t, | ||||
is_nan_int64_t, | ||||
is_nan_int32_t, | ||||
is_nan_int16_t, | ||||
is_nan_int8_t, | ||||
is_nan_uint64_t, | ||||
is_nan_uint32_t, | ||||
is_nan_uint16_t, | ||||
is_nan_uint8_t, | ||||
# Casting | ||||
#to_complex64, | ||||
#to_complex128, | ||||
#to_khcomplex128_t, | ||||
#to_khcomplex64_t, | ||||
) | ||||
|
||||
{{py: | ||||
# TODO: add complex64 and complex128 (requires comparisons between complex numbers) | ||||
# dtype, ttype, c_type, to_c_type, to_dtype | ||||
dtypes = [#('complex128', 'complex128', 'khcomplex128_t', | ||||
# 'to_khcomplex128_t', 'to_complex128'), | ||||
#('complex64', 'complex64', 'khcomplex64_t', | ||||
# 'to_khcomplex64_t', 'to_complex64'), | ||||
('float64', 'float64', 'float64_t', '', ''), | ||||
('float32', 'float32', 'float32_t', '', ''), | ||||
('uint64', 'uint64', 'uint64_t', '', ''), | ||||
('uint32', 'uint32', 'uint32_t', '', ''), | ||||
('uint16', 'uint16', 'uint16_t', '', ''), | ||||
('uint8', 'uint8', 'uint8_t', '', ''), | ||||
('object', 'pymap', 'object', '', ''), | ||||
('int64', 'int64', 'int64_t', '', ''), | ||||
('int32', 'int32', 'int32_t', '', ''), | ||||
('int16', 'int16', 'int16_t', '', ''), | ||||
('int8', 'int8', 'int8_t', '', '')] | ||||
|
||||
}} | ||||
|
||||
{{for dtype, ttype, c_type, to_c_type, to_dtype in dtypes}} | ||||
|
||||
|
||||
@cython.wraparound(False) | ||||
@cython.boundscheck(False) | ||||
cdef {{c_type}} calc_mode_{{dtype}}(kh_{{ttype}}_t *table): | ||||
cdef: | ||||
{{c_type}} mode = 0 # fix annoying uninitialized warning | ||||
{{c_type}} val | ||||
int count, max_count = 0 | ||||
khiter_t k | ||||
|
||||
for k in range(table.n_buckets): | ||||
if kh_exist_{{ttype}}(table, k): | ||||
count = table.vals[k] | ||||
{{if dtype != 'object'}} | ||||
val = table.keys[k] | ||||
if count == max_count and val < mode: | ||||
{{else}} | ||||
val = <object>table.keys[k] | ||||
if count == max_count: | ||||
{{endif}} | ||||
mode = val | ||||
elif count > max_count: | ||||
mode = val | ||||
max_count = count | ||||
return mode | ||||
|
||||
|
||||
@cython.wraparound(False) | ||||
@cython.boundscheck(False) | ||||
def group_mode_{{dtype}}(ndarray[{{c_type}}, ndim=1] out, | ||||
ndarray[{{c_type}}, ndim=1] values, | ||||
ndarray[int64_t, ndim=1] labels, | ||||
bint dropna = True): | ||||
""" | ||||
Calculates the mode of each group. | ||||
If multimodal returns the smallest mode in each group if numeric. | ||||
For all other datatypes, returns a mode. | ||||
""" | ||||
cdef: | ||||
Py_ssize_t i, N = len(values) | ||||
int64_t lab, curr_label = -1 | ||||
kh_{{ttype}}_t *table | ||||
khiter_t k | ||||
int ret = 0 | ||||
|
||||
table = kh_init_{{ttype}}() | ||||
{{if dtype != 'object'}} | ||||
#TODO: Fix NOGIL later | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The non-numeric code paths prevent the Gil from being released. At least until Cython 3 gets released we usually do something like this: pandas/pandas/_libs/groupby.pyx Line 928 in 859a787
|
||||
#with nogil: | ||||
for i in range(N): | ||||
lab = labels[i] | ||||
if lab < 0: # NaN case | ||||
continue | ||||
if lab != curr_label and curr_label != -1: | ||||
out[curr_label] = calc_mode_{{dtype}}(table) | ||||
# Reset variables | ||||
max_count = 0 | ||||
table = kh_init_{{ttype}}() | ||||
|
||||
val = {{to_c_type}}(values[i]) | ||||
|
||||
if not is_nan_{{c_type}}(val) or not dropna: | ||||
k = kh_get_{{ttype}}(table, val) | ||||
if k != table.n_buckets: | ||||
table.vals[k] += 1 | ||||
else: | ||||
k = kh_put_{{ttype}}(table, val, &ret) | ||||
table.vals[k] = 1 | ||||
curr_label = lab | ||||
# Calc mode for the last group | ||||
out[curr_label] = calc_mode_{{dtype}}(table) | ||||
{{else}} | ||||
for i in range(N): | ||||
lab = labels[i] | ||||
if lab < 0: # NaN case | ||||
continue | ||||
if lab != curr_label and curr_label != -1: | ||||
out[curr_label] = calc_mode_{{dtype}}(table) | ||||
# Reset variables | ||||
table = kh_init_{{ttype}}() | ||||
|
||||
val = values[i] | ||||
if not checknull(val) or not dropna: | ||||
k = kh_get_{{ttype}}(table, <PyObject*>val) | ||||
if k != table.n_buckets: | ||||
table.vals[k] += 1 | ||||
else: | ||||
k = kh_put_{{ttype}}(table, <PyObject*>val, &ret) | ||||
table.vals[k] = 1 | ||||
curr_label = lab | ||||
out[curr_label] = calc_mode_{{dtype}}(table) | ||||
{{endif}} | ||||
kh_destroy_{{ttype}}(table) | ||||
{{endfor}} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1578,6 +1578,35 @@ def median(self, numeric_only=True): | |
numeric_only=numeric_only, | ||
) | ||
|
||
@final | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this not just value counts(sort=True, dropna=True)[0]? why are we adding a whole bunch of cython code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well value_counts is about twice as slow as my current implementation(takes same time for 1 column as my implementation does for 2), and all the other methods have corresponding cython code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the code that can do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah - whoops, I was trying to use DataFrameGroupBy instead of SeriesGroupBy. Ignore the above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
can you show some timings. I am really hesistant to add this much code for a niche case. |
||
@Substitution(name="groupby") | ||
@Appender(_common_see_also) | ||
def mode(self, dropna=True, numeric_only=False): | ||
""" | ||
Compute mode of groups, excluding missing values. If a group has | ||
multiple modes, the smallest mode will be used. | ||
|
||
Parameters | ||
---------- | ||
dropna : bool, default True | ||
Do not use NaNs in mode calculation | ||
numeric_only: bool, default False | ||
Include only float, int, boolean columns. If None, will attempt to use | ||
everything, then use only numeric data. | ||
Returns | ||
------- | ||
Series or DataFrame | ||
Mode of values within each group. | ||
""" | ||
# Note: get_cythonized_result iterates in python, slow for many columns? | ||
return self._get_cythonized_result( | ||
"group_mode", | ||
aggregate=True, | ||
numeric_only=numeric_only, | ||
needs_values=True, | ||
dropna=dropna, | ||
) | ||
|
||
@final | ||
@Substitution(name="groupby") | ||
@Appender(_common_see_also) | ||
|
@@ -2585,7 +2614,7 @@ def cummax(self, axis=0, **kwargs): | |
def _get_cythonized_result( | ||
self, | ||
how: str, | ||
cython_dtype: np.dtype, | ||
cython_dtype: np.dtype = None, | ||
aggregate: bool = False, | ||
numeric_only: bool = True, | ||
needs_counts: bool = False, | ||
|
@@ -2666,12 +2695,18 @@ def _get_cythonized_result( | |
|
||
labels, _, ngroups = grouper.group_info | ||
output: Dict[base.OutputKey, np.ndarray] = {} | ||
base_func = getattr(libgroupby, how) | ||
if cython_dtype is not None: | ||
base_func = getattr(libgroupby, how) | ||
|
||
error_msg = "" | ||
for idx, obj in enumerate(self._iterate_slices()): | ||
name = obj.name | ||
values = obj._values | ||
if cython_dtype is None: | ||
cython_dtype = values.dtype | ||
# We also need to get the specific function for that dtype | ||
how += f"_{cython_dtype}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using cython fused types should make cython figure this out for us (also avoids need for tempita) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have to use tempita unfortunately because of the hashtables have different functions names based on dtype. |
||
base_func = getattr(libgroupby, how) | ||
|
||
if numeric_only and not is_numeric_dtype(values): | ||
continue | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -388,3 +388,25 @@ def test_cython_agg_EA_known_dtypes(data, op_name, action, with_na): | |
|
||
result = grouped["col"].aggregate(op_name) | ||
assert result.dtype == expected_dtype | ||
|
||
|
||
def test_mode_numeric(): | ||
data = { | ||
"A": [0, 0, 0, 0, 1, 1, 1, 1, 1, 1.0, np.nan, np.nan], | ||
"B": ["A", "B"] * 6, | ||
"C": [2, 4, 3, 1, 2, 3, 4, 5, 2, 7, 9, 9], | ||
} | ||
df = DataFrame(data) | ||
df.drop(columns="B", inplace=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the expected result for column |
||
# Group by 1 column | ||
result = df.groupby("A").mode() | ||
exp = DataFrame({"C": [1, 2]}, index=Series([0.0, 1.0], name="A")) | ||
tm.assert_frame_equal(result, exp) | ||
# Group by 2 column | ||
df = DataFrame(data) | ||
result = df.groupby(by=["A", "B"]).mode() | ||
exp = DataFrame( | ||
{"C": [3, 1, 2, 7]}, | ||
index=pd.MultiIndex.from_product([[0.0, 1.0], ["A", "B"]], names=["A", "B"]), | ||
) | ||
tm.assert_frame_equal(result, exp) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -285,6 +285,7 @@ def test_tab_completion(mframe): | |
"mean", | ||
"median", | ||
"min", | ||
"mode", | ||
"ngroups", | ||
"nth", | ||
"ohlc", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To @jbrockmendel point a lot of this can be simplified if using fused types instead of a template; we've started to move to that in most other places in Cython