Skip to content

Commit

Permalink
CLN: one less try/except in Block methods (#27606)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and jreback committed Jul 26, 2019
1 parent d170cc9 commit a39f238
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
29 changes: 15 additions & 14 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,9 @@ def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
if isinstance(values, np.ndarray):
values = values.reshape(self.shape)

except Exception: # noqa: E722
except Exception:
# e.g. astype_nansafe can fail on object-dtype of strings
# trying to convert to float
if errors == "raise":
raise
newb = self.copy() if copy else self
Expand Down Expand Up @@ -877,9 +879,17 @@ def setitem(self, indexer, value):

# coerce if block dtype can store value
values = self.values
try:
if self._can_hold_element(value):
value = self._try_coerce_args(value)
except (TypeError, ValueError):

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"

else:
# current dtype cannot store value, coerce to common dtype
find_dtype = False

Expand All @@ -902,13 +912,6 @@ 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)
Expand Down Expand Up @@ -938,7 +941,7 @@ def setitem(self, indexer, value):
elif (
len(arr_value.shape)
and arr_value.shape[0] == values.shape[0]
and np.prod(arr_value.shape) == np.prod(values.shape)
and arr_value.size == values.size
):
values[indexer] = value
try:
Expand Down Expand Up @@ -1134,9 +1137,7 @@ def coerce_to_target_dtype(self, other):
try:
return self.astype(dtype)
except (ValueError, TypeError, OverflowError):
pass

return self.astype(object)
return self.astype(object)

def interpolate(
self,
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def init_dict(data, index, columns, dtype=None):
arrays = Series(data, index=columns, dtype=object)
data_names = arrays.index

missing = arrays.isnull()
missing = arrays.isna()
if index is None:
# GH10856
# raise ValueError if only scalars in dict
Expand Down
22 changes: 11 additions & 11 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def remove_na(arr):
"""

warnings.warn(
"remove_na is deprecated and is a private " "function. Do not use.",
"remove_na is deprecated and is a private function. Do not use.",
FutureWarning,
stacklevel=2,
)
Expand All @@ -127,7 +127,7 @@ def _coerce_method(converter):
def wrapper(self):
if len(self) == 1:
return converter(self.iloc[0])
raise TypeError("cannot convert the series to " "{0}".format(str(converter)))
raise TypeError("cannot convert the series to {0}".format(str(converter)))

wrapper.__name__ = "__{name}__".format(name=converter.__name__)
return wrapper
Expand Down Expand Up @@ -226,7 +226,7 @@ def __init__(

if isinstance(data, MultiIndex):
raise NotImplementedError(
"initializing a Series from a " "MultiIndex is not supported"
"initializing a Series from a MultiIndex is not supported"
)
elif isinstance(data, Index):
if name is None:
Expand Down Expand Up @@ -275,7 +275,7 @@ def __init__(
pass
elif isinstance(data, (set, frozenset)):
raise TypeError(
"{0!r} type is unordered" "".format(data.__class__.__name__)
"{0!r} type is unordered".format(data.__class__.__name__)
)
elif isinstance(data, ABCSparseArray):
# handle sparse passed here (and force conversion)
Expand Down Expand Up @@ -604,7 +604,7 @@ def asobject(self):
*this is an internal non-public method*
"""
warnings.warn(
"'asobject' is deprecated. Use 'astype(object)'" " instead",
"'asobject' is deprecated. Use 'astype(object)' instead",
FutureWarning,
stacklevel=2,
)
Expand Down Expand Up @@ -710,7 +710,7 @@ def put(self, *args, **kwargs):
numpy.ndarray.put
"""
warnings.warn(
"`put` has been deprecated and will be removed in a" "future version.",
"`put` has been deprecated and will be removed in a future version.",
FutureWarning,
stacklevel=2,
)
Expand Down Expand Up @@ -955,7 +955,7 @@ def real(self):
.. deprecated 0.25.0
"""
warnings.warn(
"`real` has be deprecated and will be removed in a " "future verison",
"`real` has be deprecated and will be removed in a future version",
FutureWarning,
stacklevel=2,
)
Expand All @@ -973,7 +973,7 @@ def imag(self):
.. deprecated 0.25.0
"""
warnings.warn(
"`imag` has be deprecated and will be removed in a " "future verison",
"`imag` has be deprecated and will be removed in a future version",
FutureWarning,
stacklevel=2,
)
Expand Down Expand Up @@ -1561,7 +1561,7 @@ def reset_index(self, level=None, drop=False, name=None, inplace=False):
).__finalize__(self)
elif inplace:
raise TypeError(
"Cannot reset_index inplace on a Series " "to create a DataFrame"
"Cannot reset_index inplace on a Series to create a DataFrame"
)
else:
df = self.to_frame(name)
Expand Down Expand Up @@ -1813,7 +1813,7 @@ def to_sparse(self, kind="block", fill_value=None):
"""

warnings.warn(
"Series.to_sparse is deprecated and will be removed " "in a future version",
"Series.to_sparse is deprecated and will be removed in a future version",
FutureWarning,
stacklevel=2,
)
Expand Down Expand Up @@ -4055,7 +4055,7 @@ def _reduce(
elif isinstance(delegate, np.ndarray):
if numeric_only:
raise NotImplementedError(
"Series.{0} does not implement " "numeric_only.".format(name)
"Series.{0} does not implement numeric_only.".format(name)
)
with np.errstate(all="ignore"):
return op(delegate, skipna=skipna, **kwds)
Expand Down

0 comments on commit a39f238

Please sign in to comment.