From 4297f82babe64a219604ebb54468ef0be953f3ca Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 17 Apr 2020 13:55:17 -0700 Subject: [PATCH] CLN: avoid catching AssertionError, AttributeError in NDFrame methods --- pandas/core/generic.py | 13 +++---------- pandas/core/internals/blocks.py | 7 ++++--- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 2878204f5ee79..a77af4139d188 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -701,10 +701,8 @@ def pop(self: FrameOrSeries, item) -> FrameOrSeries: """ result = self[item] del self[item] - try: + if self.ndim == 2: result._reset_cacher() - except AttributeError: - pass return result @@ -3254,14 +3252,9 @@ def _maybe_update_cacher( if ref is None: del self._cacher else: - # Note: we need to call ref._maybe_cache_changed even in the - # case where it will raise. (Uh, not clear why) - try: + if len(self) == len(ref): + # otherwise, either self or ref has swapped in new arrays ref._maybe_cache_changed(cacher[0], self) - except AssertionError: - # ref._mgr.setitem can raise - # AssertionError because of shape mismatch - pass if verify_is_copy: self._check_setitem_copy(stacklevel=5, t="referant") diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 1bcbcb61ddde4..bb16bd0550aba 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -51,7 +51,6 @@ from pandas.core.dtypes.dtypes import ExtensionDtype from pandas.core.dtypes.generic import ( ABCDataFrame, - ABCExtensionArray, ABCIndexClass, ABCPandasArray, ABCSeries, @@ -2758,8 +2757,10 @@ def _safe_reshape(arr, new_shape): """ if isinstance(arr, ABCSeries): arr = arr._values - if not isinstance(arr, ABCExtensionArray): - arr = arr.reshape(new_shape) + if not is_extension_array_dtype(arr.dtype): + # Note: this will include TimedeltaArray and tz-naive DatetimeArray + # TODO(EA2D): special case will be unnecessary with 2D EAs + arr = np.asarray(arr).reshape(new_shape) return arr