diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index e488ca52be8a0..b1e43485cc44e 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -233,6 +233,7 @@ Other enhancements - :func:`read_csv` supports memory-mapping for compressed files (:issue:`37621`) - Improve error reporting for :meth:`DataFrame.merge()` when invalid merge column definitions were given (:issue:`16228`) - Improve numerical stability for :meth:`Rolling.skew()`, :meth:`Rolling.kurt()`, :meth:`Expanding.skew()` and :meth:`Expanding.kurt()` through implementation of Kahan summation (:issue:`6929`) +- Improved error reporting for subsetting columns of a :class:`DataFrameGroupBy` with ``axis=1`` (:issue:`37725`) .. _whatsnew_120.api_breaking.python: diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index d0b58e8abc4ee..6f86819303537 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -1530,6 +1530,9 @@ def filter(self, func, dropna=True, *args, **kwargs): return self._apply_filter(indices, dropna) def __getitem__(self, key): + if self.axis == 1: + # GH 37725 + raise ValueError("Cannot subset columns when using axis=1") # per GH 23566 if isinstance(key, tuple) and len(key) > 1: # if len == 1, then it becomes a SeriesGroupBy and this is actually diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index a0c228200e73a..3a4abd58f0d39 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2081,7 +2081,6 @@ def test_group_on_two_row_multiindex_returns_one_tuple_key(): @pytest.mark.parametrize( "klass, attr, value", [ - (DataFrame, "axis", 1), (DataFrame, "level", "a"), (DataFrame, "as_index", False), (DataFrame, "sort", False), @@ -2120,6 +2119,14 @@ def test_subsetting_columns_keeps_attrs(klass, attr, value): assert getattr(result, attr) == getattr(expected, attr) +def test_subsetting_columns_axis_1(): + # GH 37725 + g = DataFrame({"A": [1], "B": [2], "C": [3]}).groupby([0, 0, 1], axis=1) + match = "Cannot subset columns when using axis=1" + with pytest.raises(ValueError, match=match): + g[["A", "B"]].sum() + + @pytest.mark.parametrize("func", ["sum", "any", "shift"]) def test_groupby_column_index_name_lost(func): # GH: 29764 groupby loses index sometimes