Skip to content

fillna(value=[partial-multi-index]) raises "A value is trying to be set on a copy of a slice from a DataFrame" warning #45821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Ponteaus opened this issue Feb 4, 2022 · 3 comments
Labels
Duplicate Report Duplicate issue or pull request replace replace method

Comments

@Ponteaus
Copy link

Ponteaus commented Feb 4, 2022

from pandas import DataFrame
df = DataFrame({'a':[1,2,3],'b':[1,2,3],'c':[1,2,3]})
piv = df.pivot(columns='a',values=['b','c'])
piv.fillna(value={'b':0,'c':100})

This code produces the expected result, where all columns under the 'b' index are filled with 0s and all columns under the 'c' index are filled with 100s, but it unexpectedly raises a "/lib/python3.9/site-packages/pandas/core/frame.py:5176: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame" warning.

This warning is NOT raised if I fully specify the column index:

from pandas import DataFrame
df = DataFrame({'a':[1,2,3],'b':[1,2,3],'c':[1,2,3]})
piv = df.pivot(columns='a',values=['b','c'])
piv.fillna(value={('b',1):0,('b',2):0,('b',3):0,('c',1):100,('c',2):100,('c',3):100})

But this is too verbose. The first code segment is preferred

@Ponteaus Ponteaus changed the title fillna(value=[partial-multi-index) fillna(value=[partial-multi-index]) raises "A value is trying to be set on a copy of a slice from a DataFrame" warning Feb 4, 2022
@phofl
Copy link
Member

phofl commented Feb 6, 2022

Which Version of pandas are you using? Please Show your versions with pd.show_versions()

also please create your df without pivot

@phofl phofl added the Needs Info Clarification about behavior needed to assess issue label Feb 6, 2022
@phofl
Copy link
Member

phofl commented Feb 6, 2022

Duplicate of #43476

@phofl phofl marked this as a duplicate of #43476 Feb 6, 2022
@phofl phofl closed this as completed Feb 6, 2022
@phofl phofl added Duplicate Report Duplicate issue or pull request replace replace method and removed Needs Info Clarification about behavior needed to assess issue labels Feb 6, 2022
@phofl phofl added this to the No action milestone Feb 6, 2022
@Ponteaus
Copy link
Author

Ponteaus commented Feb 7, 2022

pandas version = 1.3.4

from pandas import DataFrame
from numpy import NaN
df = DataFrame({('b',1):[1,NaN], ('b',2):[NaN, 2], ('c',1):[1,NaN], ('c',2):[NaN, 2]})
df.fillna(value={'b':0,'c':100})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate Report Duplicate issue or pull request replace replace method
Projects
None yet
Development

No branches or pull requests

2 participants