There seems to be an issue with pandas replace() option when doing replacement on only a few columns:
# Example dataframe:
pd.DataFrame(data={"x":[1,2,3,4,5], "y":[2,4,1,2,4], "z":["no", "yes", "no", "no", "no"], "t":["a", "b", "c", "d", "d"]})
# Try to replace the 2s inplace:
a.loc[:, ["x", "y"]].replace(2,-9999, inplace=True)
a is still:
Out[32]:
x y z t
0 1 2 no a
1 2 4 yes b
2 3 1 no c
3 4 2 no d
4 5 4 no d
Note that I do not get a settingWithCopy warning - also, I am using .loc as recommended. Since I use inplace=True, I would have expected the dataframe to change. Am I doing something wrong, or is this a bug to report on github?
I am using pandas version 0.23.0.
a.loc[:, ["x", "y"]]uses.loc.__getitem__. When using__getitem__, the returned object might be a copy. Here,a.loc[:, ["x", "y"]]returns a copy and that copied structured is in fact modified inplace but since you didn't assign it to anything you cannot see the change. The original df remains unchanged as well.