Skip to content

Setting elements with MultiIndex through ix can fail #551

Closed
@wesm

Description

@wesm

reported by @CRP

Hi, I am currently trying to access and change elements of a multiindexed DataFrame with .ix. This works on read but not on write:

In [342]: arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]

In [343]: tuples = zip(*arrays)

In [344]: index = MultiIndex.from_tuples(tuples, names=['first', 'second'])

In [345]: a=DataFrame(randn(6, 3), index=index[:6])

In [346]: a.ix[('bar','two'),1]
Out[346]: -0.574310975217078

In [347]: a.ix[('bar','two'),1]=999
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/Users/c.prinoth/<ipython-input-347-755b7b16a7da> in <module>()
----> 1 a.ix[('bar','two'),1]=999

/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/pandas/core/indexing.pyc in __setitem__(self, key, value)
     56                 raise IndexingError('only tuples of length <= %d supported',
     57                                     self.ndim)
---> 58             indexer = self._convert_tuple(key)
     59         else:
     60             indexer = self._convert_to_indexer(key)

/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/pandas/core/indexing.pyc in _convert_tuple(self, key)
     65         keyidx = []
     66         for i, k in enumerate(key):
---> 67             idx = self._convert_to_indexer(k, axis=i)
     68             keyidx.append(idx)
     69         return _maybe_convert_ix(*keyidx)

/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/pandas/core/indexing.pyc in _convert_to_indexer(self, obj, axis)
    239                 mask = indexer == -1
    240                 if mask.any():
--> 241                     raise KeyError('%s not in index' % objarr[mask])
    242 
    243                 return indexer

KeyError: '[bar two] not in index'

Is this a bug or am I using the index incorrectly?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions