Buggy negative slicing with ix #2600

Closed
wesm opened this Issue Dec 27, 2012 · 6 comments

Comments

Projects
None yet
4 participants
Owner

wesm commented Dec 27, 2012

http://stackoverflow.com/questions/14035817/slicing-pandas-dataframe-with-negative-index-with-ix-method

    In [1]: df = pd.DataFrame(np.random.randn(10, 4))

    In [2]: df
    Out[2]: 
              0         1         2         3
    0 -3.100926 -0.580586 -1.216032  0.425951
    1 -0.264271 -1.091915 -0.602675  0.099971
    2 -0.846290  1.363663 -0.382874  0.065783
    3 -0.099879 -0.679027 -0.708940  0.138728
    4 -0.302597  0.753350 -0.112674 -1.253316
    5 -0.213237 -0.467802  0.037350  0.369167
    6  0.754915 -0.569134 -0.297824 -0.600527
    7  0.644742  0.038862  0.216869  0.294149
    8  0.101684  0.784329  0.218221  0.965897
    9 -1.482837 -1.325625  1.008795 -0.150439

    In [3]: df.ix[-2:]
    Out[3]: 
              0         1         2         3
    0 -3.100926 -0.580586 -1.216032  0.425951
    1 -0.264271 -1.091915 -0.602675  0.099971
    2 -0.846290  1.363663 -0.382874  0.065783
    3 -0.099879 -0.679027 -0.708940  0.138728
    4 -0.302597  0.753350 -0.112674 -1.253316
    5 -0.213237 -0.467802  0.037350  0.369167
    6  0.754915 -0.569134 -0.297824 -0.600527
    7  0.644742  0.038862  0.216869  0.294149
    8  0.101684  0.784329  0.218221  0.965897
    9 -1.482837 -1.325625  1.008795 -0.150439
Owner

wesm commented Dec 28, 2012

It's a binary-search slicing issue on monotonic indexes. Not sure if will fix. deferring to 0.11 after discussion with @changhiskhan

Contributor

jreback commented Sep 20, 2013

since ix transforms neg indicies to pos this won't work..hmm

but the following do

In [4]: df.iloc[-2:]
Out[4]: 
          0         1         2         3
8 -0.128100  0.033696  0.391006  0.492019
9  0.719473  0.079920  1.710083 -0.291418

In [5]: df[-2:]
Out[5]: 
          0         1         2         3
8 -0.128100  0.033696  0.391006  0.492019
9  0.719473  0.079920  1.710083 -0.291418

@jreback jreback modified the milestone: 0.15.0, 0.14.0 Feb 18, 2014

@jreback jreback modified the milestone: 0.16.0, 0.17.0 Jan 26, 2015

I just ran into this bug. Are you still planning to fix it?

I don't think we will fix this.
As it is actually not a bug. ix does label-based indexing when having an integer index, so the result you see is correct. If you want integer positional indexing, you can use df.iloc[-2:]

@jreback agree to close?

Contributor

jreback commented Apr 8, 2016

@jorisvandenbossche yep agreed.

jreback closed this Apr 8, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment