Skip to content

BUG: Loc changes dtype when condition is completly False #37550

Closed
@phofl

Description

@phofl
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


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

Code Sample, a copy-pastable example

df = pd.DataFrame({
    "a": ["a"],
    "b": [1],
    "c": [1]
})
df.loc[[False], ["b"]] = 10 - df["c"]
print(df)

Thus converts b to float and returns

   a    b  c
0  a  1.0  1

Problem description

[this should explain why the current behaviour is a problem and why the expected output is a better solution]

Expected Output

Would expect that this returns an integer.

   a    b  c
0  a  1  1

Interestingly, this is returned if we drop the column a.

df = pd.DataFrame({
    "b": [1],
    "c": [1]
})
df.loc[[False], ["b"]] = 10 - df["c"]
print(df)

or if we are not using df["c"] and an integer instead.

df = pd.DataFrame({
    "a": ["a"],
    "b": [1],
    "c": [1]
})
df.loc[[False], ["b"]] = 10 - 1
print(df)

Both return 1 instead of 1.0 for column b

Edit: The condition [False] is just a simplification. This also happens if we use a condition, which evaluates sometimes to completly False based on the input data.

Output of pd.show_versions()

master

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDtype ConversionsUnexpected or buggy dtype conversionsIndexingRelated 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