Skip to content

BUG: incompatible dtype when creating string column with loc #55025

Closed
@mortewle

Description

@mortewle

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
df = pd.DataFrame({"x": [1]})
df.loc[df["x"] == 1, "y"] = "1"

Issue Description

I get a FutureWarning when conditionally creating a new string column in loc.

It does not happen if the column is numeric, assumably because float64 is the default dtype.

The warning dissappears when setting the column to NA beforehand, but I assume this is not meant as a nessecary additional step for non-numeric columns only.

FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value '1' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
df.loc[df["x"] == 1, "y"] = "1"

Expected Behavior

No warning.

Installed Versions

2.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugWarningsWarnings that appear or should be added to pandas

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions