Skip to content

iloc boolean mask setitem #20438

Closed
Closed
@TomAugspurger

Description

@TomAugspurger

Is this supported?

x = pd.Series([0, 1, 2])
x.iloc[[True, False, True]] = [1, 2]

Currently that raises with

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-aa3d14fe6683> in <module>()
      1 x = pd.Series([0, 1, 2])
----> 2 x.iloc[[True, False, True]] = [1, 2]

~/sandbox/pandas-ip/pandas/pandas/core/indexing.py in __setitem__(self, key, value)
    185             key = com._apply_if_callable(key, self.obj)
    186         indexer = self._get_setitem_indexer(key)
--> 187         self._setitem_with_indexer(indexer, value)
    188
    189     def _has_valid_type(self, k, axis):

~/sandbox/pandas-ip/pandas/pandas/core/indexing.py in _setitem_with_indexer(self, indexer, value)
    636             self.obj._consolidate_inplace()
    637             self.obj._data = self.obj._data.setitem(indexer=indexer,
--> 638                                                     value=value)
    639             self.obj._maybe_update_cacher(clear=True)
    640

~/sandbox/pandas-ip/pandas/pandas/core/internals.py in setitem(self, **kwargs)
   3646
   3647     def setitem(self, **kwargs):
-> 3648         return self.apply('setitem', **kwargs)
   3649
   3650     def putmask(self, **kwargs):

~/sandbox/pandas-ip/pandas/pandas/core/internals.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
   3534
   3535             kwargs['mgr'] = self
-> 3536             applied = getattr(b, f)(**kwargs)
   3537             result_blocks = _extend_blocks(applied, result_blocks)
   3538

~/sandbox/pandas-ip/pandas/pandas/core/internals.py in setitem(self, indexer, value, mgr)
    883                         indexer.dtype == np.bool_ and
    884                         len(indexer[indexer]) == len(value)):
--> 885                     raise ValueError("cannot set using a list-like indexer "
    886                                      "with a different length than the value")
    887

ValueError: cannot set using a list-like indexer with a different length than the value

But __setitem__ and .loc support setting via that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions