Skip to content

Commit 0312a18

Browse files
committed
Used kwargs to call Cython groupby funcs
1 parent f21dbcd commit 0312a18

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

pandas/_libs/groupby_helper.pxi.in

+8-6
Original file line numberDiff line numberDiff line change
@@ -907,14 +907,16 @@ def group_cumsum(numeric[:, :] out,
907907
@cython.boundscheck(False)
908908
@cython.wraparound(False)
909909
def group_shift_indexer(ndarray[int64_t] out, ndarray[int64_t] labels,
910-
int ngroups, int periods):
910+
int64_t ngroups, **kwargs):
911911
cdef:
912912
Py_ssize_t N, i, j, ii
913-
int offset, sign
913+
int offset, sign, periods
914914
int64_t lab, idxer, idxer_slot
915915
int64_t[:] label_seen = np.zeros(ngroups, dtype=np.int64)
916916
int64_t[:, :] label_indexer
917917

918+
periods = kwargs['periods']
919+
918920
N, = (<object> labels).shape
919921

920922
if periods < 0:
@@ -958,8 +960,7 @@ def group_shift_indexer(ndarray[int64_t] out, ndarray[int64_t] labels,
958960
@cython.wraparound(False)
959961
@cython.boundscheck(False)
960962
def group_fillna_indexer(ndarray[int64_t] out, ndarray[int64_t] labels,
961-
ndarray[uint8_t] mask, object direction,
962-
int64_t limit):
963+
ndarray[uint8_t] mask, **kwargs):
963964
"""Indexes how to fill values forwards or backwards within a group
964965

965966
Parameters
@@ -980,9 +981,10 @@ def group_fillna_indexer(ndarray[int64_t] out, ndarray[int64_t] labels,
980981
cdef:
981982
Py_ssize_t i, N
982983
ndarray[int64_t] sorted_labels
983-
int64_t curr_fill_idx=-1
984-
int64_t idx, filled_vals=0
984+
int64_t limit, idx, curr_fill_idx=-1, filled_vals=0
985985

986+
direction = kwargs['direction']
987+
limit = kwargs['limit']
986988
N = len(out)
987989

988990
# Make sure all arrays are the same size

pandas/core/groupby.py

+3-10
Original file line numberDiff line numberDiff line change
@@ -1865,17 +1865,12 @@ def _get_cythonized_result(self, how, grouper, needs_mask=False,
18651865
needs_ngroups : bool, default False
18661866
Whether number of groups part of the Cython call signature
18671867
**kwargs : dict
1868-
Extra arguments required for the given function. This method
1869-
internally stores an OrderedDict that maps those keywords to
1870-
positional arguments before calling the Cython layer
1868+
Extra arguments to be passed back to Cython funcs
18711869
18721870
Returns
18731871
-------
18741872
GroupBy object populated with appropriate result(s)
18751873
"""
1876-
exp_kwds = collections.OrderedDict([
1877-
(('group_fillna_indexer'), ('direction', 'limit')),
1878-
(('group_shift_indexer'), ('nperiods',))])
18791874

18801875
labels, _, ngroups = grouper.group_info
18811876
output = collections.OrderedDict()
@@ -1891,9 +1886,7 @@ def _get_cythonized_result(self, how, grouper, needs_mask=False,
18911886
if needs_ngroups:
18921887
func = partial(func, ngroups)
18931888

1894-
# Convert any keywords into positional arguments
1895-
func = partial(func, *(kwargs[x] for x in exp_kwds[how]))
1896-
func() # Call func to modify indexer values in place
1889+
func(**kwargs) # Call func to modify indexer values in place
18971890
output[name] = algorithms.take_nd(obj.values, indexer)
18981891

18991892
return self._wrap_transformed_output(output)
@@ -1917,7 +1910,7 @@ def shift(self, periods=1, freq=None, axis=0):
19171910

19181911
return self._get_cythonized_result('group_shift_indexer',
19191912
self.grouper, needs_ngroups=True,
1920-
nperiods=periods)
1913+
periods=periods)
19211914

19221915
@Substitution(name='groupby')
19231916
@Appender(_doc_template)

0 commit comments

Comments
 (0)