-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Using tuples to specify single columns in .loc #54464
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
Comments
Non single item nested Lists and Tuples are unpacked and matched individually, which is on purpose. Imagine the following example in which you want to specify which information to return. Both calls work but return different dataframes.
Output 1:
Output 2:
|
I totally agree with your point, but why would we unpack the tuple as columns while we can do that with the list based approach. Doesn't it create a confusion here? Consider there is a big project which used** .loc** function to slice a part of data in which the column name is tuple based, and it throws error as I mentioned in the issue. Do you think it's easy to debug the code with the error and exception raises. Either the code should raise a appropriate exception or it should enforce a rule to restrict a user from using tuple based column names. I am open to discuss a logical and valid points, and correct me if there is any other perspectives. And I don't see a documentation which demands this squared bracket enclosed format while I want to fetch a single column, where the column name is a tuple. |
@SudhanAnnamalai - can you give this issue a descriptive title? |
Sure |
pandas does at times treat tuples and lists differently, and at first glance this seems to be a case where perhaps we should do so here. However it might be more complicated - for example, tuples are also used to indicate levels of a MultiIndex - so there is a number of cases to consider here. cc @pandas-dev/pandas-core for any thoughts. |
There are many cases to consider: see this thread (which may be a bit out of date in operation terms but not in function): #39424 (comment) That specific link is a pathological example of multindexes where tuples form part of an element in a multiindex. Allowing tuples both as element labels and as containers for multinindex element labels in my opinion creates a plethora of problems without really giving a lot to users. Users could probably redefine their element tuples as something else. @jbrockmendel proposed something else that didnt get much traction and didnt stir a discussion, unfortunately. #42349 |
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
#Error line
df.loc[:, (6, "two")] #KeyError: "None of [Index([6, 'two'], dtype='object')] are in the [columns]"
df.loc["a", (6, "two")] # Assertion Error : assert retval.ndim == self.ndim
Expected Behavior
However, the code below works fine(as expected)
Either, the syntax should work regardless of the datatype of the column_name, like in our example : tuple, or else the module should raise a exception or error with a clear information saying that the tuple based column name has to be changed.
I have also checked the above scenario, with other methods.
Providing a single or multiple tuple based columne(s) results a data frame without error as in the above (3) (4)
That could recommended along with the error statement, This could save a lot of time to the end user
Installed Versions
INSTALLED VERSIONS
commit : 2e218d1
python : 3.10.1.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252
pandas : 1.5.3
numpy : 1.24.0
pytz : 2022.7.1
dateutil : 2.8.2
setuptools : 58.1.0
pip : 21.2.4
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.2
html5lib : None
pymysql : None
psycopg2 : 2.9.6
jinja2 : 3.1.2
IPython : 8.12.0
pandas_datareader: None
bs4 : 4.12.2
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.0
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : 2022.7
The text was updated successfully, but these errors were encountered: