Skip to content

Commit 1f47301

Browse files
Backport PR #31690: TST: add test for regression in groupby with empty MultiIndex level (#31692)
Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent 3e8a4c2 commit 1f47301

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

doc/source/whatsnew/v1.0.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Fixed regressions
2222
- Fixed regression in ``.groupby()`` aggregations with categorical dtype using Cythonized reduction functions (e.g. ``first``) (:issue:`31450`)
2323
- 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`)
2424
- Fixed regression in :meth:`DataFrame.groupby` whereby taking the minimum or maximum of a column with period dtype would raise a ``TypeError``. (:issue:`31471`)
25+
- Fixed regression in :meth:`DataFrame.groupby` with an empty DataFrame grouping by a level of a MultiIndex (:issue:`31670`).
2526
- Fixed regression in :meth:`DataFrame.apply` with object dtype and non-reducing function (:issue:`31505`)
2627
- Fixed regression in :meth:`to_datetime` when parsing non-nanosecond resolution datetimes (:issue:`31491`)
2728
- Fixed regression in :meth:`~DataFrame.to_csv` where specifying an ``na_rep`` might truncate the values written (:issue:`31447`)

pandas/tests/groupby/test_grouping.py

+13
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,19 @@ def test_groupby_level_index_value_all_na(self):
676676
)
677677
tm.assert_frame_equal(result, expected)
678678

679+
def test_groupby_multiindex_level_empty(self):
680+
# https://github.com/pandas-dev/pandas/issues/31670
681+
df = pd.DataFrame(
682+
[[123, "a", 1.0], [123, "b", 2.0]], columns=["id", "category", "value"]
683+
)
684+
df = df.set_index(["id", "category"])
685+
empty = df[df.value < 0]
686+
result = empty.groupby("id").sum()
687+
expected = pd.DataFrame(
688+
dtype="float64", columns=["value"], index=pd.Int64Index([], name="id")
689+
)
690+
tm.assert_frame_equal(result, expected)
691+
679692

680693
# get_group
681694
# --------------------------------

0 commit comments

Comments
 (0)