Skip to content

Commit de17188

Browse files
committed
Move check and add another test
1 parent 55a6a2a commit de17188

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

pandas/core/frame.py

+21-10
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
_ensure_int64,
5555
_ensure_platform_int,
5656
is_list_like,
57+
is_nested_list_like,
5758
is_iterator,
5859
is_sequence,
5960
is_named_tuple)
@@ -1256,11 +1257,6 @@ def from_items(cls, items, columns=None, orient='columns'):
12561257
"""
12571258
keys, values = lzip(*items)
12581259

1259-
for val in values:
1260-
if not is_list_like(val):
1261-
raise TypeError('The value in each (key, value) pair must '
1262-
'be an array or a Series')
1263-
12641260
if orient == 'columns':
12651261
if columns is not None:
12661262
columns = _ensure_index(columns)
@@ -1277,16 +1273,31 @@ def from_items(cls, items, columns=None, orient='columns'):
12771273
columns = _ensure_index(keys)
12781274
arrays = values
12791275

1280-
return cls._from_arrays(arrays, columns, None)
1276+
try:
1277+
return cls._from_arrays(arrays, columns, None)
1278+
1279+
except ValueError:
1280+
if not is_nested_list_like(values):
1281+
raise TypeError('The value in each (key, value) pair must '
1282+
'be an array or a Series')
1283+
12811284
elif orient == 'index':
12821285
if columns is None:
12831286
raise TypeError("Must pass columns with orient='index'")
12841287

1285-
keys = _ensure_index(keys)
1288+
try:
1289+
keys = _ensure_index(keys)
1290+
1291+
arr = np.array(values, dtype=object).T
1292+
data = [lib.maybe_convert_objects(v) for v in arr]
1293+
1294+
return cls._from_arrays(data, columns, keys)
1295+
1296+
except TypeError:
1297+
if not is_nested_list_like(values):
1298+
raise TypeError('The value in each (key, value) pair must '
1299+
'be an array or a Series')
12861300

1287-
arr = np.array(values, dtype=object).T
1288-
data = [lib.maybe_convert_objects(v) for v in arr]
1289-
return cls._from_arrays(data, columns, keys)
12901301
else: # pragma: no cover
12911302
raise ValueError("'orient' must be either 'columns' or 'index'")
12921303

pandas/tests/frame/test_constructors.py

+6
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,12 @@ def test_constructor_from_items(self):
12111211
'pair must be an array or a Series'):
12121212
DataFrame.from_items([('A', 1), ('B', 4)])
12131213

1214+
with tm.assert_raises_regex(TypeError,
1215+
'The value in each \(key, value\) '
1216+
'pair must be an array or a Series'):
1217+
DataFrame.from_items([('A', 1), ('B', 2)], columns=['col1'],
1218+
orient='index')
1219+
12141220
def test_constructor_mix_series_nonseries(self):
12151221
df = DataFrame({'A': self.frame['A'],
12161222
'B': list(self.frame['B'])}, columns=['A', 'B'])

0 commit comments

Comments
 (0)