From dea162b463a3dea0f2d2ade3bceebbcdd65ec8b5 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Mon, 9 Nov 2020 17:23:43 -0500 Subject: [PATCH 1/2] ENH: Raise when subsetting columns on groupby with axis=1 --- pandas/core/groupby/generic.py | 3 +++ pandas/tests/groupby/test_groupby.py | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) 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 From 0d2630185b937cce362e8c9e34a1fc4bb3d03ad5 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Mon, 9 Nov 2020 17:28:45 -0500 Subject: [PATCH 2/2] whatsnew --- doc/source/whatsnew/v1.2.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index d4d28dde52d58..a5d60ca90d92f 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -232,6 +232,7 @@ Other enhancements - :meth:`testing.assert_index_equal` now has a ``check_order`` parameter that allows indexes to be checked in an order-insensitive manner (:issue:`37478`) - :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`) +- Improved error reporting for subsetting columns of a :class:`DataFrameGroupBy` with ``axis=1`` (:issue:`37725`) .. _whatsnew_120.api_breaking.python: