Description
-
[ 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