Skip to content

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

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

Closed
jorisvandenbossche opened this issue Jul 14, 2016 · 2 comments · Fixed by #29519
Closed

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

jorisvandenbossche opened this issue Jul 14, 2016 · 2 comments · Fixed by #29519
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@jorisvandenbossche
Copy link
Member

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]'
@jorisvandenbossche jorisvandenbossche added the Indexing Related to indexing on series/frames, not to indexes themselves label Jul 14, 2016
@mroeschke
Copy link
Member

This looks to be fixed on master. Could use a test.

In [93]: df.loc(axis=1)['a1':'a2']
Out[93]:
   a1          a2
   b1  b2  b3  b1  b2  b3
0   0   1   2   3   4   5
1   9  10  11  12  13  14
2  18  19  20  21  22  23

In [94]: df.loc(axis=1)['a1':'a2']
Out[94]:
   a1          a2
   b1  b2  b3  b1  b2  b3
0   0   1   2   3   4   5
1   9  10  11  12  13  14
2  18  19  20  21  22  23

In [95]: In [27]: df.loc(axis=1)['a1']
    ...:
Out[95]:
   b1  b2  b3
0   0   1   2
1   9  10  11
2  18  19  20

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

In [97]: In [30]: df2.loc(axis=1)['a']
    ...:
Out[97]:
0    0
1    3
2    6
Name: a, dtype: int64

In [98]: pd.__version__
Out[98]: '0.26.0.dev0+652.g30362ed82'

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Indexing Related to indexing on series/frames, not to indexes themselves labels Oct 23, 2019
@ryankarlos
Copy link
Contributor

Ill have a go at this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants