Skip to content

Commit 67c10cc

Browse files
committed
ERROR: better error message reporting on inserting incompatible column to frame (GH4107)
1 parent 76272e4 commit 67c10cc

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ pandas 0.12
313313
(:issue:`4089`)
314314
- Fix bug where ``HDFStore`` will fail to append because of a different block
315315
ordering on-disk (:issue:`4096`)
316+
- Better error messages on inserting incompatible columns to a frame (:issue:`4107`)
316317

317318

318319
pandas 0.11.0

pandas/core/frame.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2190,7 +2190,13 @@ def _sanitize_column(self, key, value):
21902190
# copy the values
21912191
value = value.values.copy()
21922192
else:
2193-
value = value.reindex(self.index).values
2193+
2194+
# GH 4107
2195+
try:
2196+
value = value.reindex(self.index).values
2197+
except:
2198+
raise TypeError('incompatible index of inserted column '
2199+
'with frame index')
21942200

21952201
if is_frame:
21962202
value = value.T

pandas/tests/test_frame.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,6 +2250,19 @@ def test_constructor_error_msgs(self):
22502250
self.assert_(type(detail) == ValueError)
22512251
self.assert_(msg in str(detail))
22522252

2253+
def test_insert_error_msmgs(self):
2254+
2255+
# GH 4107, more descriptive error message
2256+
df = DataFrame(np.random.randint(0,2,(4,4)),
2257+
columns=['a', 'b', 'c', 'd'])
2258+
2259+
try:
2260+
df['gr'] = df.groupby(['b', 'c']).count()
2261+
except (Exception), detail:
2262+
msg = 'incompatible index of inserted column with frame index'
2263+
self.assert_(type(detail) == TypeError)
2264+
self.assert_(msg in str(detail))
2265+
22532266
def test_constructor_subclass_dict(self):
22542267
# Test for passing dict subclass to constructor
22552268
data = {'col1': tm.TestSubDict((x, 10.0 * x) for x in xrange(10)),

0 commit comments

Comments
 (0)