Skip to content

Commit ec0e284

Browse files
authored
CLN: remove unnecessary branches in Series.__setitem__ (#33424)
1 parent 1fe47b6 commit ec0e284

File tree

3 files changed

+6
-38
lines changed

3 files changed

+6
-38
lines changed

pandas/_libs/interval.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ cdef class IntervalMixin:
194194
f"expected {repr(self.closed)}.")
195195

196196

197-
cdef _interval_like(other):
197+
cdef bint _interval_like(other):
198198
return (hasattr(other, 'left')
199199
and hasattr(other, 'right')
200200
and hasattr(other, 'closed'))

pandas/core/dtypes/common.py

-17
Original file line numberDiff line numberDiff line change
@@ -1059,23 +1059,6 @@ def is_datetime_or_timedelta_dtype(arr_or_dtype) -> bool:
10591059
return _is_dtype_type(arr_or_dtype, classes(np.datetime64, np.timedelta64))
10601060

10611061

1062-
def _is_unorderable_exception(e: TypeError) -> bool:
1063-
"""
1064-
Check if the exception raised is an unorderable exception.
1065-
1066-
Parameters
1067-
----------
1068-
e : Exception or sub-class
1069-
The exception object to check.
1070-
1071-
Returns
1072-
-------
1073-
bool
1074-
Whether or not the exception raised is an unorderable exception.
1075-
"""
1076-
return "'>' not supported between instances of" in str(e)
1077-
1078-
10791062
# This exists to silence numpy deprecation warnings, see GH#29553
10801063
def is_numeric_v_string_like(a, b):
10811064
"""

pandas/core/series.py

+5-20
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
validate_numeric_casting,
3434
)
3535
from pandas.core.dtypes.common import (
36-
_is_unorderable_exception,
3736
ensure_platform_int,
3837
is_bool,
3938
is_categorical_dtype,
@@ -1015,26 +1014,24 @@ def __setitem__(self, key, value):
10151014
except (KeyError, ValueError):
10161015
values = self._values
10171016
if is_integer(key) and not self.index.inferred_type == "integer":
1017+
# positional setter
10181018
values[key] = value
10191019
else:
1020+
# GH#12862 adding an new key to the Series
10201021
self.loc[key] = value
10211022

10221023
except TypeError as e:
10231024
if isinstance(key, tuple) and not isinstance(self.index, MultiIndex):
10241025
raise ValueError("Can only tuple-index with a MultiIndex") from e
10251026

1026-
# python 3 type errors should be raised
1027-
if _is_unorderable_exception(e):
1028-
raise IndexError(key) from e
1029-
10301027
if com.is_bool_indexer(key):
10311028
key = check_bool_indexer(self.index, key)
10321029
key = np.asarray(key, dtype=bool)
10331030
try:
10341031
self._where(~key, value, inplace=True)
1035-
return
10361032
except InvalidIndexError:
10371033
self._set_values(key.astype(np.bool_), value)
1034+
return
10381035

10391036
else:
10401037
self._set_with(key, value)
@@ -1054,20 +1051,8 @@ def _set_with(self, key, value):
10541051
indexer = self.index._convert_slice_indexer(key, kind="getitem")
10551052
return self._set_values(indexer, value)
10561053

1057-
elif is_scalar(key) and not is_integer(key) and key not in self.index:
1058-
# GH#12862 adding an new key to the Series
1059-
# Note: have to exclude integers because that is ambiguously
1060-
# position-based
1061-
self.loc[key] = value
1062-
return
1063-
10641054
else:
1065-
if isinstance(key, tuple):
1066-
try:
1067-
# TODO: no test cases that get here
1068-
self._set_values(key, value)
1069-
except Exception:
1070-
pass
1055+
assert not isinstance(key, tuple)
10711056

10721057
if is_scalar(key):
10731058
key = [key]
@@ -1084,7 +1069,7 @@ def _set_with(self, key, value):
10841069
if self.index.inferred_type == "integer":
10851070
self._set_labels(key, value)
10861071
else:
1087-
return self._set_values(key, value)
1072+
self._set_values(key, value)
10881073
else:
10891074
self._set_labels(key, value)
10901075

0 commit comments

Comments
 (0)