Skip to content

BUG: Boolean indexed assignment #4192

Closed
@SleepingPills

Description

@SleepingPills

Take the simple example below:

s = pd.Series([1.0, 2.0], dtype=float)
s[[True, False]] = np.array([2.0])

The expectation would be that the first item in the series will be set to 2. Unfortunately due to a bug in broadcasting, pandas will set the first item in the series to 2*len(s). The culprit appears to be the below code (series.py:757 - where() method definition):

if len(other) == 1:
    other = np.array(other[0]*len(ser))

other in this case is the length 1 array we need to broadcast. I believe the code made the assumption that other will be perhaps a list of lists? As it stands, this code will take the single element out of the array, multiply it by the length of the series and then carry out the update.

I propose that we fix this by using np.repeat which does the correct thing (pending testing).

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