# Buggy code
def _convert_to_indexer(self, obj, axis=0):
    """
    Convert indexing key into something we can use to do actual fancy
    indexing on an ndarray

    Examples
    ix[:5] -> slice(0, 5)
    ix[[1,2,3]] -> [1,2,3]
    ix[['foo', 'bar', 'baz']] -> [i, j, k] (indices of foo, bar, baz)

    Going by Zen of Python?
    "In the face of ambiguity, refuse the temptation to guess."
    raise AmbiguousIndexError with integer labels?
    - No, prefer label-based indexing
    """
    index = self.obj._get_axis(axis)
    is_int_index = _is_integer_index(index)
    if isinstance(obj, slice):
        if _is_label_slice(index, obj):
            i, j = index.slice_locs(obj.start, obj.stop)

            if obj.step is not None:
                raise Exception("Non-zero step not supported with label-based slicing")
            return slice(i, j)
        else:
            return obj
    elif _is_list_like(obj):
        objarr = _asarray_tuplesafe(obj)

        if objarr.dtype == np.bool_:
            if not obj.index.equals(index):
                raise IndexingError(
                    "Cannot use boolean index with misaligned or unequal labels"
                )
            return objarr
        else:
            # If have integer labels, defer to label-based indexing
            if _is_integer_dtype(objarr) and not is_int_index:
                return objarr

            indexer = index.get_indexer(objarr)
            mask = indexer == -1
            if mask.any():
                raise KeyError("%s not in index" % objarr[mask])

            return indexer
    else:
        if _is_int_like(obj) and not is_int_index:
            return obj
        return index.get_loc(obj)


# Traceback (KeyError)
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?

# Fixed code
def _convert_to_indexer(self, obj, axis=0):
    """
    Convert indexing key into something we can use to do actual fancy
    indexing on an ndarray

    Examples
    ix[:5] -> slice(0, 5)
    ix[[1,2,3]] -> [1,2,3]
    ix[['foo', 'bar', 'baz']] -> [i, j, k] (indices of foo, bar, baz)

    Going by Zen of Python?
    "In the face of ambiguity, refuse the temptation to guess."
    raise AmbiguousIndexError with integer labels?
    - No, prefer label-based indexing
    """
    index = self.obj._get_axis(axis)

    try:
        return index.get_loc(obj)
    except (KeyError, TypeError):
        pass

    is_int_index = _is_integer_index(index)
    if isinstance(obj, slice):
        if _is_label_slice(index, obj):
            i, j = index.slice_locs(obj.start, obj.stop)

            if obj.step is not None:
                raise Exception("Non-zero step not supported with label-based slicing")
            return slice(i, j)
        else:
            return obj
    elif _is_list_like(obj):
        objarr = _asarray_tuplesafe(obj)

        if objarr.dtype == np.bool_:
            if not obj.index.equals(index):
                raise IndexingError(
                    "Cannot use boolean index with misaligned or unequal labels"
                )
            return objarr
        else:
            # If have integer labels, defer to label-based indexing
            if _is_integer_dtype(objarr) and not is_int_index:
                return objarr

            indexer = index.get_indexer(objarr)
            mask = indexer == -1
            if mask.any():
                raise KeyError("%s not in index" % objarr[mask])

            return indexer
    else:
        if _is_int_like(obj) and not is_int_index:
            return obj
        return index.get_loc(obj)