Skip to content

df.ix[] inconsistency between axis for MultiIndex #2904

Closed
@lodagro

Description

@lodagro
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

No one assigned

    Labels

    IndexingRelated to indexing on series/frames, not to indexes themselvesMultiIndex

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions