Skip to content

Improve error message for DataFrame.from_dict when wrong orient is provided #47451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Bug fixes
~~~~~~~~~
- 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`)
- Most I/O methods no longer suppress ``OSError`` and ``ValueError`` when closing file handles (:issue:`47136`)
- Improving error message raised by :meth:`DataFrame.from_dict` when passing an invalid ``orient`` parameter (:issue:`47450`)

.. ---------------------------------------------------------------------------

Expand Down
7 changes: 5 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1720,7 +1720,10 @@ def from_dict(
if columns is not None:
raise ValueError(f"cannot use columns parameter with orient='{orient}'")
else: # pragma: no cover
raise ValueError("only recognize index or columns for orient")
raise ValueError(
f"Expected 'index', 'columns' or 'tight' for orient parameter. "
f"Got '{orient}' instead"
)

if orient != "tight":
return cls(data, index=index, columns=columns, dtype=dtype)
Expand Down Expand Up @@ -1817,7 +1820,7 @@ def to_dict(self, orient: str = "dict", into=dict):

Parameters
----------
orient : str {'dict', 'list', 'series', 'split', 'records', 'index'}
orient : str {'dict', 'list', 'series', 'split', 'tight', 'records', 'index'}
Determines the type of the values of the dictionary.

- 'dict' (default) : dict like {column -> {index -> value}}
Expand Down
18 changes: 13 additions & 5 deletions pandas/tests/frame/constructors/test_from_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ class TestFromDict:
# Note: these tests are specific to the from_dict method, not for
# passing dictionaries to DataFrame.__init__

def test_from_dict_scalars_requires_index(self):
msg = "If using all scalar values, you must pass an index"
with pytest.raises(ValueError, match=msg):
DataFrame.from_dict(OrderedDict([("b", 8), ("a", 5), ("a", 6)]))

def test_constructor_list_of_odicts(self):
data = [
OrderedDict([["a", 1.5], ["b", 3], ["c", 4], ["d", 6]]),
Expand Down Expand Up @@ -189,3 +184,16 @@ def test_frame_dict_constructor_empty_series(self):
# it works!
DataFrame({"foo": s1, "bar": s2, "baz": s3})
DataFrame.from_dict({"foo": s1, "baz": s3, "bar": s2})

def test_from_dict_scalars_requires_index(self):
Copy link
Contributor Author

@KianShah KianShah Jun 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I moved this function down here, so that all functions that test exception behaviours are grouped together. Not sure if that complies to the guidelines, but it certainly seems better

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

msg = "If using all scalar values, you must pass an index"
with pytest.raises(ValueError, match=msg):
DataFrame.from_dict(OrderedDict([("b", 8), ("a", 5), ("a", 6)]))

def test_from_dict_orient_invalid(self):
msg = (
"Expected 'index', 'columns' or 'tight' for orient parameter. "
"Got 'abc' instead"
)
with pytest.raises(ValueError, match=msg):
DataFrame.from_dict({"foo": 1, "baz": 3, "bar": 2}, orient="abc")