From cd99e8bb6d9ce5dcd22c9a591dbbf60a8b5f479a Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 18 Oct 2019 11:21:07 -0700 Subject: [PATCH 1/3] CLN: tighten exception catching in indexes --- pandas/core/indexes/base.py | 17 ++++------------- pandas/core/indexes/period.py | 10 ++++++++-- pandas/core/indexes/timedeltas.py | 3 ++- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 526b2c2e2c412..1a08609ccd99a 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3142,16 +3142,7 @@ def is_int(v): elif is_positional: indexer = key else: - try: - indexer = self.slice_indexer(start, stop, step, kind=kind) - except Exception: - if is_index_slice: - if self.is_integer(): - raise - else: - indexer = key - else: - raise + indexer = self.slice_indexer(start, stop, step, kind=kind) return indexer @@ -4676,11 +4667,11 @@ def get_value(self, series, key): raise InvalidIndexError(key) else: raise e1 - except Exception: # pragma: no cover + except Exception: raise e1 except TypeError: - # python 3 - if is_scalar(key): # pragma: no cover + # e.g. "[False] is an invalid key" + if is_scalar(key): raise IndexError(key) raise InvalidIndexError(key) diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 0fc74f4e78c9f..f085dff84462d 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -457,7 +457,11 @@ def __contains__(self, key): try: self.get_loc(key) return True - except Exception: + except (ValueError, TypeError, KeyError): + # TypeError can be reached if we pass a tuple that is not hashable + # ValueError can be reached if pass a 2-tuple and parse_time_string + # raises with the wrong number of return values + # TODO: the latter is a bug in parse_time_string return False @cache_readonly @@ -765,7 +769,9 @@ def _maybe_cast_slice_bound(self, label, side, kind): _, parsed, reso = parse_time_string(label, self.freq) bounds = self._parsed_string_to_bounds(reso, parsed) return bounds[0 if side == "left" else 1] - except Exception: + except ValueError: + # string cannot be parsed as datetime-like + # TODO: we need tests for this case raise KeyError(label) elif is_integer(label) or is_float(label): self._invalid_indexer("slice", label) diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index 755992c881fe5..62a74fefa6577 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -630,7 +630,8 @@ def insert(self, loc, item): if _is_convertible_to_td(item): try: item = Timedelta(item) - except Exception: + except ValueError: + # e.g. str that can't be parsed to timedelta pass elif is_scalar(item) and isna(item): # GH 18295 From f1b30a410a59c0026844026e2431efc38d621726 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 18 Oct 2019 11:53:02 -0700 Subject: [PATCH 2/3] commit so i can rebase --- pandas/core/dtypes/cast.py | 2 +- pandas/core/groupby/generic.py | 22 ++++++++++++++++++++++ pandas/core/series.py | 6 ++---- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index dd001e78c07de..17b44569be283 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1052,7 +1052,7 @@ def try_datetime(v): pass except Exception: - pass + raise # no test failures return v.reshape(shape) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 8e53972c95275..b3cd646632a9c 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -891,7 +891,26 @@ def aggregate(self, func=None, *args, **kwargs): ) except AssertionError: raise + except ValueError as err: + if "no results" not in str(err): + # raised directly by _aggregate_multiple_funcs + raise + result = self._aggregate_frame(func) + except NotImplementedError as err: + if "axis other than 0 is not supported" in str(err): + # raised directly by _aggregate_multiple_funcs + pass + elif "decimal does not support skipna=True" in str(err): + # FIXME: kludge for DecimalArray tests + pass + else: + raise + # FIXME: this is raised in a bunch of + # test_whitelist.test_regression_whitelist_methods tests, + # can be avoided + result = self._aggregate_frame(func) except Exception: + raise result = self._aggregate_frame(func) else: result.columns = Index( @@ -1060,6 +1079,7 @@ def _aggregate_frame(self, func, *args, **kwargs): except AssertionError: raise except Exception: + raise return self._aggregate_item_by_item(func, *args, **kwargs) else: for name in self.indices: @@ -1426,6 +1446,7 @@ def _choose_path(self, fast_path, slow_path, group): except AssertionError: raise except Exception: + raise # Hard to know ex-ante what exceptions `fast_path` might raise return path, res @@ -1452,6 +1473,7 @@ def _transform_item_by_item(self, obj, wrapper): except AssertionError: raise except Exception: + #raise pass else: inds.append(i) diff --git a/pandas/core/series.py b/pandas/core/series.py index 1039e9af929d4..cc62c74b303c9 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1287,10 +1287,8 @@ def _set_with(self, key, value): else: if isinstance(key, tuple): - try: - self._set_values(key, value) - except Exception: - pass + # TODO: apparently no test cases that get here + self._set_values(key, value) if is_scalar(key): key = [key] From ca30a9d6127c90590853c2d021b865b8911585a5 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 18 Oct 2019 11:58:02 -0700 Subject: [PATCH 3/3] revert --- pandas/core/dtypes/cast.py | 2 +- pandas/core/groupby/generic.py | 22 ---------------------- pandas/core/series.py | 6 ++++-- 3 files changed, 5 insertions(+), 25 deletions(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 17b44569be283..dd001e78c07de 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1052,7 +1052,7 @@ def try_datetime(v): pass except Exception: - raise # no test failures + pass return v.reshape(shape) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index b3cd646632a9c..8e53972c95275 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -891,26 +891,7 @@ def aggregate(self, func=None, *args, **kwargs): ) except AssertionError: raise - except ValueError as err: - if "no results" not in str(err): - # raised directly by _aggregate_multiple_funcs - raise - result = self._aggregate_frame(func) - except NotImplementedError as err: - if "axis other than 0 is not supported" in str(err): - # raised directly by _aggregate_multiple_funcs - pass - elif "decimal does not support skipna=True" in str(err): - # FIXME: kludge for DecimalArray tests - pass - else: - raise - # FIXME: this is raised in a bunch of - # test_whitelist.test_regression_whitelist_methods tests, - # can be avoided - result = self._aggregate_frame(func) except Exception: - raise result = self._aggregate_frame(func) else: result.columns = Index( @@ -1079,7 +1060,6 @@ def _aggregate_frame(self, func, *args, **kwargs): except AssertionError: raise except Exception: - raise return self._aggregate_item_by_item(func, *args, **kwargs) else: for name in self.indices: @@ -1446,7 +1426,6 @@ def _choose_path(self, fast_path, slow_path, group): except AssertionError: raise except Exception: - raise # Hard to know ex-ante what exceptions `fast_path` might raise return path, res @@ -1473,7 +1452,6 @@ def _transform_item_by_item(self, obj, wrapper): except AssertionError: raise except Exception: - #raise pass else: inds.append(i) diff --git a/pandas/core/series.py b/pandas/core/series.py index cc62c74b303c9..1039e9af929d4 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1287,8 +1287,10 @@ def _set_with(self, key, value): else: if isinstance(key, tuple): - # TODO: apparently no test cases that get here - self._set_values(key, value) + try: + self._set_values(key, value) + except Exception: + pass if is_scalar(key): key = [key]