Skip to content

CLN: simplify _setitem_with_indexer #31887

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 6 commits into from
Feb 20, 2020
Merged
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
49 changes: 27 additions & 22 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,17 @@ def _convert_to_indexer(self, key, axis: int, is_setter: bool = False):
# -------------------------------------------------------------------

def _setitem_with_indexer(self, indexer, value):
"""
_setitem_with_indexer is for setting values on a Series/DataFrame
using positional indexers.

If the relevant keys are not present, the Series/DataFrame may be
expanded.

This method is currently broken when dealing with non-unique Indexes,
since it goes from positional indexers back to labels when calling
BlockManager methods, see GH#12991, GH#22046, GH#15686.
"""

# also has the side effect of consolidating in-place
from pandas import Series
Expand Down Expand Up @@ -1678,24 +1689,19 @@ def _setitem_with_indexer(self, indexer, value):
info_idx = [info_idx]
labels = item_labels[info_idx]

# if we have a partial multiindex, then need to adjust the plane
# indexer here
if len(labels) == 1 and isinstance(
self.obj[labels[0]].axes[0], ABCMultiIndex
):
if len(labels) == 1:
# We can operate on a single column
item = labels[0]
obj = self.obj[item]
index = obj.index
idx = indexer[:info_axis][0]
idx = indexer[0]

plane_indexer = tuple([idx]) + indexer[info_axis + 1 :]
lplane_indexer = length_of_indexer(plane_indexer[0], index)
plane_indexer = tuple([idx])
lplane_indexer = length_of_indexer(plane_indexer[0], self.obj.index)
# lplane_indexer gives the expected length of obj[idx]

# require that we are setting the right number of values that
# we are indexing
if is_list_like_indexer(value) and lplane_indexer != len(value):

if is_list_like_indexer(value) and 0 != lplane_indexer != len(value):
# Exclude zero-len for e.g. boolean masking that is all-false
raise ValueError(
"cannot set using a multi-index "
"selection indexer with a different "
Expand All @@ -1704,12 +1710,11 @@ def _setitem_with_indexer(self, indexer, value):

# non-mi
else:
plane_indexer = indexer[:info_axis] + indexer[info_axis + 1 :]
plane_axis = self.obj.axes[:info_axis][0]
lplane_indexer = length_of_indexer(plane_indexer[0], plane_axis)
plane_indexer = indexer[:1]
lplane_indexer = length_of_indexer(plane_indexer[0], self.obj.index)

def setter(item, v):
s = self.obj[item]
ser = self.obj[item]
pi = plane_indexer[0] if lplane_indexer == 1 else plane_indexer

# perform the equivalent of a setitem on the info axis
Expand All @@ -1721,16 +1726,16 @@ def setter(item, v):
com.is_null_slice(idx) or com.is_full_slice(idx, len(self.obj))
for idx in pi
):
s = v
ser = v
else:
# set the item, possibly having a dtype change
s._consolidate_inplace()
s = s.copy()
s._data = s._data.setitem(indexer=pi, value=v)
s._maybe_update_cacher(clear=True)
ser._consolidate_inplace()
ser = ser.copy()
ser._data = ser._data.setitem(indexer=pi, value=v)
ser._maybe_update_cacher(clear=True)

# reset the sliced object if unique
self.obj[item] = s
self.obj[item] = ser

# we need an iterable, with a ndim of at least 1
# eg. don't pass through np.array(0)
Expand Down