Skip to content

DataFrame.loc(axis=1)[..] not working for single-level indexer #13655

Closed
@jorisvandenbossche

Description

@jorisvandenbossche

When doing .loc(axis=1)[...], this only works when indexing into multiple levels. Here an example with a MultiIndex column:

In [23]: df = pd.DataFrame(np.arange(27).reshape(3, 9), columns=pd.MultiIndex.from_product([['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']]))

In [24]: df
Out[24]:
   a1          a2          a3
   b1  b2  b3  b1  b2  b3  b1  b2  b3
0   0   1   2   3   4   5   6   7   8
1   9  10  11  12  13  14  15  16  17
2  18  19  20  21  22  23  24  25  26

In [25]: df.loc(axis=1)['a1':'a2', ['b1', 'b2']]
Out[25]:
   a1      a2
   b1  b2  b1  b2
0   0   1   3   4
1   9  10  12  13
2  18  19  21  22

Given the above, I would expect leaving out the ['b1', 'b2'] to work as well, but in this case it looks like the axis=1 is ignored, which is a bit surprising IMO:

In [26]: df.loc(axis=1)['a1':'a2']
Out[26]:
Empty DataFrame
Columns: [(a1, b1), (a1, b2), (a1, b3), (a2, b1), (a2, b2), (a2, b3), (a3, b1),
(a3, b2), (a3, b3)]
Index: []

In [27]: df.loc(axis=1)['a1']
...
KeyError: 'the label [a1] is not in the [index]'

Also when having a simple frame, the same observation applies:

In [28]: df2 = pd.DataFrame(np.arange(9).reshape(3,3), columns=['a', 'b', 'c'])

In [29]: df2
Out[29]:
   a  b  c
0  0  1  2
1  3  4  5
2  6  7  8

In [30]: df2.loc(axis=1)['a']
...
KeyError: 'the label [a] is not in the [index]'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions