Skip to content

Commit 2c1ec41

Browse files
authored
BUG: Fix Series.groupby raising OutOfBoundsDatetime with DatetimeIndex and month name (#54306)
Fix Series.groupby raising OutOfBoundsDatetime with DatetimeIndex and month name
1 parent ae6a335 commit 2c1ec41

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

doc/source/whatsnew/v2.1.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ Groupby/resample/rolling
723723
- Bug in :meth:`GroupBy.var` failing to raise ``TypeError`` when called with datetime64, timedelta64 or :class:`PeriodDtype` values (:issue:`52128`, :issue:`53045`)
724724
- Bug in :meth:`DataFrameGroupby.resample` with ``kind="period"`` raising ``AttributeError`` (:issue:`24103`)
725725
- Bug in :meth:`Resampler.ohlc` with empty object returning a :class:`Series` instead of empty :class:`DataFrame` (:issue:`42902`)
726+
- Bug in :meth:`Series.groupby` raising an error when grouped :class:`Series` has a :class:`DatetimeIndex` index and a :class:`Series` with a name that is a month is given to the ``by`` argument (:issue:`48509`)
726727
- Bug in :meth:`SeriesGroupBy.count` and :meth:`DataFrameGroupBy.count` where the dtype would be ``np.int64`` for data with :class:`ArrowDtype` or masked dtypes (e.g. ``Int64``) (:issue:`53831`)
727728
- Bug in :meth:`SeriesGroupBy.nth` and :meth:`DataFrameGroupBy.nth` after performing column selection when using ``dropna="any"`` or ``dropna="all"`` would not subset columns (:issue:`53518`)
728729
- Bug in :meth:`SeriesGroupBy.nth` and :meth:`DataFrameGroupBy.nth` raised after performing column selection when using ``dropna="any"`` or ``dropna="all"`` resulted in rows being dropped (:issue:`53518`)

pandas/core/groupby/grouper.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pandas._config import using_copy_on_write
1616

1717
from pandas._libs import lib
18+
from pandas._libs.tslibs import OutOfBoundsDatetime
1819
from pandas.errors import InvalidIndexError
1920
from pandas.util._decorators import cache_readonly
2021
from pandas.util._exceptions import find_stack_level
@@ -969,7 +970,7 @@ def is_in_obj(gpr) -> bool:
969970
# series is part of the object
970971
try:
971972
obj_gpr_column = obj[gpr.name]
972-
except (KeyError, IndexError, InvalidIndexError):
973+
except (KeyError, IndexError, InvalidIndexError, OutOfBoundsDatetime):
973974
return False
974975
if isinstance(gpr, Series) and isinstance(obj_gpr_column, Series):
975976
return gpr._mgr.references_same_values( # type: ignore[union-attr]
@@ -978,11 +979,13 @@ def is_in_obj(gpr) -> bool:
978979
return False
979980
try:
980981
return gpr is obj[gpr.name]
981-
except (KeyError, IndexError, InvalidIndexError):
982+
except (KeyError, IndexError, InvalidIndexError, OutOfBoundsDatetime):
982983
# IndexError reached in e.g. test_skip_group_keys when we pass
983984
# lambda here
984985
# InvalidIndexError raised on key-types inappropriate for index,
985986
# e.g. DatetimeIndex.get_loc(tuple())
987+
# OutOfBoundsDatetime raised when obj is a Series with DatetimeIndex
988+
# and gpr.name is month str
986989
return False
987990

988991
for gpr, level in zip(keys, levels):

pandas/tests/groupby/test_groupby.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3124,3 +3124,12 @@ def test_groupby_with_Time_Grouper():
31243124
df = test_data.groupby(Grouper(key="time2", freq="1T")).count().reset_index()
31253125

31263126
tm.assert_frame_equal(df, expected_output)
3127+
3128+
3129+
def test_groupby_series_with_datetimeindex_month_name():
3130+
# GH 48509
3131+
s = Series([0, 1, 0], index=date_range("2022-01-01", periods=3), name="jan")
3132+
result = s.groupby(s).count()
3133+
expected = Series([2, 1], name="jan")
3134+
expected.index.name = "jan"
3135+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)