You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
That is because res = df["a"] creates a reference to the "a" column, not a copy that you are allowed to modify. There is an easy fix.
deff(df):
res=df['a'].copy() # copy the columnis_two=df['a'] ==2res.loc[is_two] =Nonereturnres
To be honest, I'm not certain why it only raises an error/warning with the second dataframe. Maybe it has to do with a subtle threshold where fully numeric dataframes automatically copy columns on access? E.g. maybe df['a'] is the same as df['a'].copy() for numeric dataframes? That would be my assumption.
In any case, you can look at this post for a more detailed explanation about chain-expressions.
Hi @nmay231 ! Thanks for looking at this. I'm aware of the causes of the SettingWithCopyWarning/Error, its fixes, etc, but the bug that I am specifically referring to in this issue is that it is raised inconsistently!
My thinking is that if one is setting with a reference, that the error should be raised regardless of what other data is in the frame. Is this not the desired behavior?
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
(optional) I have confirmed this bug exists on the master branch of pandas.
Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.
Code Sample, a copy-pastable example
Problem description
The function above selects a column
a
, finds the position of values that are equal to two and sets those values to None, then returns thea
Series.When a dataframe only contains the
a
column, there is no problem. When a datetime column is introduced,SettingWithCopyError
is raised.Expected Output
Because the function
f
does not access any column besidesa
, I expect it to behave identically for both dataframes.Output of
pd.show_versions()
INSTALLED VERSIONS
commit : 9d598a5
python : 3.8.5.final.0
python-bits : 64
OS : Darwin
OS-release : 19.6.0
Version : Darwin Kernel Version 19.6.0: Tue Nov 10 00:10:30 PST 2020; root:xnu-6153.141.10~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.2.1
numpy : 1.19.4
pytz : 2020.4
dateutil : 2.8.1
pip : 20.3.3
setuptools : 47.1.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.8.6 (dt dec pq3 ext lo64)
jinja2 : 2.11.2
IPython : 7.19.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.5.4
sqlalchemy : None
tables : None
tabulate : None
xarray : 0.16.2
xlrd : None
xlwt : None
numba : None
The text was updated successfully, but these errors were encountered: