Skip to content

BUG: where method results in a different outputs with "object" and "string" dtypes #46366

Closed
@Leejung8763

Description

@Leejung8763

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

df = pd.DataFrame({'A':["1","","3"]}, dtype="object")
df.where(df!="", np.nan, inplace=True)

df = pd.DataFrame({'A':["1","","3"]}, dtype="string")
df.where(df!="", np.nan, inplace=True)

Issue Description

I am getting different results when using the where method with different data type which are "object" and "string".

Expected Behavior

First, consider the dataframe with "object" and inplace=True options.

Then, I got a result that I expected.

df = pd.DataFrame({'A':["1","","3"]}, dtype="object")
df.where(df!="", np.nan, inplace=True)
    a
0   1
1   NaN
2   3

But, when I changed the dtypes "object" to "string", I got different result as below.

df = pd.DataFrame({'A':["1","","3"]}, dtype="string")
df.where(df!="", np.nan, inplace=True)
    A
0   <NA>
1   
2   <NA>

Installed Versions

pandas version: 1.3.5 , numpy version: 1.19.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions