From 7240ecab70257a403e7c20cd0906723d3f888585 Mon Sep 17 00:00:00 2001 From: Kian S <34144932+KianShah@users.noreply.github.com> Date: Tue, 21 Jun 2022 12:52:44 -0700 Subject: [PATCH 01/11] Fixed error message --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 8711d53353185..f7e50483a5991 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1720,7 +1720,7 @@ 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("only recognize 'index', 'columns', or 'tight' for orient") if orient != "tight": return cls(data, index=index, columns=columns, dtype=dtype) From de3cc119c62fe6a530c1061dd180655024de380c Mon Sep 17 00:00:00 2001 From: Kian S <34144932+KianShah@users.noreply.github.com> Date: Tue, 21 Jun 2022 13:15:20 -0700 Subject: [PATCH 02/11] Update v1.4.3.rst --- doc/source/whatsnew/v1.4.3.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.3.rst b/doc/source/whatsnew/v1.4.3.rst index d031426a2abbf..4ab7f2335d972 100644 --- a/doc/source/whatsnew/v1.4.3.rst +++ b/doc/source/whatsnew/v1.4.3.rst @@ -40,7 +40,7 @@ Bug fixes ~~~~~~~~~ - Bug in :meth:`pd.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 do no longer suppress ``OSError`` and ``ValueError`` when closing file handles (:issue:`47136`) -- +- Fixed unintuitive error message in :meth:`DataFrame.from_dict` that didn't list 'tight' as an acceptable parameter .. --------------------------------------------------------------------------- From cc41d3a66c6d1d52728da73348dd5cfed9d2c5bc Mon Sep 17 00:00:00 2001 From: Kian S <34144932+KianShah@users.noreply.github.com> Date: Tue, 21 Jun 2022 16:08:08 -0700 Subject: [PATCH 03/11] Update v1.4.3.rst --- doc/source/whatsnew/v1.4.3.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.3.rst b/doc/source/whatsnew/v1.4.3.rst index 4ab7f2335d972..630e1c4eefa82 100644 --- a/doc/source/whatsnew/v1.4.3.rst +++ b/doc/source/whatsnew/v1.4.3.rst @@ -40,7 +40,7 @@ Bug fixes ~~~~~~~~~ - Bug in :meth:`pd.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 do no longer suppress ``OSError`` and ``ValueError`` when closing file handles (:issue:`47136`) -- Fixed unintuitive error message in :meth:`DataFrame.from_dict` that didn't list 'tight' as an acceptable parameter +- Added parameter `tight` in :meth:`DataFrame.from_dict` error message .. --------------------------------------------------------------------------- From bffe1c18746c5b6cd54c351316d7a68ffeedfdc1 Mon Sep 17 00:00:00 2001 From: kians376 Date: Tue, 21 Jun 2022 16:26:27 -0700 Subject: [PATCH 04/11] Added test case --- .../tests/frame/constructors/test_from_dict.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/pandas/tests/frame/constructors/test_from_dict.py b/pandas/tests/frame/constructors/test_from_dict.py index 72107d849f598..cd7c8af70d6fc 100644 --- a/pandas/tests/frame/constructors/test_from_dict.py +++ b/pandas/tests/frame/constructors/test_from_dict.py @@ -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]]), @@ -189,3 +184,14 @@ 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): + 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 = "only recognize 'index', 'columns', or 'tight' for orient" + with pytest.raises(ValueError, match=msg): + DataFrame.from_dict({"foo": s1, "baz": s3, "bar": s2}, orient='abc') + From 16ccf810ba15f0e1d4ac19fb9723582b4bdafe2a Mon Sep 17 00:00:00 2001 From: Kian S <34144932+KianShah@users.noreply.github.com> Date: Tue, 21 Jun 2022 16:59:16 -0700 Subject: [PATCH 05/11] Added test --- pandas/tests/frame/constructors/test_from_dict.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pandas/tests/frame/constructors/test_from_dict.py b/pandas/tests/frame/constructors/test_from_dict.py index 72107d849f598..68a88c38005ff 100644 --- a/pandas/tests/frame/constructors/test_from_dict.py +++ b/pandas/tests/frame/constructors/test_from_dict.py @@ -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]]), @@ -189,3 +184,13 @@ 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): + 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 = "only recognize 'index', 'columns', or 'tight' for orient" + with pytest.raises(ValueError, match=msg): + DataFrame.from_dict({"foo": 1, "baz": 3, "bar": 2}, orient='abc') From 869d8bc1e9f34f0fc371f93e6854e83482337473 Mon Sep 17 00:00:00 2001 From: Kian S <34144932+KianShah@users.noreply.github.com> Date: Tue, 21 Jun 2022 17:39:20 -0700 Subject: [PATCH 06/11] Fix rst file --- doc/source/whatsnew/v1.4.3.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.3.rst b/doc/source/whatsnew/v1.4.3.rst index 630e1c4eefa82..70a6be7e696a5 100644 --- a/doc/source/whatsnew/v1.4.3.rst +++ b/doc/source/whatsnew/v1.4.3.rst @@ -40,7 +40,7 @@ Bug fixes ~~~~~~~~~ - Bug in :meth:`pd.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 do no longer suppress ``OSError`` and ``ValueError`` when closing file handles (:issue:`47136`) -- Added parameter `tight` in :meth:`DataFrame.from_dict` error message +- Added parameter ``tight`` in :meth:`DataFrame.from_dict` error message .. --------------------------------------------------------------------------- From d47c8185053f4b9e9be24acfa66d4481a65d94f8 Mon Sep 17 00:00:00 2001 From: Kian S <34144932+KianShah@users.noreply.github.com> Date: Tue, 21 Jun 2022 22:27:42 -0700 Subject: [PATCH 07/11] Fix black issues --- pandas/tests/frame/constructors/test_from_dict.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/frame/constructors/test_from_dict.py b/pandas/tests/frame/constructors/test_from_dict.py index 68a88c38005ff..501e275c1f983 100644 --- a/pandas/tests/frame/constructors/test_from_dict.py +++ b/pandas/tests/frame/constructors/test_from_dict.py @@ -193,4 +193,4 @@ def test_from_dict_scalars_requires_index(self): def test_from_dict_orient_invalid(self): msg = "only recognize 'index', 'columns', or 'tight' for orient" with pytest.raises(ValueError, match=msg): - DataFrame.from_dict({"foo": 1, "baz": 3, "bar": 2}, orient='abc') + DataFrame.from_dict({"foo": 1, "baz": 3, "bar": 2}, orient="abc") From a2d6860ff5a07176ddc8ecba876469eed58f71d3 Mon Sep 17 00:00:00 2001 From: kians376 Date: Wed, 22 Jun 2022 23:02:17 -0700 Subject: [PATCH 08/11] Fixed error msg and corresponding test --- pandas/core/frame.py | 5 ++++- pandas/tests/frame/constructors/test_from_dict.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index b769fd216df57..64aadc0aac223 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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', 'columns', or 'tight' 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) diff --git a/pandas/tests/frame/constructors/test_from_dict.py b/pandas/tests/frame/constructors/test_from_dict.py index 501e275c1f983..cd8857c14033b 100644 --- a/pandas/tests/frame/constructors/test_from_dict.py +++ b/pandas/tests/frame/constructors/test_from_dict.py @@ -191,6 +191,9 @@ def test_from_dict_scalars_requires_index(self): DataFrame.from_dict(OrderedDict([("b", 8), ("a", 5), ("a", 6)])) def test_from_dict_orient_invalid(self): - msg = "only recognize 'index', 'columns', or 'tight' for orient" + msg = ( + "Expected 'index', 'columns' or 'tight' for orient parameter. " + "Got '{orient}' instead" + ) with pytest.raises(ValueError, match=msg): DataFrame.from_dict({"foo": 1, "baz": 3, "bar": 2}, orient="abc") From 6b43ddac8ad4c42e6ca55e2ddac7246f53e837fb Mon Sep 17 00:00:00 2001 From: kians376 Date: Wed, 22 Jun 2022 23:04:54 -0700 Subject: [PATCH 09/11] improved changelog --- doc/source/whatsnew/v1.4.3.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.4.3.rst b/doc/source/whatsnew/v1.4.3.rst index 24b66ca3af37c..a57a040dcfd8b 100644 --- a/doc/source/whatsnew/v1.4.3.rst +++ b/doc/source/whatsnew/v1.4.3.rst @@ -51,8 +51,8 @@ Fixed regressions 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`)- Added parameter ``tight`` in :meth:`DataFrame.from_dict` error message -- Improving error message raised by :meth:`DataFrame.from_dict` when passing an invalid ``orient`` (:issue:`47450`) +- 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`) .. --------------------------------------------------------------------------- From fd78ffd5357c45fffe2638ff042fd4e6e6c6b37a Mon Sep 17 00:00:00 2001 From: kians376 Date: Thu, 23 Jun 2022 01:20:59 -0700 Subject: [PATCH 10/11] Also fix python docstring for :func:to_dict --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 64aadc0aac223..b4a278185b01b 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1820,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}} From 928bdf71bf0c8d3375fd2c1d1b0035a4d6a6961f Mon Sep 17 00:00:00 2001 From: Kian S <34144932+KianShah@users.noreply.github.com> Date: Thu, 23 Jun 2022 12:52:04 -0700 Subject: [PATCH 11/11] Fix failing test --- pandas/tests/frame/constructors/test_from_dict.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/frame/constructors/test_from_dict.py b/pandas/tests/frame/constructors/test_from_dict.py index cd8857c14033b..7c2b009673bb7 100644 --- a/pandas/tests/frame/constructors/test_from_dict.py +++ b/pandas/tests/frame/constructors/test_from_dict.py @@ -193,7 +193,7 @@ def test_from_dict_scalars_requires_index(self): def test_from_dict_orient_invalid(self): msg = ( "Expected 'index', 'columns' or 'tight' for orient parameter. " - "Got '{orient}' instead" + "Got 'abc' instead" ) with pytest.raises(ValueError, match=msg): DataFrame.from_dict({"foo": 1, "baz": 3, "bar": 2}, orient="abc")