File tree 3 files changed +20
-1
lines changed
3 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -136,6 +136,8 @@ Improvements to existing features
136
136
- Both ExcelFile and read_excel to accept an xlrd.Book for the io
137
137
(formerly path_or_buf) argument; this requires engine to be set.
138
138
(:issue: `4961 `).
139
+ - ``concat `` now gives a more informative error message when passed objects
140
+ that cannot be concatenated (:issue: `4608 `).
139
141
140
142
API Changes
141
143
~~~~~~~~~~~
Original file line number Diff line number Diff line change @@ -1245,7 +1245,11 @@ def _get_comb_axis(self, i):
1245
1245
if self ._is_series :
1246
1246
all_indexes = [x .index for x in self .objs ]
1247
1247
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 )
1249
1253
1250
1254
return _get_combined_index (all_indexes , intersect = self .intersect )
1251
1255
@@ -1256,6 +1260,10 @@ def _get_concat_axis(self):
1256
1260
elif self .keys is None :
1257
1261
names = []
1258
1262
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__ )
1259
1267
if x .name is not None :
1260
1268
names .append (x .name )
1261
1269
else :
Original file line number Diff line number Diff line change @@ -1804,6 +1804,15 @@ def test_concat_invalid_first_argument(self):
1804
1804
# generator ok though
1805
1805
concat (DataFrame (np .random .rand (5 ,5 )) for _ in range (3 ))
1806
1806
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
+
1807
1816
class TestOrderedMerge (unittest .TestCase ):
1808
1817
1809
1818
def setUp (self ):
You can’t perform that action at this time.
0 commit comments