Skip to content

CLN: Catch more specific exceptions in groupby #27909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime, timedelta, date
import warnings

import cython

Expand Down
11 changes: 10 additions & 1 deletion pandas/_libs/index_class_helper.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,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):
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,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
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,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)):
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down