From 9ac002a342c43f76b45cbbf3acc4d82a624a37b9 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Wed, 5 Feb 2020 11:17:05 +0100 Subject: [PATCH] Backport PR #31690: TST: add test for regression in groupby with empty MultiIndex level --- doc/source/whatsnew/v1.0.1.rst | 1 + pandas/tests/groupby/test_grouping.py | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/doc/source/whatsnew/v1.0.1.rst b/doc/source/whatsnew/v1.0.1.rst index c82a58e5d3c45..2ca836d9c5508 100644 --- a/doc/source/whatsnew/v1.0.1.rst +++ b/doc/source/whatsnew/v1.0.1.rst @@ -22,6 +22,7 @@ Fixed regressions - Fixed regression in ``.groupby()`` aggregations with categorical dtype using Cythonized reduction functions (e.g. ``first``) (:issue:`31450`) - Fixed regression in :meth:`GroupBy.apply` if called with a function which returned a non-pandas non-scalar object (e.g. a list or numpy array) (:issue:`31441`) - Fixed regression in :meth:`DataFrame.groupby` whereby taking the minimum or maximum of a column with period dtype would raise a ``TypeError``. (:issue:`31471`) +- Fixed regression in :meth:`DataFrame.groupby` with an empty DataFrame grouping by a level of a MultiIndex (:issue:`31670`). - Fixed regression in :meth:`DataFrame.apply` with object dtype and non-reducing function (:issue:`31505`) - Fixed regression in :meth:`to_datetime` when parsing non-nanosecond resolution datetimes (:issue:`31491`) - Fixed regression in :meth:`~DataFrame.to_csv` where specifying an ``na_rep`` might truncate the values written (:issue:`31447`) diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index 70ba21d89d22f..7245d6f481d99 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -676,6 +676,19 @@ def test_groupby_level_index_value_all_na(self): ) tm.assert_frame_equal(result, expected) + def test_groupby_multiindex_level_empty(self): + # https://github.com/pandas-dev/pandas/issues/31670 + df = pd.DataFrame( + [[123, "a", 1.0], [123, "b", 2.0]], columns=["id", "category", "value"] + ) + df = df.set_index(["id", "category"]) + empty = df[df.value < 0] + result = empty.groupby("id").sum() + expected = pd.DataFrame( + dtype="float64", columns=["value"], index=pd.Int64Index([], name="id") + ) + tm.assert_frame_equal(result, expected) + # get_group # --------------------------------