-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Cythonized GroupBy Quantile #20405
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
Cythonized GroupBy Quantile #20405
Changes from 12 commits
618ec99
74871d8
7b6ca68
31aff03
4a43815
eb18823
813da81
e152dd5
7a8fefb
b4938ba
3f7d0a9
d7aec3f
e712946
72cd30e
a3c4b11
ac96526
7d439d8
3047eed
70bf89a
02eb336
7c3c349
3b9c7c4
ad8b184
b846bc2
93b122c
09308d4
1a718f2
ff062bd
bdb5089
9b55fb5
31e66fc
41a734f
67e0f00
07b0c00
86aeb4a
86b9d8d
cfa1b45
00085d0
1f02532
3c64c1f
09695f5
68cfed9
4ce1448
5e840da
7969fb6
f9a8317
464a831
4b3f9be
b996e1d
cdd8985
64f46a3
4d88e8a
1cd93dd
9ae23c1
eb99f07
94d4892
0512f37
2370129
a018570
f41cd05
21691bb
082aea3
dc5877a
7496a9b
ec013bf
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,6 @@ | ||
cdef enum InterpolationEnumType: | ||
INTERPOLATION_LINEAR, | ||
INTERPOLATION_LOWER, | ||
INTERPOLATION_HIGHER, | ||
INTERPOLATION_NEAREST, | ||
INTERPOLATION_MIDPOINT |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1732,6 +1732,70 @@ def nth(self, n, dropna=None): | |
|
||
return result | ||
|
||
def quantile(self, q=0.5, interpolation='linear'): | ||
""" | ||
Return group values at the given quantile, a la numpy.percentile. | ||
|
||
Parameters | ||
---------- | ||
q : float or array-like, default 0.5 (50% quantile) | ||
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. checking this should share code with Series.quantile. (or be in cython is ok) |
||
0 <= q <= 1, the quantile(s) to compute | ||
interpolation : str | ||
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 list the methods here |
||
Method to use when the desired quantile falls between two points. | ||
|
||
Returns | ||
------- | ||
Series or DataFrame | ||
Return type determined by caller of GroupBy object. | ||
|
||
See Also | ||
-------- | ||
Series.quantile : Similar method for Series | ||
DataFrame.quantile : Similar method for DataFrame | ||
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 add numpy.percentile |
||
|
||
Examples | ||
-------- | ||
>>> df = pd.DataFrame( | ||
... [['foo'] * 5 + ['bar'] * 5, | ||
... [1, 2, 3, 4, 5, 5, 4, 3, 2, 1]], | ||
... columns=['key', 'val']) | ||
>>> df | ||
""" | ||
|
||
is_dt = False | ||
is_int = False | ||
|
||
def pre_processor(vals): | ||
if vals.dtype == np.object: | ||
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. we really really need to clean this up and simply put this in a class. I would be really happy to do this before this PR. |
||
raise TypeError("'quantile' cannot be performed against " | ||
"'object' dtypes!") | ||
elif vals.dtype == np.int: | ||
nonlocal is_int | ||
is_int = True | ||
elif vals.dtype == 'datetime64[ns]': | ||
vals = vals.astype(np.float) | ||
nonlocal is_dt | ||
is_dt = True | ||
|
||
return vals | ||
|
||
def post_processor(vals): | ||
if is_dt: | ||
vals = vals.astype('datetime64[ns]') | ||
elif is_int and interpolation in ['lower', 'higher', 'nearest']: | ||
vals = vals.astype(np.int) | ||
|
||
return vals | ||
|
||
return self._get_cythonized_result('group_quantile', self.grouper, | ||
aggregate=True, | ||
needs_values=True, | ||
needs_mask=True, | ||
cython_dtype=np.float64, | ||
pre_processing=pre_processor, | ||
post_processing=post_processor, | ||
q=q, interpolation=interpolation) | ||
|
||
@Substitution(name='groupby') | ||
def ngroup(self, ascending=True): | ||
""" | ||
|
@@ -1928,43 +1992,46 @@ def cummax(self, axis=0, **kwargs): | |
def _get_cythonized_result(self, how, grouper, aggregate=False, | ||
cython_dtype=None, needs_values=False, | ||
needs_mask=False, needs_ngroups=False, | ||
result_is_index=False, | ||
pre_processing=None, post_processing=None, | ||
**kwargs): | ||
"""Get result for Cythonized functions | ||
result_is_index=False, pre_processing=None, | ||
post_processing=None, **kwargs): | ||
""" | ||
Get result for Cythonized functions. | ||
|
||
Parameters | ||
---------- | ||
how : str, Cythonized function name to be called | ||
grouper : Grouper object containing pertinent group info | ||
how : str | ||
Cythonized function name to be called. | ||
grouper : pandas.Grouper | ||
Grouper object containing pertinent group info. | ||
aggregate : bool, default False | ||
Whether the result should be aggregated to match the number of | ||
groups | ||
groups. | ||
cython_dtype : default None | ||
Type of the array that will be modified by the Cython call. If | ||
`None`, the type will be inferred from the values of each slice | ||
`None`, the type will be inferred from the values of each slice. | ||
needs_values : bool, default False | ||
Whether the values should be a part of the Cython call | ||
signature | ||
signature. | ||
needs_mask : bool, default False | ||
Whether boolean mask needs to be part of the Cython call | ||
signature | ||
signature. | ||
needs_ngroups : bool, default False | ||
Whether number of groups is part of the Cython call signature | ||
Whether number of groups is part of the Cython call signature. | ||
result_is_index : bool, default False | ||
Whether the result of the Cython operation is an index of | ||
values to be retrieved, instead of the actual values themselves | ||
values to be retrieved, instead of the actual values themselves. | ||
pre_processing : function, default None | ||
Function to be applied to `values` prior to passing to Cython | ||
Raises if `needs_values` is False | ||
Function to be applied to `values` prior to passing to Cython. | ||
Raises if `needs_values` is False. | ||
post_processing : function, default None | ||
Function to be applied to result of Cython function | ||
**kwargs : dict | ||
Extra arguments to be passed back to Cython funcs | ||
Function to be applied to result of Cython function. | ||
**kwargs | ||
Extra arguments to be passed back to Cython funcs. | ||
|
||
Returns | ||
------- | ||
`Series` or `DataFrame` with filled values | ||
`Series` or `DataFrame` | ||
Object type determined by caller of the ``GroupBy`` object. | ||
""" | ||
if result_is_index and aggregate: | ||
raise ValueError("'result_is_index' and 'aggregate' cannot both " | ||
|
Uh oh!
There was an error while loading. Please reload this page.