Skip to content

Commit ed1f044

Browse files
authored
Backport PR pandas-dev#54882 on branch 2.1.x (REGR: get_group raising with axis=1) (pandas-dev#54956)
REGR: get_group raising with axis=1 (pandas-dev#54882) (cherry picked from commit ab34dd6)
1 parent 06a1581 commit ed1f044

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

doc/source/whatsnew/v2.1.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Fixed regressions
1515
~~~~~~~~~~~~~~~~~
1616
- Fixed regression in :func:`merge` when merging over a PyArrow string index (:issue:`54894`)
1717
- Fixed regression in :func:`read_csv` when ``usecols`` is given and ``dtypes`` is a dict for ``engine="python"`` (:issue:`54868`)
18+
- Fixed regression in :meth:`.GroupBy.get_group` raising for ``axis=1`` (:issue:`54858`)
1819
- Fixed regression in :meth:`DataFrame.__setitem__` raising ``AssertionError`` when setting a :class:`Series` with a partial :class:`MultiIndex` (:issue:`54875`)
1920
- Fixed regression in :meth:`Series.drop_duplicates` for PyArrow strings (:issue:`54904`)
2021
- Fixed regression in :meth:`Series.value_counts` raising for numeric data if ``bins`` was specified (:issue:`54857`)

pandas/core/groupby/groupby.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,8 @@ def get_group(self, name, obj=None) -> DataFrame | Series:
10591059
raise KeyError(name)
10601060

10611061
if obj is None:
1062-
return self._selected_obj.iloc[inds]
1062+
indexer = inds if self.axis == 0 else (slice(None), inds)
1063+
return self._selected_obj.iloc[indexer]
10631064
else:
10641065
warnings.warn(
10651066
"obj is deprecated and will be removed in a future version. "

pandas/tests/groupby/test_groupby.py

+23
Original file line numberDiff line numberDiff line change
@@ -3159,3 +3159,26 @@ def test_groupby_series_with_datetimeindex_month_name():
31593159
expected = Series([2, 1], name="jan")
31603160
expected.index.name = "jan"
31613161
tm.assert_series_equal(result, expected)
3162+
3163+
3164+
def test_get_group_axis_1():
3165+
# GH#54858
3166+
df = DataFrame(
3167+
{
3168+
"col1": [0, 3, 2, 3],
3169+
"col2": [4, 1, 6, 7],
3170+
"col3": [3, 8, 2, 10],
3171+
"col4": [1, 13, 6, 15],
3172+
"col5": [-4, 5, 6, -7],
3173+
}
3174+
)
3175+
with tm.assert_produces_warning(FutureWarning, match="deprecated"):
3176+
grouped = df.groupby(axis=1, by=[1, 2, 3, 2, 1])
3177+
result = grouped.get_group(1)
3178+
expected = DataFrame(
3179+
{
3180+
"col1": [0, 3, 2, 3],
3181+
"col5": [-4, 5, 6, -7],
3182+
}
3183+
)
3184+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)