Skip to content

Better error message when merging fails because of non unique index #2649

Closed
@jankatins

Description

@jankatins

I've seen this error lot's of times in the last few days and usually it usuall took me a long time to get it working (and mostly I had no idea why it worked when I did something differently :-( ). The below error is actually from https://github.com/statsmodels/statsmodels/pull/582/files

Would it be possibleto let the merge method catch this error and let it produce a more informative error message which says what has to be done differently? Or the original Exception say what index (value) is the problem?

C:\portabel\Python27\lib\site-packages\pandas\core\frame.pyc in merge(self, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy)
   4362                      left_on=left_on, right_on=right_on,
   4363                      left_index=left_index, right_index=right_index, sort=sort,
-> 4364                      suffixes=suffixes, copy=copy)
   4365 
   4366     #----------------------------------------------------------------------

C:\portabel\Python27\lib\site-packages\pandas\tools\merge.pyc in merge(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy)
     34                          right_index=right_index, sort=sort, suffixes=suffixes,
     35                          copy=copy)
---> 36     return op.get_result()
     37 if __debug__: merge.__doc__ = _merge_doc % '\nleft : DataFrame'
     38 

C:\portabel\Python27\lib\site-packages\pandas\tools\merge.pyc in get_result(self)
    185 
    186         # this is a bit kludgy
--> 187         ldata, rdata = self._get_merge_data()
    188 
    189         # TODO: more efficiently handle group keys to avoid extra

C:\portabel\Python27\lib\site-packages\pandas\tools\merge.pyc in _get_merge_data(self)
    276         lsuf, rsuf = self.suffixes
    277         ldata, rdata = ldata._maybe_rename_join(rdata, lsuf, rsuf,
--> 278                                                 copydata=False)
    279         return ldata, rdata
    280 

C:\portabel\Python27\lib\site-packages\pandas\core\internals.pyc in _maybe_rename_join(self, other, lsuffix, rsuffix, copydata)
   1174 
   1175     def _maybe_rename_join(self, other, lsuffix, rsuffix, copydata=True):
-> 1176         to_rename = self.items.intersection(other.items)
   1177         if len(to_rename) > 0:
   1178             if not lsuffix and not rsuffix:

C:\portabel\Python27\lib\site-packages\pandas\core\index.pyc in intersection(self, other)
    653             this = self.astype('O')
    654             other = other.astype('O')
--> 655             return this.intersection(other)
    656 
    657         if self.is_monotonic and other.is_monotonic:

C:\portabel\Python27\lib\site-packages\pandas\core\index.pyc in intersection(self, other)
    662                 pass
    663 
--> 664         indexer = self.get_indexer(other.values)
    665         indexer = indexer.take((indexer != -1).nonzero()[0])
    666         return self.take(indexer)

C:\portabel\Python27\lib\site-packages\pandas\core\index.pyc in get_indexer(self, target, method, limit)
    789             this = self.astype(object)
    790             target = target.astype(object)
--> 791             return this.get_indexer(target, method=method, limit=limit)
    792 
    793         if not self.is_unique:

C:\portabel\Python27\lib\site-packages\pandas\core\index.pyc in get_indexer(self, target, method, limit)
    792 
    793         if not self.is_unique:
--> 794             raise Exception('Reindexing only valid with uniquely valued Index '
    795                             'objects')
    796 

Exception: Reindexing only valid with uniquely valued Index objects

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions