Skip to content

Commit 89138d6

Browse files
jbrockmendelproost
authored andcommitted
CLN: Catch more specific exceptions in groupby (pandas-dev#27909)
* catch stricter
1 parent 2aab3fb commit 89138d6

File tree

6 files changed

+19
-8
lines changed

6 files changed

+19
-8
lines changed

pandas/_libs/index.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime, timedelta, date
2+
import warnings
23

34
import cython
45

pandas/_libs/index_class_helper.pxi.in

+10-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,16 @@ cdef class {{name}}Engine(IndexEngine):
6060

6161
# A view is needed for some subclasses, such as PeriodEngine:
6262
values = self._get_index_values().view('{{dtype}}')
63-
indexer = values == val
63+
try:
64+
with warnings.catch_warnings():
65+
# e.g. if values is float64 and `val` is a str, suppress warning
66+
warnings.filterwarnings("ignore", category=FutureWarning)
67+
indexer = values == val
68+
except TypeError:
69+
# if the equality above returns a bool, cython will raise TypeError
70+
# when trying to cast it to ndarray
71+
raise KeyError(val)
72+
6473
found = np.where(indexer)[0]
6574
count = len(found)
6675

pandas/core/groupby/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def _decide_output_index(self, output, labels):
349349
output_keys = sorted(output)
350350
try:
351351
output_keys.sort()
352-
except Exception: # pragma: no cover
352+
except TypeError:
353353
pass
354354

355355
if isinstance(labels, MultiIndex):

pandas/core/groupby/groupby.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -727,8 +727,7 @@ def f(g):
727727
with option_context("mode.chained_assignment", None):
728728
try:
729729
result = self._python_apply_general(f)
730-
except Exception:
731-
730+
except TypeError:
732731
# gh-20949
733732
# try again, with .apply acting as a filtering
734733
# operation, by excluding the grouping column

pandas/core/groupby/grouper.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -592,9 +592,11 @@ def is_in_axis(key):
592592

593593
# if the grouper is obj[name]
594594
def is_in_obj(gpr):
595+
if not hasattr(gpr, "name"):
596+
return False
595597
try:
596-
return id(gpr) == id(obj[gpr.name])
597-
except Exception:
598+
return gpr is obj[gpr.name]
599+
except (KeyError, IndexError):
598600
return False
599601

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

pandas/core/groupby/ops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ def apply(self, f, data, axis=0):
212212
# This Exception is also raised if `f` triggers an exception
213213
# but it is preferable to raise the exception in Python.
214214
pass
215-
except Exception:
216-
# raise this error to the caller
215+
except TypeError:
216+
# occurs if we have any EAs
217217
pass
218218

219219
for key, (i, group) in zip(group_keys, splitter):

0 commit comments

Comments
 (0)