Skip to content

Commit 1aa4a56

Browse files
committed
add comments and tests
1 parent 2ecbb4d commit 1aa4a56

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

doc/source/whatsnew/v0.22.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Other API Changes
7979
- :func:`Dataframe.unstack` will now default to filling with ``np.nan`` for ``object`` columns. (:issue:`12815`)
8080
- :class:`IntervalIndex` constructor will raise if the ``closed`` parameter conflicts with how the input data is inferred to be closed (:issue:`18421`)
8181
- Inserting missing values into indexes will work for all types of indexes and automatically insert the correct type of missing value (``NaN``, ``NaT``, etc.) regardless of the type passed in (:issue:`18295`)
82-
82+
- :func:`DataFrame.from_items` provides a more informative error message when passed scalar values (:issue:`17312`)
8383

8484
.. _whatsnew_0220.deprecations:
8585

pandas/core/frame.py

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

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

12781280
except ValueError:
12791281
if not is_nested_list_like(values):
12801282
raise TypeError('The value in each (key, value) pair must '
1281-
'be an array or a Series')
1283+
'be an array, Series, or dict')
12821284

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

1287-
try:
1288-
keys = _ensure_index(keys)
1289+
keys = _ensure_index(keys)
12891290

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

12951298
except TypeError:
12961299
if not is_nested_list_like(values):
12971300
raise TypeError('The value in each (key, value) pair must '
1298-
'be an array or a Series')
1301+
'be an array, Series, or dict')
12991302

13001303
else: # pragma: no cover
13011304
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):
@@ -1204,15 +1205,16 @@ def test_constructor_from_items(self):
12041205
columns=['one', 'two', 'three'])
12051206
tm.assert_frame_equal(rs, xp)
12061207

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

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

0 commit comments

Comments
 (0)