Skip to content

ENH: Inconsistency when string refers both to index level and column label #34791

@ChrisStuff

Description

@ChrisStuff

While groupby throws a ValueError when the string parameter refers both to an index level and column name, query gives the column precedence in such a case:

>>> df = pd.DataFrame({'a':[1,2,3,4,5], 'b':[3,3,3,3,3]})
>>> df.index.name = 'a'
>>> df.query('a < b')
   a  b
a
0  1  3
1  2  3
>>> df.groupby('a') 
Traceback (most recent call last):
ValueError: 'a' is both an index level and a column label, which is ambiguous.

I am sure there are many other places where a string can refer to both an index level and column name. #27652 and #8162 are somehow linked.

Even though those issues might not be solved, it would be desirable to handle the above ambiguity consistently in places where both index levels and column labels can be referenced by a string argument.

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions