Skip to content

BUG: SettingWithCopyWarning #46869

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
3 tasks done
iliya-malecki opened this issue Apr 25, 2022 · 3 comments
Closed
3 tasks done

BUG: SettingWithCopyWarning #46869

iliya-malecki opened this issue Apr 25, 2022 · 3 comments
Labels
Closing Candidate May be closeable, needs more eyeballs Usage Question Warnings Warnings that appear or should be added to pandas

Comments

@iliya-malecki
Copy link

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

A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  available_data['loading'] = 1

Issue Description

Sorry but for the life of me i cant create a reproducible example. It is just a dataframe that was created using maindf.query('some_time > @todays_datetime')
available_data.values.base is maindf.values.base # False

Expected Behavior

not have the warning

Installed Versions

INSTALLED VERSIONS

commit : 5f648bf
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-1051-azure
Version : #53~18.04.1-Ubuntu SMP Fri Jun 18 22:32:58 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.2
numpy : 1.21.2
pytz : 2021.1
dateutil : 2.8.2
pip : 21.2.2
setuptools : 52.0.0.post20210125
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.27.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.3
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 3.0.0
pyxlsb : None
s3fs : None
scipy : 1.7.1
sqlalchemy : 1.4.35
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@iliya-malecki iliya-malecki added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 25, 2022
@rhshadrach
Copy link
Member

rhshadrach commented Apr 25, 2022

Here is what I believe is happening:

df = pd.DataFrame({'a': [1, 2, 3]})
df2 = df.query('a > 1')
df2['a'] = 5

The method .query may return a view or copy; it will certainly return a view when the condition always holds (e.g. replace a > 1 with a > 0 in the example above). When it makes a copy and then you are setting new values on that copy, pandas is generating this warning in case you meant to modify the original (df in the example above) instead. To silence the warning, you can copy after doing the query: df2 = df.query('a > 1').copy().

Does this resolve the issue?

@rhshadrach rhshadrach added Usage Question Warnings Warnings that appear or should be added to pandas Closing Candidate May be closeable, needs more eyeballs and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 25, 2022
@iliya-malecki
Copy link
Author

@rhshadrach thank you, .copy() helped!
I have a couple of questions now:

  1. Why is it a valid idea to return a view from .query()? What is the intention here? To me, it looks like a dangerous feature to have - conditionally ruining an original dataframe. I obviously dont understand what is happening under the hood, so i can only think about usage.
  2. Is it a valid way to check whether a dataframe is a view or copy? available_data.values.base is maindf.values.base

@mroeschke
Copy link
Member

  1. query tries to follow numpy indexing semantics which can also return a view or copy depending on the indexing operation
  2. You can use np.shares_memory to also see if the underlying data shares memory. https://numpy.org/doc/stable/reference/generated/numpy.shares_memory.html

Closing as this is not a bug rather a usage question

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Closing Candidate May be closeable, needs more eyeballs Usage Question Warnings Warnings that appear or should be added to pandas
Projects
None yet
Development

No branches or pull requests

3 participants