From dfc0e4f30ff1bbee522dabf0cfa1b051737254bd Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 25 Jul 2019 20:42:14 -0700 Subject: [PATCH 1/2] CLN: one less try/except, smallc leansups --- pandas/core/internals/blocks.py | 29 ++++++++++++++------------- pandas/core/internals/construction.py | 4 ++-- pandas/core/series.py | 22 ++++++++++---------- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 83849ea41d032..6e5a2aab298c7 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -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 @@ -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 @@ -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) @@ -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: @@ -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, diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index 74b16f0e72883..78e48de13fb00 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -202,7 +202,7 @@ def init_ndarray(values, index, columns, dtype=None, copy=False): return create_block_manager_from_blocks(block_values, [columns, index]) -def init_dict(data, index, columns, dtype=None): +def init_dict(data: dict, index, columns, dtype=None): """ Segregate Series based on type and coerce into matrices. Needs to handle a lot of exceptional cases. @@ -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 diff --git a/pandas/core/series.py b/pandas/core/series.py index 59f39758cb3b0..6054f592f7409 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -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, ) @@ -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 @@ -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: @@ -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) @@ -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, ) @@ -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, ) @@ -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, ) @@ -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, ) @@ -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) @@ -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, ) @@ -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) From c36a16b7487e5f73ec60cda5e2da29c5d551307f Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 25 Jul 2019 21:06:33 -0700 Subject: [PATCH 2/2] undo type --- pandas/core/internals/construction.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index 78e48de13fb00..3126b9d9d3e2e 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -202,7 +202,7 @@ def init_ndarray(values, index, columns, dtype=None, copy=False): return create_block_manager_from_blocks(block_values, [columns, index]) -def init_dict(data: dict, index, columns, dtype=None): +def init_dict(data, index, columns, dtype=None): """ Segregate Series based on type and coerce into matrices. Needs to handle a lot of exceptional cases.