From ab78324024cefc9bf0fbfa6bb39db8c023807741 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 13 Aug 2019 13:17:22 -0700 Subject: [PATCH 1/2] catch stricter --- pandas/core/groupby/groupby.py | 3 +-- pandas/core/groupby/ops.py | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index c5e81e21e9fd5..1bcbecddd082a 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -726,8 +726,7 @@ def f(g): with option_context("mode.chained_assignment", None): try: result = self._python_apply_general(f) - except Exception: - + except TypeError: # gh-20949 # try again, with .apply acting as a filtering # operation, by excluding the grouping column diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index b0c629f017dd3..8cdc62dc3d47a 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -212,8 +212,8 @@ def apply(self, f, data, axis=0): # This Exception is also raised if `f` triggers an exception # but it is preferable to raise the exception in Python. pass - except Exception: - # raise this error to the caller + except TypeError: + # occurs if we have any EAs pass for key, (i, group) in zip(group_keys, splitter): From 8d339d4c90c87294582cd629344546bd4018b073 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 13 Aug 2019 14:37:27 -0700 Subject: [PATCH 2/2] CLN: catch more specific errors in groupby --- pandas/_libs/index.pyx | 1 + pandas/_libs/index_class_helper.pxi.in | 11 ++++++++++- pandas/core/groupby/generic.py | 2 +- pandas/core/groupby/grouper.py | 6 ++++-- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/pandas/_libs/index.pyx b/pandas/_libs/index.pyx index 7424c4ddc3d92..979dad6db0838 100644 --- a/pandas/_libs/index.pyx +++ b/pandas/_libs/index.pyx @@ -1,4 +1,5 @@ from datetime import datetime, timedelta, date +import warnings import cython diff --git a/pandas/_libs/index_class_helper.pxi.in b/pandas/_libs/index_class_helper.pxi.in index 3c9a096e7ecc0..4db048eeb0383 100644 --- a/pandas/_libs/index_class_helper.pxi.in +++ b/pandas/_libs/index_class_helper.pxi.in @@ -60,7 +60,16 @@ cdef class {{name}}Engine(IndexEngine): # A view is needed for some subclasses, such as PeriodEngine: values = self._get_index_values().view('{{dtype}}') - indexer = values == val + try: + with warnings.catch_warnings(): + # e.g. if values is float64 and `val` is a str, suppress warning + warnings.filterwarnings("ignore", category=FutureWarning) + indexer = values == val + except TypeError: + # if the equality above returns a bool, cython will raise TypeError + # when trying to cast it to ndarray + raise KeyError(val) + found = np.where(indexer)[0] count = len(found) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index ea2bd22cccc3d..def2031d45621 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -346,7 +346,7 @@ def _decide_output_index(self, output, labels): output_keys = sorted(output) try: output_keys.sort() - except Exception: # pragma: no cover + except TypeError: pass if isinstance(labels, MultiIndex): diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index 143755a47b97b..fc8b44bd2c5e2 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -593,9 +593,11 @@ def is_in_axis(key): # if the grouper is obj[name] def is_in_obj(gpr): + 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): return False for i, (gpr, level) in enumerate(zip(keys, levels)):