diff --git a/pandas/_libs/interval.pyx b/pandas/_libs/interval.pyx index 6e41ff189592c..a47303ddc93cf 100644 --- a/pandas/_libs/interval.pyx +++ b/pandas/_libs/interval.pyx @@ -194,7 +194,7 @@ cdef class IntervalMixin: f"expected {repr(self.closed)}.") -cdef _interval_like(other): +cdef bint _interval_like(other): return (hasattr(other, 'left') and hasattr(other, 'right') and hasattr(other, 'closed')) diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index 6a9ecfe709847..b2301ab0190c7 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -1059,23 +1059,6 @@ def is_datetime_or_timedelta_dtype(arr_or_dtype) -> bool: return _is_dtype_type(arr_or_dtype, classes(np.datetime64, np.timedelta64)) -def _is_unorderable_exception(e: TypeError) -> bool: - """ - Check if the exception raised is an unorderable exception. - - Parameters - ---------- - e : Exception or sub-class - The exception object to check. - - Returns - ------- - bool - Whether or not the exception raised is an unorderable exception. - """ - return "'>' not supported between instances of" in str(e) - - # This exists to silence numpy deprecation warnings, see GH#29553 def is_numeric_v_string_like(a, b): """ diff --git a/pandas/core/series.py b/pandas/core/series.py index c9684d0985173..9e8bdc099063c 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -33,7 +33,6 @@ validate_numeric_casting, ) from pandas.core.dtypes.common import ( - _is_unorderable_exception, ensure_platform_int, is_bool, is_categorical_dtype, @@ -1005,26 +1004,24 @@ def __setitem__(self, key, value): except (KeyError, ValueError): values = self._values if is_integer(key) and not self.index.inferred_type == "integer": + # positional setter values[key] = value else: + # GH#12862 adding an new key to the Series self.loc[key] = value except TypeError as e: if isinstance(key, tuple) and not isinstance(self.index, MultiIndex): raise ValueError("Can only tuple-index with a MultiIndex") from e - # python 3 type errors should be raised - if _is_unorderable_exception(e): - raise IndexError(key) from e - if com.is_bool_indexer(key): key = check_bool_indexer(self.index, key) key = np.asarray(key, dtype=bool) try: self._where(~key, value, inplace=True) - return except InvalidIndexError: self._set_values(key.astype(np.bool_), value) + return else: self._set_with(key, value) @@ -1044,20 +1041,8 @@ def _set_with(self, key, value): indexer = self.index._convert_slice_indexer(key, kind="getitem") return self._set_values(indexer, value) - elif is_scalar(key) and not is_integer(key) and key not in self.index: - # GH#12862 adding an new key to the Series - # Note: have to exclude integers because that is ambiguously - # position-based - self.loc[key] = value - return - else: - if isinstance(key, tuple): - try: - # TODO: no test cases that get here - self._set_values(key, value) - except Exception: - pass + assert not isinstance(key, tuple) if is_scalar(key): key = [key] @@ -1074,7 +1059,7 @@ def _set_with(self, key, value): if self.index.inferred_type == "integer": self._set_labels(key, value) else: - return self._set_values(key, value) + self._set_values(key, value) else: self._set_labels(key, value)