Skip to content

BUG: row filtering by index slicing does not always work #40663

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
2 tasks done
yohplala opened this issue Mar 28, 2021 · 6 comments
Closed
2 tasks done

BUG: row filtering by index slicing does not always work #40663

yohplala opened this issue Mar 28, 2021 · 6 comments
Labels
Copy / view semantics Duplicate Report Duplicate issue or pull request Indexing Related to indexing on series/frames, not to indexes themselves Warnings Warnings that appear or should be added to pandas

Comments

@yohplala
Copy link

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.


Code Sample, a copy-pastable example

import pandas as pd
df = pd.DataFrame(columns=['a', 'b'], index=range(6))
# Slicings that work
df[::2] = 1
df['a'][::2] = 'a'
df[1::2] = 2
df['a'][1::2] = 'c'
# Slicing that does not work
df[['a','b']][1::2] = 'aha'

Problem description

The last slicing does not work (Dataframe is not modified), and no error message is issued.
I could check that it works with pd.IndexSlice.
At least an error message should be raised.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f2c8480
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.8.0-48-generic
Version : #54~20.04.1-Ubuntu SMP Sat Mar 20 13:40:25 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : fr_FR.UTF-8
LOCALE : fr_FR.UTF-8

pandas : 1.2.3
numpy : 1.18.5
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 52.0.0.post20210125
Cython : 0.29.22
pytest : 6.2.2
hypothesis : None
sphinx : 3.5.3
blosc : None
feather : None
xlsxwriter : 1.3.7
lxml.etree : 4.6.3
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.21.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : 1.3.2
fsspec : 0.8.7
fastparquet : 0.5.0
gcsfs : None
matplotlib : 3.3.4
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : 2.0.0
pyxlsb : None
s3fs : None
scipy : 1.6.2
sqlalchemy : 1.3.23
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : 1.3.0
numba : 0.51.2

@yohplala yohplala added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 28, 2021
@mzeitlin11
Copy link
Member

Thanks for the report @yohplala. I don't think this is row filtering specific, but rather a case of chained assignment (see https://pandas.pydata.org/docs/user_guide/indexing.html#indexing-view-versus-copy). Column setting also fails in this case, eg: df[['a','b']][1::2] = 5 also does not modify df. The right way to slice with your final statement would be

df.loc[1::2, ['a', 'b']] = 'aha'

@mzeitlin11 mzeitlin11 added Indexing Related to indexing on series/frames, not to indexes themselves Warnings Warnings that appear or should be added to pandas and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 28, 2021
@yohplala
Copy link
Author

Thanks @mzeitlin11
The .loc does the trick indeed, a lot of thanks.
Bests
(leaving the ticket open - I think issuing an error in above mentionned case would be the answer)

@attack68
Copy link
Contributor

I think this is already covered here: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#indexing-view-versus-copy

@attack68 attack68 added the Closing Candidate May be closeable, needs more eyeballs label Mar 29, 2021
@phofl
Copy link
Member

phofl commented Mar 29, 2021

This should probably raise a SettingWithCopyWarning?

@jreback
Copy link
Contributor

jreback commented Mar 29, 2021

yeah ideally this would show the warning

@phofl phofl added Copy / view semantics and removed Closing Candidate May be closeable, needs more eyeballs labels Mar 29, 2021
@mzeitlin11
Copy link
Member

Will close in favor of #9767

@mzeitlin11 mzeitlin11 added the Duplicate Report Duplicate issue or pull request label Apr 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Copy / view semantics Duplicate Report Duplicate issue or pull request Indexing Related to indexing on series/frames, not to indexes themselves Warnings Warnings that appear or should be added to pandas
Projects
None yet
Development

No branches or pull requests

5 participants