Skip to content

REF: implement mask_setitem_value #43632

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

Closed
Closed
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
28 changes: 26 additions & 2 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1239,8 +1239,7 @@ def _convert_to_indexer(self, key, axis: int):

if com.is_bool_indexer(key):
key = check_bool_indexer(labels, key)
(inds,) = key.nonzero()
return inds
return key
else:
return self._get_listlike_indexer(key, axis)[1]
else:
Expand Down Expand Up @@ -1804,6 +1803,7 @@ def _setitem_single_column(self, loc: int, value, plane_indexer):
The indexer we use for setitem along axis=0.
"""
pi = plane_indexer
pi, value = mask_setitem_value(pi, value, len(self.obj))

ser = self.obj._ixs(loc, axis=1)

Expand Down Expand Up @@ -2395,3 +2395,27 @@ def need_slice(obj: slice) -> bool:
or obj.stop is not None
or (obj.step is not None and obj.step != 1)
)


def mask_setitem_value(indexer, value, length: int):
"""
Convert a boolean indexer to a positional indexer, masking `value` if necessary.
"""
if com.is_bool_indexer(indexer):
indexer = np.asarray(indexer).nonzero()[0]
if is_list_like(value) and len(value) == length:
if not is_array_like(value):
value = [value[n] for n in indexer]
else:
value = value[indexer]

elif isinstance(indexer, tuple):
indexer = list(indexer)
for i, key in enumerate(indexer):
if com.is_bool_indexer(key):
new_key = np.asarray(key).nonzero()[0]
indexer[i] = new_key
# TODO: sometimes need to do take on the value?

indexer = tuple(indexer)
return indexer, value
3 changes: 3 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
is_exact_shape_match,
is_scalar_indexer,
)
from pandas.core.indexing import mask_setitem_value
import pandas.core.missing as missing

if TYPE_CHECKING:
Expand Down Expand Up @@ -936,6 +937,8 @@ def setitem(self, indexer, value):
if transpose:
values = values.T

indexer, value = mask_setitem_value(indexer, value, len(values))

# length checking
check_setitem_lengths(indexer, value, values)
exact_match = is_exact_shape_match(values, arr_value)
Expand Down