Skip to content

Commit 1219b0f

Browse files
jbrockmendeljreback
authored andcommitted
CLN: Move code outside of try/except blocks (pandas-dev#27223)
1 parent de006f8 commit 1219b0f

File tree

2 files changed

+38
-36
lines changed

2 files changed

+38
-36
lines changed

pandas/core/indexing.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,15 @@ def __getitem__(self, key):
118118
key = tuple(com.apply_if_callable(x, self.obj) for x in key)
119119
try:
120120
values = self.obj._get_value(*key)
121+
except (KeyError, TypeError):
122+
# TypeError occurs here if the key has non-hashable entries,
123+
# generally slice or list.
124+
# TODO(ix): most/all of the TypeError cases here are for ix,
125+
# so this check can be removed once ix is removed.
126+
pass
127+
else:
121128
if is_scalar(values):
122129
return values
123-
except Exception:
124-
pass
125130

126131
return self._getitem_tuple(key)
127132
else:

pandas/core/internals/blocks.py

+31-34
Original file line numberDiff line numberDiff line change
@@ -413,12 +413,6 @@ def fillna(self, value, limit=None, inplace=False, downcast=None):
413413
try:
414414
# Note: we only call try_coerce_args to let it raise
415415
self._try_coerce_args(value)
416-
417-
blocks = self.putmask(mask, value, inplace=inplace)
418-
blocks = [
419-
b.make_block(values=self._try_coerce_result(b.values)) for b in blocks
420-
]
421-
return self._maybe_downcast(blocks, downcast)
422416
except (TypeError, ValueError):
423417

424418
# we can't process the value, but nothing to do
@@ -435,6 +429,12 @@ def f(m, v, i):
435429
return block.fillna(value, limit=limit, inplace=inplace, downcast=None)
436430

437431
return self.split_and_operate(mask, f, inplace)
432+
else:
433+
blocks = self.putmask(mask, value, inplace=inplace)
434+
blocks = [
435+
b.make_block(values=self._try_coerce_result(b.values)) for b in blocks
436+
]
437+
return self._maybe_downcast(blocks, downcast)
438438

439439
def split_and_operate(self, mask, f, inplace):
440440
"""
@@ -615,10 +615,9 @@ def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
615615
return self.copy()
616616
return self
617617

618-
try:
619-
# force the copy here
620-
if values is None:
621-
618+
if values is None:
619+
try:
620+
# force the copy here
622621
if self.is_extension:
623622
values = self.values.astype(dtype)
624623
else:
@@ -644,10 +643,12 @@ def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
644643
if isinstance(values, np.ndarray):
645644
values = values.reshape(self.shape)
646645

647-
except Exception: # noqa: E722
648-
if errors == "raise":
649-
raise
650-
newb = self.copy() if copy else self
646+
except Exception: # noqa: E722
647+
if errors == "raise":
648+
raise
649+
newb = self.copy() if copy else self
650+
else:
651+
newb = make_block(values, placement=self.mgr_locs, ndim=self.ndim)
651652
else:
652653
newb = make_block(values, placement=self.mgr_locs, ndim=self.ndim)
653654

@@ -861,13 +862,6 @@ def setitem(self, indexer, value):
861862
values = self.values
862863
try:
863864
value = self._try_coerce_args(value)
864-
values = self._coerce_values(values)
865-
# can keep its own dtype
866-
if hasattr(value, "dtype") and is_dtype_equal(values.dtype, value.dtype):
867-
dtype = self.dtype
868-
else:
869-
dtype = "infer"
870-
871865
except (TypeError, ValueError):
872866
# current dtype cannot store value, coerce to common dtype
873867
find_dtype = False
@@ -891,6 +885,13 @@ def setitem(self, indexer, value):
891885
if not is_dtype_equal(self.dtype, dtype):
892886
b = self.astype(dtype)
893887
return b.setitem(indexer, value)
888+
else:
889+
values = self._coerce_values(values)
890+
# can keep its own dtype
891+
if hasattr(value, "dtype") and is_dtype_equal(values.dtype, value.dtype):
892+
dtype = self.dtype
893+
else:
894+
dtype = "infer"
894895

895896
# value must be storeable at this moment
896897
arr_value = np.array(value)
@@ -2041,13 +2042,14 @@ def where(
20412042
else:
20422043
dtype = self.dtype
20432044

2045+
result = self.values.copy()
2046+
icond = ~cond
2047+
if lib.is_scalar(other):
2048+
set_other = other
2049+
else:
2050+
set_other = other[icond]
20442051
try:
2045-
result = self.values.copy()
2046-
icond = ~cond
2047-
if lib.is_scalar(other):
2048-
result[icond] = other
2049-
else:
2050-
result[icond] = other[icond]
2052+
result[icond] = set_other
20512053
except (NotImplementedError, TypeError):
20522054
# NotImplementedError for class not implementing `__setitem__`
20532055
# TypeError for SparseArray, which implements just to raise
@@ -2314,10 +2316,7 @@ def _try_coerce_args(self, other):
23142316
-------
23152317
base-type other
23162318
"""
2317-
2318-
if isinstance(other, bool):
2319-
raise TypeError
2320-
elif is_null_datetimelike(other):
2319+
if is_null_datetimelike(other):
23212320
other = tslibs.iNaT
23222321
elif isinstance(other, (datetime, np.datetime64, date)):
23232322
other = self._box_func(other)
@@ -2689,9 +2688,7 @@ def _try_coerce_args(self, other):
26892688
base-type other
26902689
"""
26912690

2692-
if isinstance(other, bool):
2693-
raise TypeError
2694-
elif is_null_datetimelike(other):
2691+
if is_null_datetimelike(other):
26952692
other = tslibs.iNaT
26962693
elif isinstance(other, (timedelta, np.timedelta64)):
26972694
other = Timedelta(other).value

0 commit comments

Comments
 (0)