Skip to content

CLN: assorted indexing-related cleanups #31797

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 5 commits into from
Feb 10, 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
8 changes: 4 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2933,12 +2933,12 @@ def __setitem__(self, key, value):
# set column
self._set_item(key, value)

def _setitem_slice(self, key, value):
def _setitem_slice(self, key: slice, value):
# NB: we can't just use self.loc[key] = value because that
# operates on labels and we need to operate positional for
# backwards-compat, xref GH#31469
self._check_setitem_copy()
self.loc._setitem_with_indexer(key, value)
self.iloc._setitem_with_indexer(key, value)

def _setitem_array(self, key, value):
# also raises Exception if object array with NA values
Expand All @@ -2950,7 +2950,7 @@ def _setitem_array(self, key, value):
key = check_bool_indexer(self.index, key)
indexer = key.nonzero()[0]
self._check_setitem_copy()
self.loc._setitem_with_indexer(indexer, value)
self.iloc._setitem_with_indexer(indexer, value)
else:
if isinstance(value, DataFrame):
if len(value.columns) != len(key):
Expand All @@ -2962,7 +2962,7 @@ def _setitem_array(self, key, value):
key, axis=1, raise_missing=False
)[1]
self._check_setitem_copy()
self.loc._setitem_with_indexer((slice(None), indexer), value)
self.iloc._setitem_with_indexer((slice(None), indexer), value)

def _setitem_frame(self, key, value):
# support boolean setting with DataFrame input, e.g.
Expand Down
9 changes: 4 additions & 5 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3190,17 +3190,16 @@ def is_int(v):
# convert the slice to an indexer here

# if we are mixed and have integers
try:
if is_positional and self.is_mixed():
if is_positional and self.is_mixed():
try:
# Validate start & stop
if start is not None:
self.get_loc(start)
if stop is not None:
self.get_loc(stop)
is_positional = False
except KeyError:
if self.inferred_type in ["mixed-integer-float", "integer-na"]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was not needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not reachable; the check for self.is_mixed() a few lines up means these inferred_types are impossible

raise
except KeyError:
pass

if is_null_slicer:
indexer = key
Expand Down
15 changes: 8 additions & 7 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
is_list_like_indexer,
length_of_indexer,
)
from pandas.core.indexes.api import Index
from pandas.core.indexes.base import InvalidIndexError
from pandas.core.indexes.api import Index, InvalidIndexError

# "null slice"
_NS = slice(None, None)
Expand Down Expand Up @@ -592,6 +591,9 @@ def _get_label(self, label, axis: int):
return self.obj._xs(label, axis=axis)

def _get_setitem_indexer(self, key):
"""
Convert a potentially-label-based key into a positional indexer.
"""
if self.axis is not None:
return self._convert_tuple(key, is_setter=True)

Expand Down Expand Up @@ -756,7 +758,7 @@ def _setitem_with_indexer(self, indexer, value):
"defined index and a scalar"
)
self.obj[key] = value
return self.obj
return

# add a new item with the dtype setup
self.obj[key] = _infer_fill_value(value)
Expand All @@ -766,7 +768,7 @@ def _setitem_with_indexer(self, indexer, value):
)
self._setitem_with_indexer(new_indexer, value)

return self.obj
return

# reindex the axis
# make sure to clear the cache because we are
Expand All @@ -789,7 +791,8 @@ def _setitem_with_indexer(self, indexer, value):
indexer, missing = convert_missing_indexer(indexer)

if missing:
return self._setitem_with_indexer_missing(indexer, value)
self._setitem_with_indexer_missing(indexer, value)
return

# set
item_labels = self.obj._get_axis(info_axis)
Expand Down Expand Up @@ -1015,7 +1018,6 @@ def _setitem_with_indexer_missing(self, indexer, value):
new_values, index=new_index, name=self.obj.name
)._data
self.obj._maybe_update_cacher(clear=True)
return self.obj

elif self.ndim == 2:

Expand All @@ -1039,7 +1041,6 @@ def _setitem_with_indexer_missing(self, indexer, value):

self.obj._data = self.obj.append(value)._data
self.obj._maybe_update_cacher(clear=True)
return self.obj

def _align_series(self, indexer, ser: ABCSeries, multiindex_indexer: bool = False):
"""
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,9 @@ def __getitem__(self, key):

return result
except InvalidIndexError:
pass
if not isinstance(self.index, MultiIndex):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it wasn't obvious to me that non-MI shouldn't be allowed through here, this should clarify it for future readers (or me once I forget)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha; does that fix an issue somewhere else?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, just clarity, avoids doing some other checks before raising

raise

except (KeyError, ValueError):
if isinstance(key, tuple) and isinstance(self.index, MultiIndex):
# kludge
Expand Down