Skip to content

CLN: remove unnecessary branches in Series.__setitem__ #33424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/_libs/interval.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
17 changes: 0 additions & 17 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
25 changes: 5 additions & 20 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
validate_numeric_casting,
)
from pandas.core.dtypes.common import (
_is_unorderable_exception,
ensure_platform_int,
is_bool,
is_categorical_dtype,
Expand Down Expand Up @@ -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)
Expand All @@ -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]
Expand All @@ -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)

Expand Down