From 2db38e70e7e9d1a0935fb48a0deaadcf92afb05d Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 3 Jul 2019 10:49:01 -0700 Subject: [PATCH 1/4] catch less --- pandas/core/indexing.py | 10 ++++++++-- pandas/core/internals/blocks.py | 18 ++++++++++-------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 677aefa15d200..d78022e1e4ac7 100755 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -110,10 +110,16 @@ def __getitem__(self, key): for x in key) try: values = self.obj._get_value(*key) + except KeyError: + pass + except TypeError: + if any(isinstance(x, (slice, list, MultiIndex)) for x in key): + pass + else: + raise + else: if is_scalar(values): return values - except Exception: - pass return self._getitem_tuple(key) else: diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index a9b2c0491458c..a6ebd5f811960 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -572,10 +572,9 @@ def _astype(self, dtype, copy=False, errors='raise', values=None, return self.copy() return self - try: - # force the copy here - if values is None: - + if values is None: + try: + # force the copy here if self.is_extension: values = self.values.astype(dtype) else: @@ -601,10 +600,13 @@ def _astype(self, dtype, copy=False, errors='raise', values=None, if isinstance(values, np.ndarray): values = values.reshape(self.shape) - except Exception: # noqa: E722 - if errors == 'raise': - raise - newb = self.copy() if copy else self + except Exception: # noqa: E722 + if errors == 'raise': + raise + newb = self.copy() if copy else self + else: + newb = make_block(values, placement=self.mgr_locs, + ndim=self.ndim) else: newb = make_block(values, placement=self.mgr_locs, ndim=self.ndim) From db9edb6bddff66e61bb28dab606dfdb2f22ce662 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 3 Jul 2019 16:27:01 -0700 Subject: [PATCH 2/4] catch less --- pandas/core/internals/blocks.py | 47 +++++++++++++++------------------ 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index a6ebd5f811960..f9a99a65e8d8d 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -370,10 +370,6 @@ def fillna(self, value, limit=None, inplace=False, downcast=None): # Note: we only call try_coerce_args to let it raise self._try_coerce_args(value) - blocks = self.putmask(mask, value, inplace=inplace) - blocks = [b.make_block(values=self._try_coerce_result(b.values)) - for b in blocks] - return self._maybe_downcast(blocks, downcast) except (TypeError, ValueError): # we can't process the value, but nothing to do @@ -393,6 +389,11 @@ def f(m, v, i): downcast=None) return self.split_and_operate(mask, f, inplace) + else: + blocks = self.putmask(mask, value, inplace=inplace) + blocks = [b.make_block(values=self._try_coerce_result(b.values)) + for b in blocks] + return self._maybe_downcast(blocks, downcast) def split_and_operate(self, mask, f, inplace): """ @@ -809,14 +810,6 @@ def setitem(self, indexer, value): values = self.values try: value = self._try_coerce_args(value) - values = self._coerce_values(values) - # can keep its own dtype - if hasattr(value, 'dtype') and is_dtype_equal(values.dtype, - value.dtype): - dtype = self.dtype - else: - dtype = 'infer' - except (TypeError, ValueError): # current dtype cannot store value, coerce to common dtype find_dtype = False @@ -841,6 +834,14 @@ def setitem(self, indexer, value): if not is_dtype_equal(self.dtype, dtype): b = self.astype(dtype) return b.setitem(indexer, value) + else: + values = self._coerce_values(values) + # can keep its own dtype + if hasattr(value, 'dtype') and is_dtype_equal(values.dtype, + value.dtype): + dtype = self.dtype + else: + dtype = 'infer' # value must be storeable at this moment arr_value = np.array(value) @@ -1894,13 +1895,14 @@ def where(self, other, cond, align=True, errors='raise', else: dtype = self.dtype + result = self.values.copy() + icond = ~cond + if lib.is_scalar(other): + set_other = other + else: + set_other = other[icond] try: - result = self.values.copy() - icond = ~cond - if lib.is_scalar(other): - result[icond] = other - else: - result[icond] = other[icond] + result[icond] = set_other except (NotImplementedError, TypeError): # NotImplementedError for class not implementing `__setitem__` # TypeError for SparseArray, which implements just to raise @@ -2158,10 +2160,7 @@ def _try_coerce_args(self, other): ------- base-type other """ - - if isinstance(other, bool): - raise TypeError - elif is_null_datetimelike(other): + if is_null_datetimelike(other): other = tslibs.iNaT elif isinstance(other, (datetime, np.datetime64, date)): other = self._box_func(other) @@ -2519,9 +2518,7 @@ def _try_coerce_args(self, other): base-type other """ - if isinstance(other, bool): - raise TypeError - elif is_null_datetimelike(other): + if is_null_datetimelike(other): other = tslibs.iNaT elif isinstance(other, (timedelta, np.timedelta64)): other = Timedelta(other).value From 3919f661212a4a94900973601a23a688065ba6c9 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 3 Jul 2019 17:54:43 -0700 Subject: [PATCH 3/4] Change Exception check to (KeyError,TypeError) --- pandas/core/indexing.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index d78022e1e4ac7..b777025c8f655 100755 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -110,13 +110,12 @@ def __getitem__(self, key): for x in key) try: values = self.obj._get_value(*key) - except KeyError: + except (KeyError, TypeError): + # TypeError occurs here if the key has non-hashable entries, + # generally slice or list. + # TODO(ix): most/all of the TypeError cases here are for ix, + # so this check can be removed once ix is removed. pass - except TypeError: - if any(isinstance(x, (slice, list, MultiIndex)) for x in key): - pass - else: - raise else: if is_scalar(values): return values From b547f1d9417b501445b963d398eef5198364fdfc Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 4 Jul 2019 11:44:25 -0700 Subject: [PATCH 4/4] blackify --- pandas/core/internals/blocks.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 699a69aa873ef..022d855d9a15b 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -431,8 +431,9 @@ def f(m, v, i): return self.split_and_operate(mask, f, inplace) else: blocks = self.putmask(mask, value, inplace=inplace) - blocks = [b.make_block(values=self._try_coerce_result(b.values)) - for b in blocks] + blocks = [ + b.make_block(values=self._try_coerce_result(b.values)) for b in blocks + ] return self._maybe_downcast(blocks, downcast) def split_and_operate(self, mask, f, inplace): @@ -643,12 +644,11 @@ def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs): values = values.reshape(self.shape) except Exception: # noqa: E722 - if errors == 'raise': + if errors == "raise": raise newb = self.copy() if copy else self else: - newb = make_block(values, placement=self.mgr_locs, - ndim=self.ndim) + newb = make_block(values, placement=self.mgr_locs, ndim=self.ndim) else: newb = make_block(values, placement=self.mgr_locs, ndim=self.ndim) @@ -888,11 +888,10 @@ def setitem(self, indexer, value): else: values = self._coerce_values(values) # can keep its own dtype - if hasattr(value, 'dtype') and is_dtype_equal(values.dtype, - value.dtype): + if hasattr(value, "dtype") and is_dtype_equal(values.dtype, value.dtype): dtype = self.dtype else: - dtype = 'infer' + dtype = "infer" # value must be storeable at this moment arr_value = np.array(value)