From 857e57690f7fe6b891b8e70ab330b03fd5e8aa05 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Thu, 7 May 2020 15:26:58 +0100 Subject: [PATCH 1/6] Revert part "CLN: Catch more specific exceptions in groupby #27909" --- pandas/core/groupby/grouper.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index f84ca6c05f40f..6c7668d85a1c1 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -607,11 +607,9 @@ def is_in_axis(key) -> bool: # if the grouper is obj[name] def is_in_obj(gpr) -> bool: - if not hasattr(gpr, "name"): - return False try: - return gpr is obj[gpr.name] - except (KeyError, IndexError): + return id(gpr) == id(obj[gpr.name]) + except Exception: return False for i, (gpr, level) in enumerate(zip(keys, levels)): From 086cb4744d5dd2221f2e077a3bb70c34a368a4ce Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Thu, 7 May 2020 15:41:05 +0100 Subject: [PATCH 2/6] add test for 34010 --- pandas/tests/groupby/test_function.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pandas/tests/groupby/test_function.py b/pandas/tests/groupby/test_function.py index 93dd1bf23c308..195bea7b74555 100644 --- a/pandas/tests/groupby/test_function.py +++ b/pandas/tests/groupby/test_function.py @@ -1307,6 +1307,14 @@ def test_size_groupby_all_null(): tm.assert_series_equal(result, expected) +def test_size_period_index(): + # https://github.com/pandas-dev/pandas/issues/34010 + ser = Series([1], index=pd.PeriodIndex(["2000"], name="A", freq="D")) + grp = ser.groupby(level="A") + result = grp.size() + tm.assert_series_equal(result, ser) + + # quantile # -------------------------------- From f3c16fcad45007638aa91be8ceea111db3e2e025 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Thu, 7 May 2020 16:02:55 +0100 Subject: [PATCH 3/6] add ValueError to caught exceptions --- pandas/core/groupby/grouper.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index 6c7668d85a1c1..95f11c67a65c7 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -607,9 +607,13 @@ def is_in_axis(key) -> bool: # if the grouper is obj[name] def is_in_obj(gpr) -> bool: + if not hasattr(gpr, "name"): + return False try: - return id(gpr) == id(obj[gpr.name]) - except Exception: + return gpr is obj[gpr.name] + except (KeyError, IndexError, ValueError): + # TODO: ValueError: Given date string not likely a datetime. + # should be KeyError? return False for i, (gpr, level) in enumerate(zip(keys, levels)): From 0f5fb5872ed4e5a671cc0e6bfd932006f4b825de Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Thu, 7 May 2020 16:07:09 +0100 Subject: [PATCH 4/6] whatsnew --- doc/source/whatsnew/v1.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 9c424f70b1ee0..c7f18aaf0eb23 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -721,6 +721,7 @@ Groupby/resample/rolling - 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`) - Bug in :meth:`GroupBy.first` and :meth:`GroupBy.last` where None is not preserved in object dtype (:issue:`32800`) - Bug in :meth:`Rolling.min` and :meth:`Rolling.max`: Growing memory usage after multiple calls when using a fixed window (:issue:`30726`) +- Bug in :meth:`Series.groupby` would raise ValueError when grouping by PeriodIndex level (:issue:`34010`) Reshaping ^^^^^^^^^ From af642d3393f5738f43dd455b06b8fcea1d226a2e Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sat, 16 May 2020 09:32:56 +0100 Subject: [PATCH 5/6] add backticks to release note --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index ffa612d24316e..ff6c9ab48ec6d 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -815,7 +815,7 @@ Groupby/resample/rolling - 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`) - Bug in :meth:`GroupBy.first` and :meth:`GroupBy.last` where None is not preserved in object dtype (:issue:`32800`) - Bug in :meth:`Rolling.min` and :meth:`Rolling.max`: Growing memory usage after multiple calls when using a fixed window (:issue:`30726`) -- Bug in :meth:`Series.groupby` would raise ValueError when grouping by PeriodIndex level (:issue:`34010`) +- Bug in :meth:`Series.groupby` would raise ``ValueError`` when grouping by ``PeriodIndex`` level (:issue:`34010`) - Bug in :meth:`GroupBy.agg`, :meth:`GroupBy.transform`, and :meth:`GroupBy.resample` where subclasses are not preserved (:issue:`28330`) - Bug in :meth:`GroupBy.rolling.apply` ignores args and kwargs parameters (:issue:`33433`) From b5f4dfd19632da82807cb5fa4f59c23d5b911cc4 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sat, 16 May 2020 09:36:42 +0100 Subject: [PATCH 6/6] update whatsnew --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index ff6c9ab48ec6d..ab32dcc0deda7 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -815,7 +815,7 @@ Groupby/resample/rolling - 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`) - Bug in :meth:`GroupBy.first` and :meth:`GroupBy.last` where None is not preserved in object dtype (:issue:`32800`) - Bug in :meth:`Rolling.min` and :meth:`Rolling.max`: Growing memory usage after multiple calls when using a fixed window (:issue:`30726`) -- Bug in :meth:`Series.groupby` would raise ``ValueError`` when grouping by ``PeriodIndex`` level (:issue:`34010`) +- Bug in :meth:`Series.groupby` would raise ``ValueError`` when grouping by :class:`PeriodIndex` level (:issue:`34010`) - Bug in :meth:`GroupBy.agg`, :meth:`GroupBy.transform`, and :meth:`GroupBy.resample` where subclasses are not preserved (:issue:`28330`) - Bug in :meth:`GroupBy.rolling.apply` ignores args and kwargs parameters (:issue:`33433`)