Skip to content

Commit 261ed9b

Browse files
Koustav-SamaddarPingviinituutti
authored andcommitted
ZeroDivisionError when groupby rank with method="dense" and pct=True (pandas-dev#23864)
1 parent e930029 commit 261ed9b

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,7 @@ Groupby/Resample/Rolling
15131513
- Bug in :meth:`DataFrame.expanding` in which the ``axis`` argument was not being respected during aggregations (:issue:`23372`)
15141514
- Bug in :meth:`pandas.core.groupby.DataFrameGroupBy.transform` which caused missing values when the input function can accept a :class:`DataFrame` but renames it (:issue:`23455`).
15151515
- Bug in :func:`pandas.core.groupby.GroupBy.nth` where column order was not always preserved (:issue:`20760`)
1516+
- Bug in :meth:`pandas.core.groupby.DataFrameGroupBy.rank` with ``method='dense'`` and ``pct=True`` when a group has only one member would raise a ``ZeroDivisionError`` (:issue:`23666`).
15161517

15171518
Reshaping
15181519
^^^^^^^^^

pandas/_libs/groupby_helper.pxi.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ def group_rank_{{name}}(ndarray[float64_t, ndim=2] out,
587587
# rankings, so we assign them percentages of NaN.
588588
if out[i, 0] != out[i, 0] or out[i, 0] == NAN:
589589
out[i, 0] = NAN
590-
else:
590+
elif grp_sizes[i, 0] != 0:
591591
out[i, 0] = out[i, 0] / grp_sizes[i, 0]
592592
{{endif}}
593593
{{endfor}}

pandas/tests/groupby/test_rank.py

+15
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,18 @@ def test_rank_empty_group():
288288
result = df.groupby(column).rank(pct=True)
289289
expected = DataFrame({"B": [0.5, np.nan, 1.0]})
290290
tm.assert_frame_equal(result, expected)
291+
292+
293+
@pytest.mark.parametrize("input_key,input_value,output_value", [
294+
([1, 2], [1, 1], [1.0, 1.0]),
295+
([1, 1, 2, 2], [1, 2, 1, 2], [0.5, 1.0, 0.5, 1.0]),
296+
([1, 1, 2, 2], [1, 2, 1, np.nan], [0.5, 1.0, 1.0, np.nan]),
297+
([1, 1, 2], [1, 2, np.nan], [0.5, 1.0, np.nan])
298+
])
299+
def test_rank_zero_div(input_key, input_value, output_value):
300+
# GH 23666
301+
df = DataFrame({"A": input_key, "B": input_value})
302+
303+
result = df.groupby("A").rank(method="dense", pct=True)
304+
expected = DataFrame({"B": output_value})
305+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)