Skip to content

Commit 609d6a6

Browse files
committed
Merge pull request pandas-dev#5011 from cpcloud/concat-err-msg
ER: give concat a better error message for incompatible types
2 parents d3b48a8 + bb913be commit 609d6a6

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

doc/source/release.rst

+2
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ Improvements to existing features
136136
- Both ExcelFile and read_excel to accept an xlrd.Book for the io
137137
(formerly path_or_buf) argument; this requires engine to be set.
138138
(:issue:`4961`).
139+
- ``concat`` now gives a more informative error message when passed objects
140+
that cannot be concatenated (:issue:`4608`).
139141

140142
API Changes
141143
~~~~~~~~~~~

pandas/tools/merge.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,11 @@ def _get_comb_axis(self, i):
12451245
if self._is_series:
12461246
all_indexes = [x.index for x in self.objs]
12471247
else:
1248-
all_indexes = [x._data.axes[i] for x in self.objs]
1248+
try:
1249+
all_indexes = [x._data.axes[i] for x in self.objs]
1250+
except IndexError:
1251+
types = [type(x).__name__ for x in self.objs]
1252+
raise TypeError("Cannot concatenate list of %s" % types)
12491253

12501254
return _get_combined_index(all_indexes, intersect=self.intersect)
12511255

@@ -1256,6 +1260,10 @@ def _get_concat_axis(self):
12561260
elif self.keys is None:
12571261
names = []
12581262
for x in self.objs:
1263+
if not isinstance(x, Series):
1264+
raise TypeError("Cannot concatenate type 'Series' "
1265+
"with object of type "
1266+
"%r" % type(x).__name__)
12591267
if x.name is not None:
12601268
names.append(x.name)
12611269
else:

pandas/tools/tests/test_merge.py

+9
Original file line numberDiff line numberDiff line change
@@ -1804,6 +1804,15 @@ def test_concat_invalid_first_argument(self):
18041804
# generator ok though
18051805
concat(DataFrame(np.random.rand(5,5)) for _ in range(3))
18061806

1807+
def test_concat_mixed_types_fails(self):
1808+
df = DataFrame(randn(10, 1))
1809+
1810+
with tm.assertRaisesRegexp(TypeError, "Cannot concatenate.+"):
1811+
concat([df[0], df], axis=1)
1812+
1813+
with tm.assertRaisesRegexp(TypeError, "Cannot concatenate.+"):
1814+
concat([df, df[0]], axis=1)
1815+
18071816
class TestOrderedMerge(unittest.TestCase):
18081817

18091818
def setUp(self):

0 commit comments

Comments
 (0)