df.loc with non-unique index? Should this work?

778 views
Skip to first unread message

Richard Stanton

unread,
May 20, 2013, 2:15:25 PM5/20/13
to pyd...@googlegroups.com
I just tracked a bug in my code down to executing something like the
following code, which produces the error listed when the index is not
unique. Is the error intentional, or should this code work? (It's not
obvious to me).

Thanks.

Richard Stanton

df = DataFrame({'A' : [1,2,3,4], 'B' : [3, 4, 5, 6]}, index = [0,1,0,1])
df.loc[1:]



---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-46-1cdf797dd554> in <module>()
1 df = DataFrame({'A' : [1,2,3,4], 'B' : [3, 4, 5, 6]}, index =
[0,1,0,1])
----> 2 df.loc[1:]

/RHS/packages/anaconda/pandas/pandas/core/indexing.pyc in
__getitem__(self, key)
684 return self._getitem_tuple(key)
685 else:
--> 686 return self._getitem_axis(key, axis=0)
687
688 def _getitem_axis(self, key, axis=0):

/RHS/packages/anaconda/pandas/pandas/core/indexing.pyc in
_getitem_axis(self, key, axis)
760
761 if isinstance(key, slice):
--> 762 return self._get_slice_axis(key, axis=axis)
763 elif com._is_bool_indexer(key):
764 return self._getbool_axis(key, axis=axis)

/RHS/packages/anaconda/pandas/pandas/core/indexing.pyc in
_get_slice_axis(self, slice_obj, axis)
704
705 labels = obj._get_axis(axis)
--> 706 indexer = labels.slice_indexer(slice_obj.start,
slice_obj.stop, slice_obj.step)
707
708 if isinstance(indexer, slice):

/RHS/packages/anaconda/pandas/pandas/core/index.pyc in slice_indexer(self,
start, end, step)
1199
1200 # loc indexers
-> 1201 return Index(start_slice) & Index(end_slice)
1202
1203 def slice_locs(self, start=None, end=None):

/RHS/packages/anaconda/pandas/pandas/core/index.pyc in __new__(cls, data,
dtype, copy, name, **kwargs)
111 elif np.isscalar(data):
112 raise ValueError('Index(...) must be called with a
collection '
--> 113 'of some kind, %s was passed' %
repr(data))
114 else:
115 # other iterable of some kind

ValueError: Index(...) must be called with a collection of some kind, 4
was passed





Jeff

unread,
May 20, 2013, 3:27:45 PM5/20/13
to pyd...@googlegroups.com
https://github.com/pydata/pandas/issues/3659

Thanks...this is broken....will fix for 0.11.1

Jeff

unread,
May 20, 2013, 3:46:16 PM5/20/13
to pyd...@googlegroups.com
ANd here's a PR to fix it

Richard,

see my comments in the PR

the example you gave is actually non-monotic and non-unique, so 
I have to raise as you are trying to slice

the 2nd example works (it becomes monotonic when you sort)

let me know if this is not what you would expect

Jeff
Reply all
Reply to author
Forward
0 new messages