From 1aa6a9611ac403caf1bad589dcb8624fb58b35e9 Mon Sep 17 00:00:00 2001 From: MarcoGorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Sun, 14 Jan 2024 09:28:56 +0000 Subject: [PATCH 1/4] wip --- pandas/core/indexing.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 0f892d4924933..faef4f919c9bd 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -877,7 +877,13 @@ def _ensure_listlike_indexer(self, key, axis=None, value=None) -> None: ) self.obj._mgr = new_mgr return - + if len(diff): + warnings.warn( + "Multi-column-wise row-wise expansion is deprecated and will be " + "removed in a future version. ", + FutureWarning, + stacklevel=find_stack_level(), + ) self.obj._mgr = self.obj._mgr.reindex_axis(keys, axis=0, only_slice=True) @final From a079db6ecec693175b9ff5d070327156dca8ea0a Mon Sep 17 00:00:00 2001 From: MarcoGorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Sun, 14 Jan 2024 10:00:44 +0000 Subject: [PATCH 2/4] catch warnings --- doc/source/whatsnew/v2.2.0.rst | 18 ++++++++++++++++++ pandas/core/indexing.py | 11 +++++++++-- pandas/tests/indexing/multiindex/test_loc.py | 10 +++++++++- pandas/tests/indexing/test_loc.py | 19 ++++++++++++++++--- 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v2.2.0.rst b/doc/source/whatsnew/v2.2.0.rst index 5de5bd58bd35f..a676afecb27c3 100644 --- a/doc/source/whatsnew/v2.2.0.rst +++ b/doc/source/whatsnew/v2.2.0.rst @@ -657,6 +657,24 @@ Set the following option to opt into the future behavior: In [9]: pd.set_option("future.no_silent_downcasting", True) +Deprecated list-like-column-wise enlargement with partial-row-wise enlargement +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Enlarging a dataframe with a partial row indexer and a list-like column indexer is deprecated. + +Instead of + +.. code-block:: python + + df.loc[idx, ['A', 'B']] = (val_1, val_2) + +please do + +.. code-block:: python + + df.loc[idx, 'A'] = val_1 + df.loc[idx, 'B'] = val_2 + Other Deprecations ^^^^^^^^^^^^^^^^^^ - Changed :meth:`Timedelta.resolution_string` to return ``h``, ``min``, ``s``, ``ms``, ``us``, and ``ns`` instead of ``H``, ``T``, ``S``, ``L``, ``U``, and ``N``, for compatibility with respective deprecations in frequency aliases (:issue:`52536`) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index faef4f919c9bd..0c3ae79b2a663 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -879,8 +879,15 @@ def _ensure_listlike_indexer(self, key, axis=None, value=None) -> None: return if len(diff): warnings.warn( - "Multi-column-wise row-wise expansion is deprecated and will be " - "removed in a future version. ", + ( + "list-like-column-wise enlargement with partial-row-wise " + "enlargement is deprecated.\n" + "Instead of\n" + " df.loc[idx, ['A', 'B']] = (val_1, val_2)\n" + "please do\n" + " df.loc[idx, 'A'] = val_1\n" + " df.loc[idx, 'B'] = val_2\n" + ), FutureWarning, stacklevel=find_stack_level(), ) diff --git a/pandas/tests/indexing/multiindex/test_loc.py b/pandas/tests/indexing/multiindex/test_loc.py index de7d644698f2c..346dd2383ca5d 100644 --- a/pandas/tests/indexing/multiindex/test_loc.py +++ b/pandas/tests/indexing/multiindex/test_loc.py @@ -377,7 +377,15 @@ def test_multiindex_setitem_columns_enlarging(self, indexer, exp_value): # GH#39147 mi = MultiIndex.from_tuples([(1, 2), (3, 4)]) df = DataFrame([[1, 2], [3, 4]], index=mi, columns=["a", "b"]) - df.loc[indexer, ["c", "d"]] = 1.0 + warn = FutureWarning if indexer != slice(None) else None + with tm.assert_produces_warning( + warn, + match=( + "list-like-column-wise enlargement with partial-row-wise enlargement " + "is deprecated" + ), + ): + df.loc[indexer, ["c", "d"]] = 1.0 expected = DataFrame( [[1, 2, 1.0, 1.0], [3, 4, exp_value, exp_value]], index=mi, diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index c446f2c44b745..f56b376976d51 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -940,8 +940,14 @@ def test_loc_setitem_with_scalar_index(self, indexer, value): def test_loc_setitem_missing_columns(self, index, box, expected): # GH 29334 df = DataFrame([[1, 2], [3, 4], [5, 6]], columns=["A", "B"]) - - df.loc[index] = box + warn = FutureWarning if index[0] != slice(None, None, None) else None + with tm.assert_produces_warning( + warn, + match=( + "list-like-column-wise enlargement with partial-row-wise enlargement" + ), + ): + df.loc[index] = box tm.assert_frame_equal(df, expected) def test_loc_coercion(self): @@ -3327,7 +3333,14 @@ def test_loc_setitem_dict_timedelta_multiple_set(self): def test_loc_set_multiple_items_in_multiple_new_columns(self): # GH 25594 df = DataFrame(index=[1, 2], columns=["a"]) - df.loc[1, ["b", "c"]] = [6, 7] + with tm.assert_produces_warning( + FutureWarning, + match=( + "list-like-column-wise enlargement with partial-row-wise enlargement " + "is deprecated" + ), + ): + df.loc[1, ["b", "c"]] = [6, 7] expected = DataFrame( { From b7b5737f94014f3d29d606cd8cf0547afbcf96d9 Mon Sep 17 00:00:00 2001 From: MarcoGorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Sun, 14 Jan 2024 13:03:52 +0000 Subject: [PATCH 3/4] reword --- doc/source/whatsnew/v2.2.0.rst | 4 ++-- pandas/core/indexing.py | 2 +- pandas/tests/indexing/multiindex/test_loc.py | 2 +- pandas/tests/indexing/test_loc.py | 6 ++---- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/doc/source/whatsnew/v2.2.0.rst b/doc/source/whatsnew/v2.2.0.rst index a676afecb27c3..d5f3863be610a 100644 --- a/doc/source/whatsnew/v2.2.0.rst +++ b/doc/source/whatsnew/v2.2.0.rst @@ -657,8 +657,8 @@ Set the following option to opt into the future behavior: In [9]: pd.set_option("future.no_silent_downcasting", True) -Deprecated list-like-column-wise enlargement with partial-row-wise enlargement -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +Deprecated list-like-column-wise enlargement with partial row indexer +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Enlarging a dataframe with a partial row indexer and a list-like column indexer is deprecated. diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 0c3ae79b2a663..8a66f94ebbe10 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -881,7 +881,7 @@ def _ensure_listlike_indexer(self, key, axis=None, value=None) -> None: warnings.warn( ( "list-like-column-wise enlargement with partial-row-wise " - "enlargement is deprecated.\n" + "indexer is deprecated.\n" "Instead of\n" " df.loc[idx, ['A', 'B']] = (val_1, val_2)\n" "please do\n" diff --git a/pandas/tests/indexing/multiindex/test_loc.py b/pandas/tests/indexing/multiindex/test_loc.py index 346dd2383ca5d..8e4d7991d6f7d 100644 --- a/pandas/tests/indexing/multiindex/test_loc.py +++ b/pandas/tests/indexing/multiindex/test_loc.py @@ -381,7 +381,7 @@ def test_multiindex_setitem_columns_enlarging(self, indexer, exp_value): with tm.assert_produces_warning( warn, match=( - "list-like-column-wise enlargement with partial-row-wise enlargement " + "list-like-column-wise enlargement with partial-row-wise indexer " "is deprecated" ), ): diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index f56b376976d51..e40066a51db97 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -943,9 +943,7 @@ def test_loc_setitem_missing_columns(self, index, box, expected): warn = FutureWarning if index[0] != slice(None, None, None) else None with tm.assert_produces_warning( warn, - match=( - "list-like-column-wise enlargement with partial-row-wise enlargement" - ), + match=("list-like-column-wise enlargement with partial-row-wise indexer"), ): df.loc[index] = box tm.assert_frame_equal(df, expected) @@ -3336,7 +3334,7 @@ def test_loc_set_multiple_items_in_multiple_new_columns(self): with tm.assert_produces_warning( FutureWarning, match=( - "list-like-column-wise enlargement with partial-row-wise enlargement " + "list-like-column-wise enlargement with partial-row-wise indexer " "is deprecated" ), ): From 5fde1f72296e1343ce02fbc97e247f2e219df6d7 Mon Sep 17 00:00:00 2001 From: MarcoGorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Sun, 14 Jan 2024 14:45:22 +0000 Subject: [PATCH 4/4] note A and B not already in df --- doc/source/whatsnew/v2.2.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.2.0.rst b/doc/source/whatsnew/v2.2.0.rst index d5f3863be610a..f92a9b71a2781 100644 --- a/doc/source/whatsnew/v2.2.0.rst +++ b/doc/source/whatsnew/v2.2.0.rst @@ -662,7 +662,7 @@ Deprecated list-like-column-wise enlargement with partial row indexer Enlarging a dataframe with a partial row indexer and a list-like column indexer is deprecated. -Instead of +Instead of (suppose ``'A'`` and ``'B'`` are not already columns in ``df``) .. code-block:: python