diff --git a/doc/source/whatsnew/v0.22.0.txt b/doc/source/whatsnew/v0.22.0.txt index ae6d0816abc41..8c6226ec15739 100644 --- a/doc/source/whatsnew/v0.22.0.txt +++ b/doc/source/whatsnew/v0.22.0.txt @@ -310,6 +310,7 @@ Groupby/Resample/Rolling ^^^^^^^^^^^^^^^^^^^^^^^^ - Bug when grouping by a single column and aggregating with a class like ``list`` or ``tuple`` (:issue:`18079`) +- Fixed regression in :func:`DataFrame.groupby` which would not emit an error when called with a tuple key not in the index (:issue:`18798`) - - diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py index b4223ac0a177a..ced120fbdbe29 100644 --- a/pandas/core/groupby.py +++ b/pandas/core/groupby.py @@ -2977,7 +2977,7 @@ def is_in_obj(gpr): def _is_label_like(val): - return (isinstance(val, compat.string_types) or + return (isinstance(val, (compat.string_types, tuple)) or (val is not None and is_scalar(val))) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 3327612b016f4..cf4a6ec1c932a 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2750,7 +2750,6 @@ def test_tuple_warns_unhashable(self): assert "Interpreting tuple 'by' as a list" in str(w[0].message) - @pytest.mark.xfail(reason="GH-18798") def test_tuple_correct_keyerror(self): # https://github.com/pandas-dev/pandas/issues/18798 df = pd.DataFrame(1, index=range(3),