Skip to content

Commit 8764f55

Browse files
committed
Added func for obj support
1 parent 64960bf commit 8764f55

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

pandas/_libs/groupby.pyx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,59 @@ def group_last_object(ndarray[object, ndim=2] out,
119119
else:
120120
out[i, j] = resx[i, j]
121121

122+
@cython.boundscheck(False)
123+
@cython.wraparound(False)
124+
def group_rank_object(ndarray[float64_t, ndim=2] out,
125+
ndarray[object, ndim=2] values,
126+
ndarray[int64_t] labels,
127+
bint is_datetimelike, **kwargs):
128+
"""
129+
Only transforms on axis=0
130+
"""
131+
cdef:
132+
int tiebreak
133+
Py_ssize_t i, j, N, K
134+
int64_t val_start=0, grp_start=0, dups=0, sum_ranks=0
135+
ndarray[int64_t] _as
136+
137+
tiebreak = tiebreakers[kwargs['ties_method']]
138+
N, K = (<object> values).shape
139+
140+
_as = np.lexsort((values[:, 0], labels))
141+
142+
for i in range(N):
143+
dups += 1
144+
sum_ranks += i - grp_start + 1
145+
146+
if tiebreak == TIEBREAK_AVERAGE:
147+
for j in range(i - dups + 1, i + 1):
148+
out[_as[j], 0] = sum_ranks / dups
149+
elif tiebreak == TIEBREAK_MIN:
150+
for j in range(i - dups + 1, i + 1):
151+
out[_as[j], 0] = i - grp_start - dups + 2
152+
elif tiebreak == TIEBREAK_MAX:
153+
for j in range(i - dups + 1, i + 1):
154+
out[_as[j], 0] = i - grp_start + 1
155+
elif tiebreak == TIEBREAK_FIRST:
156+
for j in range(i - dups + 1, i + 1):
157+
out[_as[j], 0] = j + 1
158+
elif tiebreak == TIEBREAK_FIRST_DESCENDING:
159+
for j in range(i - dups + 1, i + 1):
160+
out[_as[j], 0] = 2 * (i - grp_start) - j - dups + 2
161+
elif tiebreak == TIEBREAK_DENSE:
162+
for j in range(i - dups + 1, i + 1):
163+
out[_as[j], 0] = val_start - grp_start
164+
165+
if (i == N - 1 or (
166+
(values[_as[i], 0] != values[_as[i+1], 0]) and not
167+
(values[_as[i], 0] is np.nan and values[_as[i+1], 0] is np.nan)
168+
)):
169+
dups = sum_ranks = 0
170+
val_start = i
171+
172+
if i == 0 or labels[_as[i]] != labels[_as[i-1]]:
173+
grp_start = i
174+
122175

123176
cdef inline float64_t median_linear(float64_t* a, int n) nogil:
124177
cdef int i, j, na_count = 0

pandas/core/groupby.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1776,7 +1776,7 @@ def cumcount(self, ascending=True):
17761776
def rank(self, method='average', ascending=True, na_option='keep',
17771777
pct=False, axis=0):
17781778
"""Rank within each group"""
1779-
return self._cython_transform('rank', ties_method=method,
1779+
return self._cython_transform('rank', numeric_only=False, ties_method=method,
17801780
ascending=ascending, na_option=na_option,
17811781
pct=pct, axis=axis)
17821782

0 commit comments

Comments
 (0)