-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
Closed
Labels
IndexingRelated to indexing on series/frames, not to indexes themselvesRelated to indexing on series/frames, not to indexes themselvesMultiIndex
Description
In [42]: from itertools import product
In [43]: import pandas as pd
In [44]: import numpy as np
In [45]: index = pd.MultiIndex.from_tuples([t for t in product([10, 20, 30], ['a', 'b'])])
In [46]: df = pd.DataFrame(np.random.randn(6, 6), index, index)
In [47]: df
Out[47]:
10 20 30
a b a b a b
10 a 0.077368 0.360018 0.649403 -0.221877 -1.527411 0.485647
b 0.890805 -2.142297 0.758411 -1.650710 0.041276 -0.040894
20 a -0.401678 0.481390 -1.080735 0.621861 1.410940 -1.106015
b -0.504422 -1.555415 -0.023859 0.211287 -0.321643 0.140895
30 a -0.118969 -0.432082 -0.888786 1.167191 -1.642356 -0.281661
b -0.580182 2.920769 -0.685617 1.327784 0.691514 -0.692361
Slicing ranges is consistent between both axis.
In [48]: df.ix[10:20, :]
Out[48]:
10 20 30
a b a b a b
10 a 0.077368 0.360018 0.649403 -0.221877 -1.527411 0.485647
b 0.890805 -2.142297 0.758411 -1.650710 0.041276 -0.040894
20 a -0.401678 0.481390 -1.080735 0.621861 1.410940 -1.106015
b -0.504422 -1.555415 -0.023859 0.211287 -0.321643 0.140895
In [49]: df.ix[:, 10:20]
Out[49]:
10 20
a b a b
10 a 0.077368 0.360018 0.649403 -0.221877
b 0.890805 -2.142297 0.758411 -1.650710
20 a -0.401678 0.481390 -1.080735 0.621861
b -0.504422 -1.555415 -0.023859 0.211287
30 a -0.118969 -0.432082 -0.888786 1.167191
b -0.580182 2.920769 -0.685617 1.327784
This is inconsistent to me:
In [50]: df.ix[10, :]
Out[50]:
10 20 30
a b a b a b
a 0.077368 0.360018 0.649403 -0.221877 -1.527411 0.485647
b 0.890805 -2.142297 0.758411 -1.650710 0.041276 -0.040894
In [51]: df.ix[:, 10]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
...
IndexError: index out of bounds
and this also
In [52]: df.ix[0, :]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
...
KeyError: 0
In [53]: df.ix[:, 0]
Out[53]:
10 a 0.077368
b 0.890805
20 a -0.401678
b -0.504422
30 a -0.118969
b -0.580182
Name: (10, a), Dtype: float64
Metadata
Metadata
Assignees
Labels
IndexingRelated to indexing on series/frames, not to indexes themselvesRelated to indexing on series/frames, not to indexes themselvesMultiIndex
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
jreback commentedon Mar 6, 2013
i think with #2922, these make more sense (obviously
.ix
unchanged, but users have a choice to use non-ambiguous selectors instead)...eg.
lodagro commentedon Mar 6, 2013
Hmm, i clearly did not follow the thread in #2922 close enough, since i am surprised by failure of
df.loc[10:20, :]
, some catching up to do :-)jreback commentedon Mar 14, 2013
close this? going to add to cookbook in any event
lodagro commentedon Mar 14, 2013
You prefer to close this since
.ix
is old stuff now, and no plans to change this?On the above DataFrame
df.loc[10, ]
anddf.loc[:, 10]
(in contrast to ix) work fine, however slicing on an integer MultiIndex level does not, as you already indicated (would that require a seperate issue?).jreback commentedon Mar 14, 2013
your example probably SHOULD work, but ix is quite tricky, I am not sure there are plans to change/fix it. could certainly bump this to 0.12. if you would like
slicing does work on integer multi-index just respects labels or positions depending on what you choose. Your example in this issue is good at showing he ambiguity!
am I missing something?
lodagro commentedon Mar 14, 2013
Ok, we agree that both
df.ix[10, :]
anddf.ix[:, 10]
should work. For me it is even fine to bump this tosome day
, i can work around it just fine, it is just something i noted and thought could be improved.The label slicing with loc is something else, i don not think i am missing between label and position, it is all label ... however
compare this to
jreback commentedon Mar 14, 2013
i think you are right, I am treating the integer slice and expanding it to the integers in the range rather than the labels, so your first example should work, I will file a bug on this. note that it will be an INCLUSIVE range because these are labels
jreback commentedon Mar 14, 2013
@lodagro I updated the example...thanks for the catch!
jreback commentedon Dec 18, 2013
@lodagro this should be closed by #3055 right?
lodagro commentedon Dec 19, 2013
@jreback We discussed in fact two issues here. One being the ix inconsistency (the reason why this issue was opened), the other one loc failure on integer slices (which are labels iso positions). Loc one is resolved, ix seems to be the same.
TomAugspurger commentedon Jan 19, 2017
Closing due to deprecation in #15113