Skip to content

Commit 8cd8ed3

Browse files
Bug in Series.groupby would raise ValueError when grouping by PeriodIndex level (#34049)
1 parent 21a10d1 commit 8cd8ed3

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,7 @@ Groupby/resample/rolling
816816
- Bug in :meth:`DataFrame.groupby` where a ``ValueError`` would be raised when grouping by a categorical column with read-only categories and ``sort=False`` (:issue:`33410`)
817817
- Bug in :meth:`GroupBy.first` and :meth:`GroupBy.last` where None is not preserved in object dtype (:issue:`32800`)
818818
- Bug in :meth:`Rolling.min` and :meth:`Rolling.max`: Growing memory usage after multiple calls when using a fixed window (:issue:`30726`)
819+
- Bug in :meth:`Series.groupby` would raise ``ValueError`` when grouping by :class:`PeriodIndex` level (:issue:`34010`)
819820
- Bug in :meth:`GroupBy.agg`, :meth:`GroupBy.transform`, and :meth:`GroupBy.resample` where subclasses are not preserved (:issue:`28330`)
820821
- Bug in :meth:`GroupBy.rolling.apply` ignores args and kwargs parameters (:issue:`33433`)
821822

pandas/core/groupby/grouper.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,9 @@ def is_in_obj(gpr) -> bool:
754754
return False
755755
try:
756756
return gpr is obj[gpr.name]
757-
except (KeyError, IndexError):
757+
except (KeyError, IndexError, ValueError):
758+
# TODO: ValueError: Given date string not likely a datetime.
759+
# should be KeyError?
758760
return False
759761

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

pandas/tests/groupby/test_size.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22
import pytest
33

4-
from pandas import DataFrame, Index, Series
4+
from pandas import DataFrame, Index, PeriodIndex, Series
55
import pandas._testing as tm
66

77

@@ -36,3 +36,11 @@ def test_size_groupby_all_null():
3636
result = df.groupby("A").size()
3737
expected = Series(dtype="int64", index=Index([], name="A"))
3838
tm.assert_series_equal(result, expected)
39+
40+
41+
def test_size_period_index():
42+
# https://github.com/pandas-dev/pandas/issues/34010
43+
ser = Series([1], index=PeriodIndex(["2000"], name="A", freq="D"))
44+
grp = ser.groupby(level="A")
45+
result = grp.size()
46+
tm.assert_series_equal(result, ser)

0 commit comments

Comments
 (0)