Skip to content
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

Open
2 of 3 tasks
lucasmouragomes opened this issue May 8, 2024 · 1 comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@lucasmouragomes
Copy link

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

pd.DataFrame({'val':[0, 1, 2]}).set_index(['a', 'b', 'c'])

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:

import pandas as pd

pd.DataFrame({'val':[0, 1, 2]}).set_index( pd.Series(['a', 'b', 'c']) )

Setting the dataframe index as the list given.

val
a 0
b 1
c 2

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

@lucasmouragomes lucasmouragomes added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 8, 2024
@luke396
Copy link
Contributor

luke396 commented May 16, 2024

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 KeyError you mentioned is correct.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

2 participants