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
Method pd.DataFrame.set_index() raises the following exception:
KeyError: "None of ['a', 'b', 'c'] are in the columns"
The documentation states that the keys parameter can be either a single column key, a single array of the same length as the calling DataFrame, or a list containing an arbitrary combination of column keys and arrays. Here, “array” encompasses Series, Index, np.ndarray, and instances of Iterator.
The list argument in the example provided is an iterator of the same length as the dataframe, so the method should detect the second option. Instead, it seems to behave as if I inserted a combination of column keys and raises an exception.
Expected Behavior
Expected behaviour should be the same as the following:
importpandasaspddf=pd.DataFrame({'val':[0, 1, 2]})
df.set_index(['a', 'b', 'c'])
# KeyError: "None of ['a', 'b', 'c'] are in the columns"df=pd.DataFrame({'val':[0, 1, 2]})
df.set_index(pd.Series(['a', 'b', 'c']))
# val# a 0# b 1# c 2
However, do you notice that the documentation states 'Set the DataFrame index using existing columns.'? I think the KeyError you mentioned is correct.
In my view, if you want to set a new index for the DataFrame, you could try:
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
Issue Description
Method pd.DataFrame.set_index() raises the following exception:
KeyError: "None of ['a', 'b', 'c'] are in the columns"
The documentation states that the keys parameter can be either a single column key, a single array of the same length as the calling DataFrame, or a list containing an arbitrary combination of column keys and arrays. Here, “array” encompasses Series, Index, np.ndarray, and instances of Iterator.
The list argument in the example provided is an iterator of the same length as the dataframe, so the method should detect the second option. Instead, it seems to behave as if I inserted a combination of column keys and raises an exception.
Expected Behavior
Expected behaviour should be the same as the following:
Setting the dataframe index as the list given.
Installed Versions
INSTALLED VERSIONS
commit : d9cdd2e
python : 3.12.3.final.0
python-bits : 64
OS : Windows
OS-release : 11
Version : 10.0.25967
machine : AMD64
processor : AMD64 Family 23 Model 113 Stepping 0, AuthenticAMD
byteorder : little
LC_ALL : None
LANG : None
LOCALE : pt_BR.cp1252
pandas : 2.2.2
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0
setuptools : 69.5.1
pip : 24.0
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.1.3
IPython : 8.22.2
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.4
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.13.0
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : 0.22.0
tzdata : 2024.1
qtpy : 2.4.1
pyqt5 : None
None
The text was updated successfully, but these errors were encountered: