Skip to content

Error when updating dataframe with empty filter #9596

Closed
@Sereger13

Description

@Sereger13
df = pd.DataFrame({'a': ['1', '2', '3'], 
               'b': ['11', '22', '33'], 
               'c': ['111', '222', '333']})
df.loc[df.b.isnull(), 'a'] = df.b

The filtering condition is always false (b is never null) and the above code produces this error:
Array is not broadcastable to correct shape.

If, however, I change the type for column 'c' - which is not even mentioned in the assignment expression (!) - from str to int,

df = pd.DataFrame({'a': ['1', '2', '3'], 
               'b': ['11', '22', '33'], 
               'c': [111, 222, 333]}) # Type changed to int
df.loc[df.b.isnull(), 'a'] = df.b

No errors are produced.

Would be nice to have consistent behaviour and not have any errors - could not find anything in the documentation saying that applying .loc on empty filter is considered illegal?

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugError ReportingIncorrect or improved errors from pandasIndexingRelated 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