Skip to content

Commit 326fb28

Browse files
committed
Move check and add another test
1 parent d794f78 commit 326fb28

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)
@@ -1255,11 +1256,6 @@ def from_items(cls, items, columns=None, orient='columns'):
12551256
"""
12561257
keys, values = lzip(*items)
12571258

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

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

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

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

pandas/tests/frame/test_constructors.py

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

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

0 commit comments

Comments
 (0)