Skip to content

Interpreting passed slicers in using .loc(axis=1) |#AssertionError: Start slice bound is non-scalar #10586

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
dickster77 opened this issue Jul 15, 2015 · 1 comment · Fixed by #30646
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@dickster77
Copy link

From SO

The documentation suggests:

You can also specify the axis argument to .loc to interpret the passed slicers on a single axis.
see http://pandas.pydata.org/pandas-docs/stable/advanced.html#using-slicers

However I get an error trying to slice along the column index.

    import pandas as pd
    import numpy as np

    cols= [(yr,m) for yr in [2014,2015] for m in [7,8,9,10]]
    df = pd.DataFrame(np.random.randint(1,100,(10,8)),index=tuple('ABCDEFGHIJ'))
    df.columns =pd.MultiIndex.from_tuples(cols)

    print df.head()

      2014             2015            
        7   8   9   10   7   8   9   10
    A   68  51   6  48   24   3   4  85
    B   79  75  68  62   19  40  63  45
    C   60  15  32  32   37  95  56  38
    D    4  54  81  50   13  64  65  13
    E   78  21  84   1   83  18  39  57


    #This does not work as expected
    print df.loc(axis=1)[(2014,9):(2015,8)]

    #AssertionError: Start slice bound is non-scalar

    #but an arbitrary transpose and changing axis works!
    df = df.T
    print df.loc(axis=0)[(2014,9):(2015,8)]

              A   B   C   D   E   F   G   H   I   J
    2014 9    6  68  32  81  84  60  83  39  94  93
         10  48  62  32  50   1  84  18  14  92  33
    2015 7   24  19  37  13  83  69  31  91  69  90
         8    3  40  95  64  18   8  32  93  16  25

    #So I could always assign the slice and re-transpose. 
    #That though feels like a hack and the axis=1 setting should have worked.

        df = df.loc(axis=0)[(2014,9):(2015,8)]

    df = df.T


    print df

      2014     2015    
        9   10   7   8 
    A   64  98   99  87
    B   43  36   22  84
    C   32  78   86  66
    D   67   8   34  73
    E   83  54   96  33
    F   18  83   36  71
    G   13  25   76   8
    H   69   4   99  84
    I    3  52   50  62
    J   67  60    9  49

The canonical way to do this is:

In [6]: df.loc()[:,(2014,9):(2015,8)]
Out[6]: 
  2014     2015    
    9   10   7   8 
A   26   2   44  69
B   41   7    5   1
C    8  27   23  22
D   54  72   81  93
E   18  23   54   7
F   11  81   37  83
G   60  38   59  29
H    3  95   89  96
I    6   9   77   9
J   90  92   10  32

So prob a bug

@jreback jreback added Bug Indexing Related to indexing on series/frames, not to indexes themselves labels Jul 15, 2015
@jreback jreback added this to the Next Major Release milestone Jul 15, 2015
@mroeschke
Copy link
Member

This works on master. Could use a test

In [88]: df.loc(axis=1)[(2014,9):(2015,8)]
Out[88]:
  2014     2015
    9   10   7   8
A   65  14   26  75
B   39  69   54  49
C   90  73   11   9
D   32  31   15  14
E   52  88   46  14
F   93  91    1  28
G   36  69   43  88
H   15  68   38  12
I   53  30   20  26
J   51   5    6  96

In [82]: pd.__version__
Out[82]: '0.26.0.dev0+490.g9cfb8b55b'

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug Indexing Related to indexing on series/frames, not to indexes themselves labels Oct 6, 2019
@simonjayhawkins simonjayhawkins modified the milestones: Contributions Welcome, 1.0 Jan 3, 2020
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