Skip to content

Commit b3ebcb0

Browse files
Backport PR #34049 on branch 1.0.x (Bug in Series.groupby would raise ValueError when grouping by PeriodIndex level) (#34247)
1 parent 76448ce commit b3ebcb0

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v1.0.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Fixed regressions
2525
- Fix to preserve the ability to index with the "nearest" method with xarray's CFTimeIndex, an :class:`Index` subclass (`pydata/xarray#3751 <https://github.com/pydata/xarray/issues/3751>`_, :issue:`32905`).
2626
- Fix regression in :meth:`DataFrame.describe` raising ``TypeError: unhashable type: 'dict'`` (:issue:`32409`)
2727
- Bug in :meth:`DataFrame.replace` casts columns to ``object`` dtype if items in ``to_replace`` not in values (:issue:`32988`)
28+
- Bug in :meth:`Series.groupby` would raise ``ValueError`` when grouping by :class:`PeriodIndex` level (:issue:`34010`)
2829
- Bug in :meth:`GroupBy.rolling.apply` ignores args and kwargs parameters (:issue:`33433`)
2930
-
3031

pandas/core/groupby/grouper.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,9 @@ def is_in_obj(gpr) -> bool:
577577
return False
578578
try:
579579
return gpr is obj[gpr.name]
580-
except (KeyError, IndexError):
580+
except (KeyError, IndexError, ValueError):
581+
# TODO: ValueError: Given date string not likely a datetime.
582+
# should be KeyError?
581583
return False
582584

583585
for i, (gpr, level) in enumerate(zip(keys, levels)):

pandas/tests/groupby/test_function.py

+8
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,14 @@ def test_size_groupby_all_null():
12881288
tm.assert_series_equal(result, expected)
12891289

12901290

1291+
def test_size_period_index():
1292+
# https://github.com/pandas-dev/pandas/issues/34010
1293+
ser = Series([1], index=pd.PeriodIndex(["2000"], name="A", freq="D"))
1294+
grp = ser.groupby(level="A")
1295+
result = grp.size()
1296+
tm.assert_series_equal(result, ser)
1297+
1298+
12911299
# quantile
12921300
# --------------------------------
12931301
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)