From b356c6a36e8dbee8858d12b00df9ef0ea99fd7b6 Mon Sep 17 00:00:00 2001 From: iofall <50991099+iofall@users.noreply.github.com> Date: Sun, 15 Jan 2023 23:34:37 +0530 Subject: [PATCH 1/2] Fix .transform(ngroup) for axis=1 grouper --- pandas/core/groupby/groupby.py | 2 +- pandas/tests/groupby/transform/test_transform.py | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 431b23023b094..736c8aafc82e2 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3369,7 +3369,7 @@ def ngroup(self, ascending: bool = True): dtype: int64 """ with self._group_selection_context(): - index = self._selected_obj.index + index = self._selected_obj._get_axis(self.axis) comp_ids = self.grouper.group_info[0] dtype: type diff --git a/pandas/tests/groupby/transform/test_transform.py b/pandas/tests/groupby/transform/test_transform.py index 528f417ea1039..5dab30c6fd7cc 100644 --- a/pandas/tests/groupby/transform/test_transform.py +++ b/pandas/tests/groupby/transform/test_transform.py @@ -164,10 +164,6 @@ def test_transform_broadcast(tsframe, ts): def test_transform_axis_1(request, transformation_func): # GH 36308 - if transformation_func == "ngroup": - msg = "ngroup fails with axis=1: #45986" - request.node.add_marker(pytest.mark.xfail(reason=msg)) - df = DataFrame({"a": [1, 2], "b": [3, 4], "c": [5, 6]}, index=["x", "y"]) args = get_groupby_method_args(transformation_func, df) result = df.groupby([0, 0, 1], axis=1).transform(transformation_func, *args) From d7c8a58923a0c7f408e21619e6e06cd655e3f126 Mon Sep 17 00:00:00 2001 From: iofall <50991099+iofall@users.noreply.github.com> Date: Mon, 23 Jan 2023 23:59:10 +0530 Subject: [PATCH 2/2] Add whatsnew for transform axis-1 ngroup bug fix --- doc/source/whatsnew/v2.0.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index bbecf3fee01f3..ac41fc8d21aa2 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -1098,6 +1098,7 @@ Groupby/resample/rolling - Bug in :meth:`.DataFrameGroupBy.transform` and :meth:`.SeriesGroupBy.transform` would raise incorrectly when grouper had ``axis=1`` for ``"idxmin"`` and ``"idxmax"`` arguments (:issue:`45986`) - Bug in :class:`.DataFrameGroupBy` would raise when used with an empty DataFrame, categorical grouper, and ``dropna=False`` (:issue:`50634`) - Bug in :meth:`.SeriesGroupBy.value_counts` did not respect ``sort=False`` (:issue:`50482`) +- Bug in :meth:`.DataFrameGroupBy.transform` and :meth:`.SeriesGroupBy.transform` would raise incorrectly when grouper had ``axis=1`` for ``"ngroup"`` argument (:issue:`45986`) - Reshaping