Skip to content

Inconsistent treatment of None with .where() and inplace #7939

Closed
@Wilfred

Description

@Wilfred

Calling .where() treats None differently depending on the value of inplace.

In [1]: import pandas

In [2]: s1 = pandas.Series(['a', 'b', 'c'])

In [3]: s1.where(s1 != 'a', None)
Out[3]: 
0    None
1       b
2       c
dtype: object

In [4]: s2 = pandas.Series(['a', 'b', 'c'])

In [5]: s2.where(s1 != 'a', None, inplace=True)

In [6]: s2
Out[6]: 
0    NaN # would have expected None here!
1      b
2      c
dtype: object

This causes surprising behaviour when setting values in a series to None.

In [1]: import pandas

In [2]: s3 = pandas.Series(['a', 'b', 'c'])

In [3]: s3[0] = None

In [4]: s3[s3 == 'b'] = None

In [5]: s3
Out[5]: 
0    None
1     NaN # Expected None here too!
2       c
dtype: object

Metadata

Metadata

Assignees

No one assigned

    Labels

    API DesignMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolate

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions