Skip to content

Commit b8f8955

Browse files
authored
BUG: groupby(axis=0).rank(axis=1) (#41324)
1 parent 6dd6f1a commit b8f8955

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,7 @@ Groupby/resample/rolling
886886
- Bug in :meth:`DataFrame.rolling` returning sum not zero for all ``NaN`` window with ``min_periods=0`` if calculation is not numerical stable (:issue:`41053`)
887887
- Bug in :meth:`SeriesGroupBy.agg` failing to retain ordered :class:`CategoricalDtype` on order-preserving aggregations (:issue:`41147`)
888888
- Bug in :meth:`DataFrameGroupBy.min` and :meth:`DataFrameGroupBy.max` with multiple object-dtype columns and ``numeric_only=False`` incorrectly raising ``ValueError`` (:issue:41111`)
889+
- Bug in :meth:`DataFrameGroupBy.rank` with the GroupBy object's ``axis=0`` and the ``rank`` method's keyword ``axis=1`` (:issue:`41320`)
889890

890891
Reshaping
891892
^^^^^^^^^

pandas/core/groupby/groupby.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -2648,14 +2648,23 @@ def rank(
26482648
if na_option not in {"keep", "top", "bottom"}:
26492649
msg = "na_option must be one of 'keep', 'top', or 'bottom'"
26502650
raise ValueError(msg)
2651+
2652+
kwargs = {
2653+
"ties_method": method,
2654+
"ascending": ascending,
2655+
"na_option": na_option,
2656+
"pct": pct,
2657+
}
2658+
if axis != 0:
2659+
# DataFrame uses different keyword name
2660+
kwargs["method"] = kwargs.pop("ties_method")
2661+
return self.apply(lambda x: x.rank(axis=axis, numeric_only=False, **kwargs))
2662+
26512663
return self._cython_transform(
26522664
"rank",
26532665
numeric_only=False,
2654-
ties_method=method,
2655-
ascending=ascending,
2656-
na_option=na_option,
2657-
pct=pct,
26582666
axis=axis,
2667+
**kwargs,
26592668
)
26602669

26612670
@final

pandas/core/groupby/ops.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
ensure_float64,
4545
ensure_int64,
4646
ensure_platform_int,
47+
is_1d_only_ea_obj,
4748
is_bool_dtype,
4849
is_categorical_dtype,
4950
is_complex_dtype,
@@ -599,9 +600,11 @@ def cython_operation(
599600
if values.ndim > 2:
600601
raise NotImplementedError("number of dimensions is currently limited to 2")
601602
elif values.ndim == 2:
603+
assert axis == 1, axis
604+
elif not is_1d_only_ea_obj(values):
602605
# Note: it is *not* the case that axis is always 0 for 1-dim values,
603606
# as we can have 1D ExtensionArrays that we need to treat as 2D
604-
assert axis == 1, axis
607+
assert axis == 0
605608

606609
dtype = values.dtype
607610
is_numeric = is_numeric_dtype(dtype)

pandas/tests/groupby/test_rank.py

+15
Original file line numberDiff line numberDiff line change
@@ -600,3 +600,18 @@ def test_rank_multiindex():
600600
)
601601

602602
tm.assert_frame_equal(result, expected)
603+
604+
605+
def test_groupby_axis0_rank_axis1():
606+
# GH#41320
607+
df = DataFrame(
608+
{0: [1, 3, 5, 7], 1: [2, 4, 6, 8], 2: [1.5, 3.5, 5.5, 7.5]},
609+
index=["a", "a", "b", "b"],
610+
)
611+
gb = df.groupby(level=0, axis=0)
612+
613+
res = gb.rank(axis=1)
614+
615+
# This should match what we get when "manually" operating group-by-group
616+
expected = concat([df.loc["a"].rank(axis=1), df.loc["b"].rank(axis=1)], axis=0)
617+
tm.assert_frame_equal(res, expected)

0 commit comments

Comments
 (0)