Skip to content

Commit

Permalink
Backport PR #54882 on branch 2.1.x (REGR: get_group raising with axis…
Browse files Browse the repository at this point in the history
…=1) (#54956)

REGR: get_group raising with axis=1 (#54882)

(cherry picked from commit ab34dd6)
  • Loading branch information
phofl committed Sep 2, 2023
1 parent 06a1581 commit ed1f044
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Fixed regressions
~~~~~~~~~~~~~~~~~
- Fixed regression in :func:`merge` when merging over a PyArrow string index (:issue:`54894`)
- Fixed regression in :func:`read_csv` when ``usecols`` is given and ``dtypes`` is a dict for ``engine="python"`` (:issue:`54868`)
- Fixed regression in :meth:`.GroupBy.get_group` raising for ``axis=1`` (:issue:`54858`)
- Fixed regression in :meth:`DataFrame.__setitem__` raising ``AssertionError`` when setting a :class:`Series` with a partial :class:`MultiIndex` (:issue:`54875`)
- Fixed regression in :meth:`Series.drop_duplicates` for PyArrow strings (:issue:`54904`)
- Fixed regression in :meth:`Series.value_counts` raising for numeric data if ``bins`` was specified (:issue:`54857`)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,8 @@ def get_group(self, name, obj=None) -> DataFrame | Series:
raise KeyError(name)

if obj is None:
return self._selected_obj.iloc[inds]
indexer = inds if self.axis == 0 else (slice(None), inds)
return self._selected_obj.iloc[indexer]
else:
warnings.warn(
"obj is deprecated and will be removed in a future version. "
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3159,3 +3159,26 @@ def test_groupby_series_with_datetimeindex_month_name():
expected = Series([2, 1], name="jan")
expected.index.name = "jan"
tm.assert_series_equal(result, expected)


def test_get_group_axis_1():
# GH#54858
df = DataFrame(
{
"col1": [0, 3, 2, 3],
"col2": [4, 1, 6, 7],
"col3": [3, 8, 2, 10],
"col4": [1, 13, 6, 15],
"col5": [-4, 5, 6, -7],
}
)
with tm.assert_produces_warning(FutureWarning, match="deprecated"):
grouped = df.groupby(axis=1, by=[1, 2, 3, 2, 1])
result = grouped.get_group(1)
expected = DataFrame(
{
"col1": [0, 3, 2, 3],
"col5": [-4, 5, 6, -7],
}
)
tm.assert_frame_equal(result, expected)

0 comments on commit ed1f044

Please sign in to comment.