-
-
Notifications
You must be signed in to change notification settings - Fork 17.4k
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
BUG: pd.DataFrame.set_index() detects "label" input instead of "iterator" when keys argument is a list #58631
Comments
Hi @lucasmouragomes, I was able to reproduce the example: import pandas as pd
df = 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 In my view, if you want to set a new index for the DataFrame, you could try: pd.DataFrame({'val':[0, 1, 2]}, index=['a', 'b', 'c']) This might work as expected. |
I can fully understand the documentation and see why you have responded like this, but wouldn't it be nicer to just rename the indexes by default instead of throwing an error? |
It's wierd to see that both of the following works:
As per the documentation, it need a |
Would someone else be interested in creating a bounty for the solving of the bounty? =>https://www.hydraoss.io/ Would be happy to contribute to it. |
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: