Skip to content

Commit e57b142

Browse files
committed
add comments and tests
1 parent de17188 commit e57b142

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

doc/source/whatsnew/v0.22.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Other API Changes
4545
- :class:`Timestamp` will no longer silently ignore unused or invalid `tz` or `tzinfo` keyword arguments (:issue:`17690`)
4646
- :class:`Timestamp` will no longer silently ignore invalid `freq` arguments (:issue:`5168`)
4747
- :class:`CacheableOffset` and :class:`WeekDay` are no longer available in the `tseries.offsets` module (:issue:`17830`)
48+
- :func:`DataFrame.from_items` provides a more informative error message when passed scalar values (:issue:`17312`)
4849

4950
.. _whatsnew_0220.deprecations:
5051

pandas/core/frame.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -1273,30 +1273,33 @@ def from_items(cls, items, columns=None, orient='columns'):
12731273
columns = _ensure_index(keys)
12741274
arrays = values
12751275

1276+
# GH 17312
1277+
# Provide more informative error msg when scalar values passed
12761278
try:
12771279
return cls._from_arrays(arrays, columns, None)
12781280

12791281
except ValueError:
12801282
if not is_nested_list_like(values):
12811283
raise TypeError('The value in each (key, value) pair must '
1282-
'be an array or a Series')
1284+
'be an array, Series, or dict')
12831285

12841286
elif orient == 'index':
12851287
if columns is None:
12861288
raise TypeError("Must pass columns with orient='index'")
12871289

1288-
try:
1289-
keys = _ensure_index(keys)
1290+
keys = _ensure_index(keys)
12901291

1292+
# GH 17312
1293+
# Provide more informative error msg when scalar values passed
1294+
try:
12911295
arr = np.array(values, dtype=object).T
12921296
data = [lib.maybe_convert_objects(v) for v in arr]
1293-
12941297
return cls._from_arrays(data, columns, keys)
12951298

12961299
except TypeError:
12971300
if not is_nested_list_like(values):
12981301
raise TypeError('The value in each (key, value) pair must '
1299-
'be an array or a Series')
1302+
'be an array, Series, or dict')
13001303

13011304
else: # pragma: no cover
13021305
raise ValueError("'orient' must be either 'columns' or 'index'")

pandas/tests/frame/test_constructors.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,14 @@ def test_constructor_dict(self):
268268

269269
# GH10856
270270
# dict with scalar values should raise error, even if columns passed
271-
with pytest.raises(ValueError):
271+
msg = 'If using all scalar values, you must pass an index'
272+
with tm.assert_raises_regex(ValueError, msg):
272273
DataFrame({'a': 0.7})
273274

274-
with pytest.raises(ValueError):
275+
with tm.assert_raises_regex(ValueError, msg):
275276
DataFrame({'a': 0.7}, columns=['a'])
276277

277-
with pytest.raises(ValueError):
278+
with tm.assert_raises_regex(ValueError, msg):
278279
DataFrame({'a': 0.7}, columns=['b'])
279280

280281
def test_constructor_multi_index(self):
@@ -1205,15 +1206,16 @@ def test_constructor_from_items(self):
12051206
columns=['one', 'two', 'three'])
12061207
tm.assert_frame_equal(rs, xp)
12071208

1209+
def test_constructor_from_items_scalars(self):
12081210
# GH 17312
12091211
with tm.assert_raises_regex(TypeError,
12101212
'The value in each \(key, value\) '
1211-
'pair must be an array or a Series'):
1213+
'pair must be an array, Series, or dict'):
12121214
DataFrame.from_items([('A', 1), ('B', 4)])
12131215

12141216
with tm.assert_raises_regex(TypeError,
12151217
'The value in each \(key, value\) '
1216-
'pair must be an array or a Series'):
1218+
'pair must be an array, Series, or dict'):
12171219
DataFrame.from_items([('A', 1), ('B', 2)], columns=['col1'],
12181220
orient='index')
12191221

0 commit comments

Comments
 (0)