Skip to content

Commit f7613a5

Browse files
committed
Added pct support
1 parent 8764f55 commit f7613a5

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

pandas/_libs/groupby.pyx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,10 @@ def group_rank_object(ndarray[float64_t, ndim=2] out,
133133
Py_ssize_t i, j, N, K
134134
int64_t val_start=0, grp_start=0, dups=0, sum_ranks=0
135135
ndarray[int64_t] _as
136+
bint pct
136137

137138
tiebreak = tiebreakers[kwargs['ties_method']]
139+
pct = kwargs['pct']
138140
N, K = (<object> values).shape
139141

140142
_as = np.lexsort((values[:, 0], labels))
@@ -169,8 +171,11 @@ def group_rank_object(ndarray[float64_t, ndim=2] out,
169171
dups = sum_ranks = 0
170172
val_start = i
171173

172-
if i == 0 or labels[_as[i]] != labels[_as[i-1]]:
173-
grp_start = i
174+
if i == N - 1 or labels[_as[i]] != labels[_as[i+1]]:
175+
if pct:
176+
for j in range(grp_start, i + 1):
177+
out[_as[j], 0] = out[_as[j], 0] / (i - grp_start + 1)
178+
grp_start = i + 1
174179

175180

176181
cdef inline float64_t median_linear(float64_t* a, int n) nogil:

pandas/_libs/groupby_helper.pxi.in

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ def group_nth_{{name}}(ndarray[{{dest_type2}}, ndim=2] out,
444444
else:
445445
out[i, j] = resx[i, j]
446446

447+
447448
@cython.boundscheck(False)
448449
@cython.wraparound(False)
449450
def group_rank_{{name}}(ndarray[float64_t, ndim=2] out,
@@ -458,8 +459,10 @@ def group_rank_{{name}}(ndarray[float64_t, ndim=2] out,
458459
Py_ssize_t i, j, N, K
459460
int64_t val_start=0, grp_start=0, dups=0, sum_ranks=0
460461
ndarray[int64_t] _as
462+
bint pct
461463

462464
tiebreak = tiebreakers[kwargs['ties_method']]
465+
pct = kwargs['pct']
463466
N, K = (<object> values).shape
464467

465468
_as = np.lexsort((values[:, 0], labels))
@@ -495,8 +498,11 @@ def group_rank_{{name}}(ndarray[float64_t, ndim=2] out,
495498
dups = sum_ranks = 0
496499
val_start = i
497500

498-
if i == 0 or labels[_as[i]] != labels[_as[i-1]]:
499-
grp_start = i
501+
if i == N - 1 or labels[_as[i]] != labels[_as[i+1]]:
502+
if pct:
503+
for j in range(grp_start, i + 1):
504+
out[_as[j], 0] = out[_as[j], 0] / (i - grp_start + 1)
505+
grp_start = i + 1
500506

501507
{{endfor}}
502508

0 commit comments

Comments
 (0)