Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
phofl opened this issue Oct 31, 2020 · 0 comments · Fixed by #37672
Closed
3 tasks done

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

phofl opened this issue Oct 31, 2020 · 0 comments · Fixed by #37672
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@phofl
Copy link
Member

phofl commented Oct 31, 2020

  • 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

@phofl phofl added Bug Needs Triage Issue that has not been reviewed by a pandas team member Indexing Related to indexing on series/frames, not to indexes themselves labels Oct 31, 2020
@rhshadrach rhshadrach removed the Needs Triage Issue that has not been reviewed by a pandas team member label Nov 6, 2020
@rhshadrach rhshadrach added this to the Contributions Welcome milestone Nov 6, 2020
@jreback jreback modified the milestones: Contributions Welcome, 1.3 May 24, 2021
@simonjayhawkins simonjayhawkins removed this from the 1.3 milestone Jun 11, 2021
@mroeschke mroeschke added the Dtype Conversions Unexpected or buggy dtype conversions label Aug 14, 2021
@jreback jreback added this to the 1.4 milestone Dec 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants