diff --git a/doc/source/release.rst b/doc/source/release.rst index 5f2dd3df0c6a3..d410f39da3ab9 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -313,6 +313,7 @@ pandas 0.12 (:issue:`4089`) - Fix bug where ``HDFStore`` will fail to append because of a different block ordering on-disk (:issue:`4096`) + - Better error messages on inserting incompatible columns to a frame (:issue:`4107`) pandas 0.11.0 diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 5fe2d60993f2c..cc25d7e066e30 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2190,7 +2190,13 @@ def _sanitize_column(self, key, value): # copy the values value = value.values.copy() else: - value = value.reindex(self.index).values + + # GH 4107 + try: + value = value.reindex(self.index).values + except: + raise TypeError('incompatible index of inserted column ' + 'with frame index') if is_frame: value = value.T diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 4ed4c37b8afc7..a8a435e3bb660 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -2250,6 +2250,19 @@ def test_constructor_error_msgs(self): self.assert_(type(detail) == ValueError) self.assert_(msg in str(detail)) + def test_insert_error_msmgs(self): + + # GH 4107, more descriptive error message + df = DataFrame(np.random.randint(0,2,(4,4)), + columns=['a', 'b', 'c', 'd']) + + try: + df['gr'] = df.groupby(['b', 'c']).count() + except (Exception), detail: + msg = 'incompatible index of inserted column with frame index' + self.assert_(type(detail) == TypeError) + self.assert_(msg in str(detail)) + def test_constructor_subclass_dict(self): # Test for passing dict subclass to constructor data = {'col1': tm.TestSubDict((x, 10.0 * x) for x in xrange(10)),