Skip to content

Commit 27135a5

Browse files
authored
Improve error message for DataFrame.from_dict when wrong orient is provided (#47451)
* Fixed error message * Update v1.4.3.rst * Update v1.4.3.rst * Added test case * Added test * Fix rst file * Fix black issues * Fixed error msg and corresponding test * improved changelog * Also fix python docstring for :func:to_dict * Fix failing test
1 parent 70adab9 commit 27135a5

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

doc/source/whatsnew/v1.4.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Bug fixes
5252
~~~~~~~~~
5353
- Bug in :func:`pandas.eval`, :meth:`DataFrame.eval` and :meth:`DataFrame.query` where passing empty ``local_dict`` or ``global_dict`` was treated as passing ``None`` (:issue:`47084`)
5454
- Most I/O methods no longer suppress ``OSError`` and ``ValueError`` when closing file handles (:issue:`47136`)
55+
- Improving error message raised by :meth:`DataFrame.from_dict` when passing an invalid ``orient`` parameter (:issue:`47450`)
5556

5657
.. ---------------------------------------------------------------------------
5758

pandas/core/frame.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1720,7 +1720,10 @@ def from_dict(
17201720
if columns is not None:
17211721
raise ValueError(f"cannot use columns parameter with orient='{orient}'")
17221722
else: # pragma: no cover
1723-
raise ValueError("only recognize index or columns for orient")
1723+
raise ValueError(
1724+
f"Expected 'index', 'columns' or 'tight' for orient parameter. "
1725+
f"Got '{orient}' instead"
1726+
)
17241727

17251728
if orient != "tight":
17261729
return cls(data, index=index, columns=columns, dtype=dtype)
@@ -1817,7 +1820,7 @@ def to_dict(self, orient: str = "dict", into=dict):
18171820
18181821
Parameters
18191822
----------
1820-
orient : str {'dict', 'list', 'series', 'split', 'records', 'index'}
1823+
orient : str {'dict', 'list', 'series', 'split', 'tight', 'records', 'index'}
18211824
Determines the type of the values of the dictionary.
18221825
18231826
- 'dict' (default) : dict like {column -> {index -> value}}

pandas/tests/frame/constructors/test_from_dict.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ class TestFromDict:
1717
# Note: these tests are specific to the from_dict method, not for
1818
# passing dictionaries to DataFrame.__init__
1919

20-
def test_from_dict_scalars_requires_index(self):
21-
msg = "If using all scalar values, you must pass an index"
22-
with pytest.raises(ValueError, match=msg):
23-
DataFrame.from_dict(OrderedDict([("b", 8), ("a", 5), ("a", 6)]))
24-
2520
def test_constructor_list_of_odicts(self):
2621
data = [
2722
OrderedDict([["a", 1.5], ["b", 3], ["c", 4], ["d", 6]]),
@@ -189,3 +184,16 @@ def test_frame_dict_constructor_empty_series(self):
189184
# it works!
190185
DataFrame({"foo": s1, "bar": s2, "baz": s3})
191186
DataFrame.from_dict({"foo": s1, "baz": s3, "bar": s2})
187+
188+
def test_from_dict_scalars_requires_index(self):
189+
msg = "If using all scalar values, you must pass an index"
190+
with pytest.raises(ValueError, match=msg):
191+
DataFrame.from_dict(OrderedDict([("b", 8), ("a", 5), ("a", 6)]))
192+
193+
def test_from_dict_orient_invalid(self):
194+
msg = (
195+
"Expected 'index', 'columns' or 'tight' for orient parameter. "
196+
"Got 'abc' instead"
197+
)
198+
with pytest.raises(ValueError, match=msg):
199+
DataFrame.from_dict({"foo": 1, "baz": 3, "bar": 2}, orient="abc")

0 commit comments

Comments
 (0)