BUG: Setting values with ILOC and list like indexers raises · Issue #36741 · pandas-dev/pandas (original) (raw)


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd

df = pd.DataFrame({"flag": ["x", "y"], "value": [1, 2]}) df.iloc[[True, False], 1] = df.iloc[[True, False], 1] * 2 print(df)

Problem description

This worked on 1.0.5 and returned

On master this raises:

Traceback (most recent call last):
  File "/home/developer/.config/JetBrains/PyCharm2020.2/scratches/scratch_5.py", line 221, in <module>
    df.iloc[[True, False], 1] = x
  File "/home/developer/PycharmProjects/pandas/pandas/core/indexing.py", line 681, in __setitem__
    iloc._setitem_with_indexer(indexer, value)
  File "/home/developer/PycharmProjects/pandas/pandas/core/indexing.py", line 1756, in _setitem_with_indexer
    self._setitem_single_column(ilocs[0], value, pi)
  File "/home/developer/PycharmProjects/pandas/pandas/core/indexing.py", line 1800, in _setitem_single_column
    ser._mgr = ser._mgr.setitem(indexer=pi, value=value)
  File "/home/developer/PycharmProjects/pandas/pandas/core/internals/managers.py", line 532, in setitem
    return self.apply("setitem", indexer=indexer, value=value)
  File "/home/developer/PycharmProjects/pandas/pandas/core/internals/managers.py", line 397, in apply
    applied = getattr(b, f)(**kwargs)
  File "/home/developer/PycharmProjects/pandas/pandas/core/internals/blocks.py", line 923, in setitem
    check_setitem_lengths(indexer, value, values)
  File "/home/developer/PycharmProjects/pandas/pandas/core/indexers.py", line 158, in check_setitem_lengths
    raise ValueError(
ValueError: cannot set using a list-like indexer with a different length than the value

Process finished with exit code 1

Was this change of behavior intended?

Expected Output

I would expect, that this would work and returns the results from 1.0.5

Output of pd.show_versions()

master