Skip to content

Confusing error message on frame.__set_item__ with duplicate index entries #7432

@roblevy

Description

@roblevy

Struggled with this for a while to work out why I was getting TypeError: incompatible index of inserted column with frame index when doing df['new_col'] = my_series.

Turns out the the series had duplicates in its index. This occured because I was importing a CSV and then setting the index. Perhaps we need a warning when setting an index with duplicate values?

In [2]: df = pd.DataFrame({'foo':['a', 'b', 'c'], 'bar':[1,2,3], 'baz':['d','e','f']}).set_index('foo')

In [3]: df
Out[3]:
     bar baz
foo
a      1   d
b      2   e
c      3   f

[3 rows x 2 columns]

In [5]: ser = pd.DataFrame({'foo':['a', 'b', 'c', 'a'], 'fiz':['g','h','i','j']}).set_index('foo')

In [6]: ser
Out[6]:
    fiz
foo
a     g
b     h
c     i
a     j

[4 rows x 1 columns]
In [8]: df['newcol'] = ser
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-a8d535085de2> in <module>()
----> 1 df['newcol'] = ser

C:\WinPython-64bit-2.7.5.2\python-2.7.5.amd64\lib\site-packages\pandas-0.13.1-py2.7-win-amd64.egg\pandas\core\frame.pyc in __setitem__(self, key, value)
   1885         else:
   1886             # set column
-> 1887             self._set_item(key, value)
   1888
   1889     def _setitem_slice(self, key, value):

C:\WinPython-64bit-2.7.5.2\python-2.7.5.amd64\lib\site-packages\pandas-0.13.1-py2.7-win-amd64.egg\pandas\core\frame.pyc in _set_item(self, key, value)
   1965         is_existing = key in self.columns
   1966         self._ensure_valid_index(value)
-> 1967         value = self._sanitize_column(key, value)
   1968         NDFrame._set_item(self, key, value)
   1969

C:\WinPython-64bit-2.7.5.2\python-2.7.5.amd64\lib\site-packages\pandas-0.13.1-py2.7-win-amd64.egg\pandas\core\frame.pyc in _sanitize_column(self, key, value)
   2008                     value = value.reindex(self.index).values
   2009                 except:
-> 2010                     raise TypeError('incompatible index of inserted column '
   2011                                     'with frame index')
   2012

TypeError: incompatible index of inserted column with frame index

Metadata

Metadata

Assignees

No one assigned

    Labels

    Error ReportingIncorrect or improved errors from pandas

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions