Skip to content

ERROR: better error message reporting on inserting incompatible column to frame (GH4107) #4108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback FYI: this format isn't Python 3 compatible, better to use except Exception as detail and frankly you could collapse all of these lines into:

assertRaisesRegexp(TypeError, "incompatible index of inserted column with frame index", df.__setitem__, 'gr', df.groupby(['b', 'c']).count())

or

def testit(): df['gr'] = df.groupby(['b', 'c']).count()
assertRaisesRegexp(TypeError, "incompatible index of inserted column with frame index", testit)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think 2to3 changes in any event

this test (and one above about constructor error msgs) should prob be changed then

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)),
Expand Down