Skip to content

reindex fails on non-unique index #2067

Closed
@gerigk

Description

@gerigk

Since reindex and ix should behave the same way I guess since

"Some might conclude that ix and reindex are 100% equivalent based on this. This is indeed true except in the case of integer indexing."

The docstring also doesn't give any hint that one should use a unique index.

In [35]:

import pandas as pd
In [36]:

df = pd.DataFrame([('a',2), ('a',3), ('b',4)], columns = ['A', 'B'])
df2 = pd.DataFrame([('a',2), ('a',3), ('d',4)], columns = ['A', 'B'])
df.set_index('A', inplace=True)
df2.set_index('A', inplace=True)
Out[36]:
B
A   
a    2
a    3
d    4
In [37]:

df.ix[df.index.intersection(df2.index)]
Out[37]:
B
A   
a    2
a    3
In [38]:

df.reindex(df.index.intersection(df2.index))
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-38-ad4829c75d14> in <module>()
----> 1 df.reindex(df.index.intersection(df2.index))

/usr/local/lib/python2.7/dist-packages/pandas-0.9.0-py2.7-linux-x86_64.egg/pandas/core/frame.pyc in reindex(self, index, columns, method, level, fill_value, limit, copy)
   2244         if index is not None:
   2245             frame = frame._reindex_index(index, method, copy, level,
-> 2246                                          fill_value, limit)
   2247 
   2248         return frame

/usr/local/lib/python2.7/dist-packages/pandas-0.9.0-py2.7-linux-x86_64.egg/pandas/core/frame.pyc in _reindex_index(self, new_index, method, copy, level, fill_value, limit)
   2320                        limit=None):
   2321         new_index, indexer = self.index.reindex(new_index, method, level,
-> 2322                                                 limit=limit)
   2323         return self._reindex_with_indexers(new_index, indexer, None, None,
   2324                                            copy, fill_value)

/usr/local/lib/python2.7/dist-packages/pandas-0.9.0-py2.7-linux-x86_64.egg/pandas/core/index.pyc in reindex(self, target, method, level, limit)
    825             else:
    826                 indexer = self.get_indexer(target, method=method,
--> 827                                            limit=limit)
    828         return target, indexer
    829 

/usr/local/lib/python2.7/dist-packages/pandas-0.9.0-py2.7-linux-x86_64.egg/pandas/core/index.pyc in get_indexer(self, target, method, limit)
    746 
    747         if not self.is_unique:
--> 748             raise Exception('Reindexing only valid with uniquely valued Index '
    749                             'objects')
    750 

Exception: Reindexing only valid with uniquely valued Index objects

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions