From 8cc7970c0826713036f0f7d66dceac32dabf4dfc Mon Sep 17 00:00:00 2001 From: Ambuj Pawar Date: Fri, 30 Sep 2022 17:22:21 +0200 Subject: [PATCH 1/8] ERR: Improve error message when assigning a complete row using 'at' method --- pandas/core/frame.py | 6 ++++++ pandas/tests/indexing/test_at.py | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 17bd50b9ad8f0..774067b9baa7a 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -222,6 +222,7 @@ console, format as fmt, ) +from pandas.errors import InvalidIndexError from pandas.io.formats.info import ( INFO_DOCSTRING, DataFrameInfo, @@ -4238,6 +4239,11 @@ def _set_value( else: self.loc[index, col] = value self._item_cache.pop(col, None) + + except InvalidIndexError: + # GH48729: Seems like you are trying to assign a value to a + # row when only scalar options are permitted + raise InvalidIndexError(f"You can only assign a scalar value not a {type(value)}") def _ensure_valid_index(self, value) -> None: """ diff --git a/pandas/tests/indexing/test_at.py b/pandas/tests/indexing/test_at.py index f6d2fb12c5d81..67509b7b2e417 100644 --- a/pandas/tests/indexing/test_at.py +++ b/pandas/tests/indexing/test_at.py @@ -234,3 +234,9 @@ def test_at_categorical_integers(self): for key in [0, 1]: with pytest.raises(KeyError, match=str(key)): df.at[key, key] + + def test_at_applied_for_rows(self): + # GH#48729 .at should raise InvalidIndexError when assigning rows + df = DataFrame(index=['a'], columns=['col1', 'col2']) + with pytest.raises(InvalidIndexError): + df.at['a'] = [123, 15] From bb5cfee20e86ed58256ab4ac4ff6e141fc784e5d Mon Sep 17 00:00:00 2001 From: Ambuj Pawar Date: Fri, 30 Sep 2022 17:37:02 +0200 Subject: [PATCH 2/8] fix precommit issues --- pandas/core/frame.py | 9 ++++++--- pandas/tests/indexing/test_at.py | 9 +++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 774067b9baa7a..89b022a425aa0 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -92,6 +92,7 @@ function as nv, np_percentile_argname, ) +from pandas.errors import InvalidIndexError from pandas.util._decorators import ( Appender, Substitution, @@ -222,7 +223,6 @@ console, format as fmt, ) -from pandas.errors import InvalidIndexError from pandas.io.formats.info import ( INFO_DOCSTRING, DataFrameInfo, @@ -4239,11 +4239,14 @@ def _set_value( else: self.loc[index, col] = value self._item_cache.pop(col, None) - + except InvalidIndexError: # GH48729: Seems like you are trying to assign a value to a # row when only scalar options are permitted - raise InvalidIndexError(f"You can only assign a scalar value not a {type(value)}") + raise InvalidIndexError( + f"You can only assign a scalar value not a {type(value)} " + "with value {value}" + ) def _ensure_valid_index(self, value) -> None: """ diff --git a/pandas/tests/indexing/test_at.py b/pandas/tests/indexing/test_at.py index 67509b7b2e417..c343cef4a6e3d 100644 --- a/pandas/tests/indexing/test_at.py +++ b/pandas/tests/indexing/test_at.py @@ -234,9 +234,10 @@ def test_at_categorical_integers(self): for key in [0, 1]: with pytest.raises(KeyError, match=str(key)): df.at[key, key] - + def test_at_applied_for_rows(self): # GH#48729 .at should raise InvalidIndexError when assigning rows - df = DataFrame(index=['a'], columns=['col1', 'col2']) - with pytest.raises(InvalidIndexError): - df.at['a'] = [123, 15] + df = DataFrame(index=["a"], columns=["col1", "col2"]) + new_row = [123, 15] + with pytest.raises(InvalidIndexError, match=str(new_row)): + df.at["a"] = new_row From 394ee64ea1a001241d8a5cd94d7c073f6b789723 Mon Sep 17 00:00:00 2001 From: Ambuj Pawar Date: Sat, 8 Oct 2022 11:22:29 +0200 Subject: [PATCH 3/8] Fix: Review comments --- pandas/core/frame.py | 2 +- pandas/tests/indexing/test_at.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 7ab7d429ff030..745f46d848cca 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4245,7 +4245,7 @@ def _set_value( # row when only scalar options are permitted raise InvalidIndexError( f"You can only assign a scalar value not a {type(value)} " - "with value {value}" + f"with value {value}" ) def _ensure_valid_index(self, value) -> None: diff --git a/pandas/tests/indexing/test_at.py b/pandas/tests/indexing/test_at.py index c343cef4a6e3d..ac694ed1764b4 100644 --- a/pandas/tests/indexing/test_at.py +++ b/pandas/tests/indexing/test_at.py @@ -239,5 +239,7 @@ def test_at_applied_for_rows(self): # GH#48729 .at should raise InvalidIndexError when assigning rows df = DataFrame(index=["a"], columns=["col1", "col2"]) new_row = [123, 15] - with pytest.raises(InvalidIndexError, match=str(new_row)): + with pytest.raises(InvalidIndexError, + match=f"You can only assign a scalar value not a \\{type(new_row)} with value \\{new_row}" + ): df.at["a"] = new_row From 5edd8d1855a507b86b32e63b42487ee704044119 Mon Sep 17 00:00:00 2001 From: Ambuj Pawar Date: Sat, 8 Oct 2022 11:36:39 +0200 Subject: [PATCH 4/8] Fix: precommit issues --- pandas/tests/indexing/test_at.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexing/test_at.py b/pandas/tests/indexing/test_at.py index ac694ed1764b4..399e212e80a1c 100644 --- a/pandas/tests/indexing/test_at.py +++ b/pandas/tests/indexing/test_at.py @@ -239,7 +239,9 @@ def test_at_applied_for_rows(self): # GH#48729 .at should raise InvalidIndexError when assigning rows df = DataFrame(index=["a"], columns=["col1", "col2"]) new_row = [123, 15] - with pytest.raises(InvalidIndexError, - match=f"You can only assign a scalar value not a \\{type(new_row)} with value \\{new_row}" - ): + with pytest.raises( + InvalidIndexError, + match=f"You can only assign a scalar value not a \\{type(new_row)} " + f"with value \\{new_row}", + ): df.at["a"] = new_row From 9db0412ee26c787c75da620065753960ba383579 Mon Sep 17 00:00:00 2001 From: Ambuj Pawar Date: Sun, 16 Oct 2022 20:38:56 +0200 Subject: [PATCH 5/8] Raise InvalidIndexError from original exception --- pandas/core/frame.py | 7 ++----- pandas/tests/indexing/test_at.py | 6 +----- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 745f46d848cca..75b1756bf876a 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4240,13 +4240,10 @@ def _set_value( self.loc[index, col] = value self._item_cache.pop(col, None) - except InvalidIndexError: + except InvalidIndexError as ii_err: # GH48729: Seems like you are trying to assign a value to a # row when only scalar options are permitted - raise InvalidIndexError( - f"You can only assign a scalar value not a {type(value)} " - f"with value {value}" - ) + raise InvalidIndexError(ii_err) def _ensure_valid_index(self, value) -> None: """ diff --git a/pandas/tests/indexing/test_at.py b/pandas/tests/indexing/test_at.py index 399e212e80a1c..2313eba96f867 100644 --- a/pandas/tests/indexing/test_at.py +++ b/pandas/tests/indexing/test_at.py @@ -239,9 +239,5 @@ def test_at_applied_for_rows(self): # GH#48729 .at should raise InvalidIndexError when assigning rows df = DataFrame(index=["a"], columns=["col1", "col2"]) new_row = [123, 15] - with pytest.raises( - InvalidIndexError, - match=f"You can only assign a scalar value not a \\{type(new_row)} " - f"with value \\{new_row}", - ): + with pytest.raises(InvalidIndexError, match=r"slice\(None, None, None\)"): df.at["a"] = new_row From 053814de6dd61a17aa358263824c6f9339de429d Mon Sep 17 00:00:00 2001 From: Ambuj Pawar Date: Mon, 17 Oct 2022 21:07:20 +0200 Subject: [PATCH 6/8] Raise InvalidIndex error from original error --- pandas/core/frame.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 4b4e58ff5dad5..728130cc93b89 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4242,7 +4242,9 @@ def _set_value( except InvalidIndexError as ii_err: # GH48729: Seems like you are trying to assign a value to a # row when only scalar options are permitted - raise InvalidIndexError(ii_err) + raise InvalidIndexError( + f"You can only assign a scalar value not a {type(value)}" + ) from ii_err def _ensure_valid_index(self, value) -> None: """ From 2c77219c9d984a26ee52a4634537772ae85b4a62 Mon Sep 17 00:00:00 2001 From: Ambuj Pawar Date: Mon, 17 Oct 2022 22:13:06 +0200 Subject: [PATCH 7/8] fix failing tests --- pandas/tests/indexing/test_at.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexing/test_at.py b/pandas/tests/indexing/test_at.py index 2313eba96f867..a3790f9b7d870 100644 --- a/pandas/tests/indexing/test_at.py +++ b/pandas/tests/indexing/test_at.py @@ -211,8 +211,13 @@ def test_at_frame_raises_key_error2(self, indexer_al): def test_at_frame_multiple_columns(self): # GH#48296 - at shouldn't modify multiple columns df = DataFrame({"a": [1, 2], "b": [3, 4]}) - with pytest.raises(InvalidIndexError, match=r"slice\(None, None, None\)"): - df.at[5] = [6, 7] + new_row = [6, 7] + with pytest.raises( + InvalidIndexError, + match=r"You can only assign a scalar value not a \\{type(new_row)} " + f"with value \\{new_row}", + ): + df.at[5] = new_row def test_at_getitem_mixed_index_no_fallback(self): # GH#19860 @@ -239,5 +244,9 @@ def test_at_applied_for_rows(self): # GH#48729 .at should raise InvalidIndexError when assigning rows df = DataFrame(index=["a"], columns=["col1", "col2"]) new_row = [123, 15] - with pytest.raises(InvalidIndexError, match=r"slice\(None, None, None\)"): + with pytest.raises( + InvalidIndexError, + match=r"You can only assign a scalar value not a \\{type(new_row)} " + f"with value \\{new_row}", + ): df.at["a"] = new_row From 38cba1f35893aa7eb02632421e19f5b0a91b8e63 Mon Sep 17 00:00:00 2001 From: Ambuj Pawar Date: Tue, 18 Oct 2022 23:10:35 +0200 Subject: [PATCH 8/8] fix failing tests --- pandas/tests/indexing/test_at.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/tests/indexing/test_at.py b/pandas/tests/indexing/test_at.py index a3790f9b7d870..9df6cb640257e 100644 --- a/pandas/tests/indexing/test_at.py +++ b/pandas/tests/indexing/test_at.py @@ -214,8 +214,7 @@ def test_at_frame_multiple_columns(self): new_row = [6, 7] with pytest.raises( InvalidIndexError, - match=r"You can only assign a scalar value not a \\{type(new_row)} " - f"with value \\{new_row}", + match=f"You can only assign a scalar value not a \\{type(new_row)}", ): df.at[5] = new_row @@ -246,7 +245,6 @@ def test_at_applied_for_rows(self): new_row = [123, 15] with pytest.raises( InvalidIndexError, - match=r"You can only assign a scalar value not a \\{type(new_row)} " - f"with value \\{new_row}", + match=f"You can only assign a scalar value not a \\{type(new_row)}", ): df.at["a"] = new_row