Skip to content
This repository

reindex fails on non-unique index #2067

Open
gerigk opened this Issue · 0 comments

1 participant

Arthur Gerigk
Arthur 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.