BUG: inplace behavior is inconsist for fillna
See original GitHub issuePandas 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+
Issue Analytics
- State:
- Created a year ago
- Comments:11 (11 by maintainers)
Top Results From Across the Web
fillna() method's behavior in Pandas -- does not obey to ...
fillna (like many other methods in pandas) isn't inplace by default. You need to either pass inplace=True or reassign its return value.
Read more >Working with missing data — pandas 1.5.2 documentation
This behavior is consistent with R, for example: ... fillna() can “fill in” NA values with non-NA data in a couple of ways,...
Read more >Using Pandas and Python to Explore Your Dataset
Missing Values; Invalid Values; Inconsistent Values ... compare Pandas and Python data structures, you'll see that this behavior makes Pandas much faster!
Read more >Why You Should Probably Never Use pandas inplace=True
Inplace is a parameter accepted by a number of pandas methods which affects the behaviour of how the method runs. Some examples of...
Read more >The IamDataFrame class — pyam 1.6.0 documentation
This is intended behaviour and consistent with pandas but may be confusing for ... If False and other is an IamDataFrame, raise an...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
opened #47449 for
eval
fix for update included in https://github.com/pandas-dev/pandas/pull/47327