Pandas version checks
-
[x] I have checked that this issue has not already been reported.
-
[x] 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
import pandas as pd
foo = pd.DataFrame({'a': [1, 2, 3], 'b': ['foo', 'foo', 'bar']})
foo = pd.concat([foo], keys=['foo'], axis=1)
foo.drop(index='b', level=1, axis=1)
Issue Description
When drop is called, an AssertionError is raised AssertionError: axis must be a MultiIndex
On inspection of the dataframe, the columns are a MultiIndex
Expected Behavior
Drop should not raise an incorrect AssertionError
Installed Versions
Also tested in a clean 3.13.5 environment:
Comment From: camriddell
Your function call specification is incorrect here, the arguments index
and columns
are supposed to be mutually exclusive from the use of labels
& axis
(confusing I know).
foo.drop(index='b', level=1, axis=1)
By specifying index='b'
pandas is only examining the row index for a MultiIndex (as hinted by level=1
), and ultimately ignores the axis=1
parameter.
So instead, you should use
>>> foo.drop(columns='b', level=1)
foo
a
0 1
1 2
2 3
or
>>> foo.drop('b', level=1, axis=1)
foo
a
0 1
1 2
2 3
Comment From: pratt-fds
Would it make sense to raise an exception related to invalid options selected, when passing index/columns and axis?
To me, index is a bit of a weird name for the index on the rows, as there can also be an index on the columns - It's easy to forget that index in this regard specifically means the row index
Comment From: rhshadrach
+1 on raising when both index
and axis
or both columns
and axis
are specified. PRs to fix are welcome!
Comment From: khemkaran10
take @rhshadrach can I work on this?
Comment From: camriddell
take @rhshadrach can I work on this?
Any chance this can be fixed for all functions/methods that allow both func(index=…, columns=…)
and func(arg, axis=…)
as mutually exclusive groups?
A few off the top of my head - DataFrame.drop - DataFrame.reindex - DataFrame.rename_axis - DataFrame.rename
(This last one may be scope creep)
- DataFrame.set_axis only supports the func(arg, axis=…)
signature. Could a func(index=…, columns=…)
be added here?
Comment From: khemkaran10
Hi @camriddell I added a basic check for this scenario. If you think I’m in the right direction, I’ll make the changes in the other functions as well. please let me know.
Comment From: khemkaran10
This check is already there in DataFrame.reindex DataFrame.rename