Skip to content

QST: Why is indexing with a reversed list possible but not assigning a new value? #39614

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
1 task
aauss opened this issue Feb 5, 2021 · 3 comments · Fixed by #39623
Closed
1 task

QST: Why is indexing with a reversed list possible but not assigning a new value? #39614

aauss opened this issue Feb 5, 2021 · 3 comments · Fixed by #39623
Assignees
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@aauss
Copy link

aauss commented Feb 5, 2021

  • [ x] I have searched the [pandas] tag on StackOverflow for similar questions.

  • I have asked my usage related question on StackOverflow.


Question about pandas

I noticed that indexing with a reversed list works but not assigning. The error message is not helpful. Is this intended?

import pandas as pd

s = pd.Series([1, 2, 3])
mask = [True, False, False]
s.loc[reversed(mask)]
>>> 2    3
>>> dtype: int64

# But assigning does not work
s.loc[reversed(mask)] = 5
>>> ---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-1-912755dd04b5> in <module>
      7 
      8 # But assigning does not work
----> 9 s.loc[reversed(mask)] = 5

~/miniconda3/envs/who/lib/python3.9/site-packages/pandas/core/indexing.py in __setitem__(self, key, value)
    686         else:
    687             key = com.apply_if_callable(key, self.obj)
--> 688         indexer = self._get_setitem_indexer(key)
    689         self._has_valid_setitem_indexer(key)
    690 

~/miniconda3/envs/who/lib/python3.9/site-packages/pandas/core/indexing.py in _get_setitem_indexer(self, key)
    634 
    635         try:
--> 636             return self._convert_to_indexer(key, axis=0, is_setter=True)
    637         except TypeError as e:
    638 

~/miniconda3/envs/who/lib/python3.9/site-packages/pandas/core/indexing.py in _convert_to_indexer(self, key, axis, is_setter)
   1210             else:
   1211                 # When setting, missing keys are not allowed, even with .loc:
-> 1212                 return self._get_listlike_indexer(key, axis, raise_missing=True)[1]
   1213         else:
   1214             try:

~/miniconda3/envs/who/lib/python3.9/site-packages/pandas/core/indexing.py in _get_listlike_indexer(self, key, axis, raise_missing)
   1264             keyarr, indexer, new_indexer = ax._reindex_non_unique(keyarr)
   1265 
-> 1266         self._validate_read_indexer(keyarr, indexer, axis, raise_missing=raise_missing)
   1267         return keyarr, indexer
   1268 

~/miniconda3/envs/who/lib/python3.9/site-packages/pandas/core/indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
   1306             if missing == len(indexer):
   1307                 axis_name = self.obj._get_axis_name(axis)
-> 1308                 raise KeyError(f"None of [{key}] are in the [{axis_name}]")
   1309 
   1310             ax = self.obj._get_axis(axis)

KeyError: "None of [Index([False, False, True], dtype='object')] are in the [index]"

pd.show_version() outputs this:
INSTALLED VERSIONS

commit : 9d598a5
python : 3.9.1.final.0
python-bits : 64
OS : Linux
OS-release : 4.19.128-microsoft-standard
Version : #1 SMP Tue Jun 23 12:58:10 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.1
numpy : 1.19.2
pytz : 2020.5
dateutil : 2.8.1
pip : 20.3.3
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 : 2.11.2
IPython : 7.19.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.2
numexpr : None
odfpy : None
openpyxl : 3.0.6
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.5.2
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@aauss aauss added Needs Triage Issue that has not been reviewed by a pandas team member Usage Question labels Feb 5, 2021
@MarcoGorelli
Copy link
Member

Hi @aauss - can you post the full traceback, as well as pd.show_versions() (as per the issue template)?

@aauss
Copy link
Author

aauss commented Feb 5, 2021

Hi @MarcoGorelli, I just updated my issue to contain the traceback and pd.show_version().

Apologies for missing that!

@phofl
Copy link
Member

phofl commented Feb 6, 2021

This is a bug, will make a PR to fix this tomorrow

@phofl phofl self-assigned this Feb 6, 2021
@phofl phofl added Indexing Related to indexing on series/frames, not to indexes themselves Bug and removed Needs Triage Issue that has not been reviewed by a pandas team member Usage Question labels Feb 6, 2021
@lithomas1 lithomas1 added this to the 1.3 milestone Feb 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants