Skip to content

BUG: inplace behavior is inconsist for fillna #47188

Closed
@Yikun

Description

@Yikun

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
import numpy as np
pdf = pd.DataFrame({"x": [np.nan, 2, 3, 4, np.nan, 6], "y": [np.nan, 2, 3, 4, np.nan, 6]})
pser = pdf.x
# Didn't make a copy write to existing  (it's unexpected behavior)
pdf.fillna(0, inplace=True)
>>> pser
0    0.0
1    2.0
2    3.0
3    4.0
4    0.0
5    6.0


import pandas as pd
import numpy as np
pdf = pd.DataFrame({"x": [np.nan, 2, 3, 4, np.nan, 6], "y": [np.nan, 2, 3, 4, np.nan, 6]})
pser = pdf.x
# Make a copy and doesn't write to existing (it's expected behavior)
>>> pdf.fillna({"x": -1, "y": -2}, inplace=True)
>>> pser
0    NaN
1    2.0
2    3.0
3    4.0
4    NaN
5    6.0
Name: x, dtype: float64

Issue Description

Looks like behavior is a little bit differet in current pandas.

Expected Behavior

Always make a copy and doesn't write to existing array for Do in-place operation in all functions with inplace (such like eval/update/fillna).

Installed Versions

1.4+

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions