Skip to content

ER: give concat a better error message for incompatible types #5011

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
Sep 27, 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
2 changes: 2 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ Improvements to existing features
- Both ExcelFile and read_excel to accept an xlrd.Book for the io
(formerly path_or_buf) argument; this requires engine to be set.
(:issue:`4961`).
- ``concat`` now gives a more informative error message when passed objects
that cannot be concatenated (:issue:`4608`).

API Changes
~~~~~~~~~~~
Expand Down
10 changes: 9 additions & 1 deletion pandas/tools/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,11 @@ def _get_comb_axis(self, i):
if self._is_series:
all_indexes = [x.index for x in self.objs]
else:
all_indexes = [x._data.axes[i] for x in self.objs]
try:
all_indexes = [x._data.axes[i] for x in self.objs]
except IndexError:
types = [type(x).__name__ for x in self.objs]
raise TypeError("Cannot concatenate list of %s" % types)

return _get_combined_index(all_indexes, intersect=self.intersect)

Expand All @@ -1256,6 +1260,10 @@ def _get_concat_axis(self):
elif self.keys is None:
names = []
for x in self.objs:
if not isinstance(x, Series):
raise TypeError("Cannot concatenate type 'Series' "
"with object of type "
"%r" % type(x).__name__)
if x.name is not None:
names.append(x.name)
else:
Expand Down
9 changes: 9 additions & 0 deletions pandas/tools/tests/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1804,6 +1804,15 @@ def test_concat_invalid_first_argument(self):
# generator ok though
concat(DataFrame(np.random.rand(5,5)) for _ in range(3))

def test_concat_mixed_types_fails(self):
df = DataFrame(randn(10, 1))

with tm.assertRaisesRegexp(TypeError, "Cannot concatenate.+"):
concat([df[0], df], axis=1)

with tm.assertRaisesRegexp(TypeError, "Cannot concatenate.+"):
concat([df, df[0]], axis=1)

class TestOrderedMerge(unittest.TestCase):

def setUp(self):
Expand Down