From 1c4c62710191436f119967f277e9fcf0d326f8db Mon Sep 17 00:00:00 2001 From: luke <2736230899@qq.com> Date: Tue, 14 Mar 2023 18:37:18 +0800 Subject: [PATCH 01/13] DEPR: DataFrame.swapaxes --- pandas/core/generic.py | 9 +++++++++ pandas/tests/copy_view/test_methods.py | 14 +++++++------ pandas/tests/extension/base/dim2.py | 5 +++-- pandas/tests/frame/methods/test_swapaxes.py | 22 ++++++++++++++------- pandas/tests/generic/test_finalize.py | 1 - 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 5b77bb9651073..372039fe7547f 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -741,6 +741,15 @@ def swapaxes( ------- same as input """ + warnings.warn( + # GH#51946 + f"'{type(self).__name__}.swapaxes' is deprecated and \ + will be removed in a future version. \ + Please use '{type(self).__name__}.transpose' instead.", + FutureWarning, + stacklevel=find_stack_level(), + ) + i = self._get_axis_number(axis1) j = self._get_axis_number(axis2) diff --git a/pandas/tests/copy_view/test_methods.py b/pandas/tests/copy_view/test_methods.py index 00ac06295572f..9a41d288df9f4 100644 --- a/pandas/tests/copy_view/test_methods.py +++ b/pandas/tests/copy_view/test_methods.py @@ -72,7 +72,7 @@ def test_copy_shallow(using_copy_on_write): lambda df, copy: df.rename_axis(columns="test", copy=copy), lambda df, copy: df.astype({"b": "int64"}, copy=copy), # lambda df, copy: df.swaplevel(0, 0, copy=copy), - lambda df, copy: df.swapaxes(0, 0, copy=copy), + # lambda df, copy: df.swapaxes(0, 0, copy=copy), lambda df, copy: df.truncate(0, 5, copy=copy), lambda df, copy: df.infer_objects(copy=copy), lambda df, copy: df.to_timestamp(copy=copy), @@ -91,7 +91,6 @@ def test_copy_shallow(using_copy_on_write): "rename_axis1", "astype", # "swaplevel", # only series - "swapaxes", "truncate", "infer_objects", "to_timestamp", @@ -142,7 +141,7 @@ def test_methods_copy_keyword( lambda ser, copy: ser.rename_axis(index="test", copy=copy), lambda ser, copy: ser.astype("int64", copy=copy), lambda ser, copy: ser.swaplevel(0, 1, copy=copy), - lambda ser, copy: ser.swapaxes(0, 0, copy=copy), + # lambda ser, copy: ser.swapaxes(0, 0, copy=copy), lambda ser, copy: ser.truncate(0, 5, copy=copy), lambda ser, copy: ser.infer_objects(copy=copy), lambda ser, copy: ser.to_timestamp(copy=copy), @@ -160,7 +159,6 @@ def test_methods_copy_keyword( "rename_axis0", "astype", "swaplevel", - "swapaxes", "truncate", "infer_objects", "to_timestamp", @@ -610,7 +608,9 @@ def test_to_frame(using_copy_on_write): def test_swapaxes_noop(using_copy_on_write, ax): df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) df_orig = df.copy() - df2 = df.swapaxes(ax, ax) + msg = "'DataFrame.swapaxes' is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + df2 = df.swapaxes(ax, ax) if using_copy_on_write: assert np.shares_memory(get_array(df2, "a"), get_array(df, "a")) @@ -627,7 +627,9 @@ def test_swapaxes_noop(using_copy_on_write, ax): def test_swapaxes_single_block(using_copy_on_write): df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}, index=["x", "y", "z"]) df_orig = df.copy() - df2 = df.swapaxes("index", "columns") + msg = "'DataFrame.swapaxes' is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + df2 = df.swapaxes("index", "columns") if using_copy_on_write: assert np.shares_memory(get_array(df2, "x"), get_array(df, "a")) diff --git a/pandas/tests/extension/base/dim2.py b/pandas/tests/extension/base/dim2.py index 6371411f9992c..5a8ab03599c5e 100644 --- a/pandas/tests/extension/base/dim2.py +++ b/pandas/tests/extension/base/dim2.py @@ -37,8 +37,9 @@ def test_frame_from_2d_array(self, data): def test_swapaxes(self, data): arr2d = data.repeat(2).reshape(-1, 2) - - result = arr2d.swapaxes(0, 1) + msg = "'DataFrame.swapaxes' is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + result = arr2d.swapaxes(0, 1) expected = arr2d.T self.assert_extension_array_equal(result, expected) diff --git a/pandas/tests/frame/methods/test_swapaxes.py b/pandas/tests/frame/methods/test_swapaxes.py index 5da2c2292f137..f2667fc973cf4 100644 --- a/pandas/tests/frame/methods/test_swapaxes.py +++ b/pandas/tests/frame/methods/test_swapaxes.py @@ -8,22 +8,30 @@ class TestSwapAxes: def test_swapaxes(self): df = DataFrame(np.random.randn(10, 5)) - tm.assert_frame_equal(df.T, df.swapaxes(0, 1)) - tm.assert_frame_equal(df.T, df.swapaxes(1, 0)) + msg = "'DataFrame.swapaxes' is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + tm.assert_frame_equal(df.T, df.swapaxes(0, 1)) + tm.assert_frame_equal(df.T, df.swapaxes(1, 0)) def test_swapaxes_noop(self): df = DataFrame(np.random.randn(10, 5)) - tm.assert_frame_equal(df, df.swapaxes(0, 0)) + msg = "'DataFrame.swapaxes' is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + tm.assert_frame_equal(df, df.swapaxes(0, 0)) def test_swapaxes_invalid_axis(self): df = DataFrame(np.random.randn(10, 5)) - msg = "No axis named 2 for object type DataFrame" - with pytest.raises(ValueError, match=msg): - df.swapaxes(2, 5) + msg = "'DataFrame.swapaxes' is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + msg = "No axis named 2 for object type DataFrame" + with pytest.raises(ValueError, match=msg): + df.swapaxes(2, 5) def test_round_empty_not_input(self): # GH#51032 df = DataFrame({"a": [1, 2]}) - result = df.swapaxes("index", "index") + msg = "'DataFrame.swapaxes' is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + result = df.swapaxes("index", "index") tm.assert_frame_equal(df, result) assert df is not result diff --git a/pandas/tests/generic/test_finalize.py b/pandas/tests/generic/test_finalize.py index d85de12566fb6..cdab112e7ad86 100644 --- a/pandas/tests/generic/test_finalize.py +++ b/pandas/tests/generic/test_finalize.py @@ -251,7 +251,6 @@ operator.methodcaller("isin", pd.DataFrame({"A": [1]})), ), ), - (pd.DataFrame, frame_data, operator.methodcaller("swapaxes", 0, 1)), (pd.DataFrame, frame_mi_data, operator.methodcaller("droplevel", "A")), (pd.DataFrame, frame_data, operator.methodcaller("pop", "A")), pytest.param( From 7dd96b4e9334cb7ffa7078aa6627b2f54bccfb37 Mon Sep 17 00:00:00 2001 From: luke <2736230899@qq.com> Date: Tue, 14 Mar 2023 18:52:45 +0800 Subject: [PATCH 02/13] add whatsnew --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 410a324be829e..a685ee12a745d 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -100,7 +100,7 @@ Deprecations - Deprecated ``axis=1`` in :meth:`DataFrame.groupby` and in :class:`Grouper` constructor, do ``frame.T.groupby(...)`` instead (:issue:`51203`) - Deprecated passing a :class:`DataFrame` to :meth:`DataFrame.from_records`, use :meth:`DataFrame.set_index` or :meth:`DataFrame.drop` instead (:issue:`51353`) - Deprecated accepting slices in :meth:`DataFrame.take`, call ``obj[slicer]`` or pass a sequence of integers instead (:issue:`51539`) -- +- Deprecated :meth:`DadaFrame.swapaxes`, use :meth:`DataFrame.transpose` instead (:issue:`51946`) .. --------------------------------------------------------------------------- .. _whatsnew_210.performance: From 158961b8a5083aa6149856546f4e5bc8584910f2 Mon Sep 17 00:00:00 2001 From: luke <2736230899@qq.com> Date: Tue, 14 Mar 2023 19:25:37 +0800 Subject: [PATCH 03/13] improve and fix ci --- pandas/tests/copy_view/test_methods.py | 24 ++++++++++++++++++++---- pandas/tests/extension/base/dim2.py | 4 +--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/pandas/tests/copy_view/test_methods.py b/pandas/tests/copy_view/test_methods.py index e9037d2888246..2578772c687f3 100644 --- a/pandas/tests/copy_view/test_methods.py +++ b/pandas/tests/copy_view/test_methods.py @@ -1,3 +1,5 @@ +import inspect + import numpy as np import pytest @@ -72,7 +74,7 @@ def test_copy_shallow(using_copy_on_write): lambda df, copy: df.rename_axis(columns="test", copy=copy), lambda df, copy: df.astype({"b": "int64"}, copy=copy), # lambda df, copy: df.swaplevel(0, 0, copy=copy), - # lambda df, copy: df.swapaxes(0, 0, copy=copy), + lambda df, copy: df.swapaxes(0, 0, copy=copy), lambda df, copy: df.truncate(0, 5, copy=copy), lambda df, copy: df.infer_objects(copy=copy), lambda df, copy: df.to_timestamp(copy=copy), @@ -91,6 +93,7 @@ def test_copy_shallow(using_copy_on_write): "rename_axis1", "astype", # "swaplevel", # only series + "swapaxes", "truncate", "infer_objects", "to_timestamp", @@ -114,7 +117,13 @@ def test_methods_copy_keyword( index = date_range("2012-01-01", freq="D", periods=3, tz="Europe/Brussels") df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]}, index=index) - df2 = method(df, copy=copy) + + if "swapaxes" in inspect.getsource(method): + msg = "'DataFrame.swapaxes' is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + df2 = method(df, copy=copy) + else: + df2 = method(df, copy=copy) share_memory = using_copy_on_write or copy is False @@ -141,7 +150,7 @@ def test_methods_copy_keyword( lambda ser, copy: ser.rename_axis(index="test", copy=copy), lambda ser, copy: ser.astype("int64", copy=copy), lambda ser, copy: ser.swaplevel(0, 1, copy=copy), - # lambda ser, copy: ser.swapaxes(0, 0, copy=copy), + lambda ser, copy: ser.swapaxes(0, 0, copy=copy), lambda ser, copy: ser.truncate(0, 5, copy=copy), lambda ser, copy: ser.infer_objects(copy=copy), lambda ser, copy: ser.to_timestamp(copy=copy), @@ -159,6 +168,7 @@ def test_methods_copy_keyword( "rename_axis0", "astype", "swaplevel", + "swapaxes", "truncate", "infer_objects", "to_timestamp", @@ -182,7 +192,13 @@ def test_methods_series_copy_keyword(request, method, copy, using_copy_on_write) index = MultiIndex.from_arrays([[1, 2, 3], [4, 5, 6]]) ser = Series([1, 2, 3], index=index) - ser2 = method(ser, copy=copy) + + if "swapaxes" in inspect.getsource(method): + msg = "'Series.swapaxes' is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + ser2 = method(ser, copy=copy) + else: + ser2 = method(ser, copy=copy) share_memory = using_copy_on_write or copy is False diff --git a/pandas/tests/extension/base/dim2.py b/pandas/tests/extension/base/dim2.py index 5a8ab03599c5e..0542948017488 100644 --- a/pandas/tests/extension/base/dim2.py +++ b/pandas/tests/extension/base/dim2.py @@ -37,9 +37,7 @@ def test_frame_from_2d_array(self, data): def test_swapaxes(self, data): arr2d = data.repeat(2).reshape(-1, 2) - msg = "'DataFrame.swapaxes' is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): - result = arr2d.swapaxes(0, 1) + result = arr2d.swapaxes(0, 1) expected = arr2d.T self.assert_extension_array_equal(result, expected) From 9890bf9b22632ad8af867b9407a3c039cc8667e5 Mon Sep 17 00:00:00 2001 From: luke <2736230899@qq.com> Date: Tue, 14 Mar 2023 19:28:37 +0800 Subject: [PATCH 04/13] improve whatsnew --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index a685ee12a745d..943f1e21e3f3e 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -100,7 +100,7 @@ Deprecations - Deprecated ``axis=1`` in :meth:`DataFrame.groupby` and in :class:`Grouper` constructor, do ``frame.T.groupby(...)`` instead (:issue:`51203`) - Deprecated passing a :class:`DataFrame` to :meth:`DataFrame.from_records`, use :meth:`DataFrame.set_index` or :meth:`DataFrame.drop` instead (:issue:`51353`) - Deprecated accepting slices in :meth:`DataFrame.take`, call ``obj[slicer]`` or pass a sequence of integers instead (:issue:`51539`) -- Deprecated :meth:`DadaFrame.swapaxes`, use :meth:`DataFrame.transpose` instead (:issue:`51946`) +- Deprecated :meth:`DadaFrame.swapaxes` and :meth:`Series.swapaxes`, use :meth:`DataFrame.transpose` or :meth:`Series.transpose` instead (:issue:`51946`) .. --------------------------------------------------------------------------- .. _whatsnew_210.performance: From 5b6046094a35d94f8be5ec5bbe490e74836eb009 Mon Sep 17 00:00:00 2001 From: luke <2736230899@qq.com> Date: Tue, 14 Mar 2023 19:32:18 +0800 Subject: [PATCH 05/13] improve typo and blank line --- doc/source/whatsnew/v2.1.0.rst | 2 +- pandas/tests/extension/base/dim2.py | 1 + pandas/tests/generic/test_finalize.py | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 943f1e21e3f3e..dd5123c42a46d 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -100,7 +100,7 @@ Deprecations - Deprecated ``axis=1`` in :meth:`DataFrame.groupby` and in :class:`Grouper` constructor, do ``frame.T.groupby(...)`` instead (:issue:`51203`) - Deprecated passing a :class:`DataFrame` to :meth:`DataFrame.from_records`, use :meth:`DataFrame.set_index` or :meth:`DataFrame.drop` instead (:issue:`51353`) - Deprecated accepting slices in :meth:`DataFrame.take`, call ``obj[slicer]`` or pass a sequence of integers instead (:issue:`51539`) -- Deprecated :meth:`DadaFrame.swapaxes` and :meth:`Series.swapaxes`, use :meth:`DataFrame.transpose` or :meth:`Series.transpose` instead (:issue:`51946`) +- Deprecated :meth:`DataFrame.swapaxes` and :meth:`Series.swapaxes`, use :meth:`DataFrame.transpose` or :meth:`Series.transpose` instead (:issue:`51946`) .. --------------------------------------------------------------------------- .. _whatsnew_210.performance: diff --git a/pandas/tests/extension/base/dim2.py b/pandas/tests/extension/base/dim2.py index 0542948017488..6371411f9992c 100644 --- a/pandas/tests/extension/base/dim2.py +++ b/pandas/tests/extension/base/dim2.py @@ -37,6 +37,7 @@ def test_frame_from_2d_array(self, data): def test_swapaxes(self, data): arr2d = data.repeat(2).reshape(-1, 2) + result = arr2d.swapaxes(0, 1) expected = arr2d.T self.assert_extension_array_equal(result, expected) diff --git a/pandas/tests/generic/test_finalize.py b/pandas/tests/generic/test_finalize.py index cdab112e7ad86..0f688675ae441 100644 --- a/pandas/tests/generic/test_finalize.py +++ b/pandas/tests/generic/test_finalize.py @@ -252,6 +252,7 @@ ), ), (pd.DataFrame, frame_mi_data, operator.methodcaller("droplevel", "A")), + (pd.DataFrame, frame_data, operator.methodcaller("swapaxes", 0, 1)), (pd.DataFrame, frame_data, operator.methodcaller("pop", "A")), pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("squeeze")), From 3595fb479e1314c52514b767c95f86f5aba8c8f3 Mon Sep 17 00:00:00 2001 From: luke <2736230899@qq.com> Date: Tue, 14 Mar 2023 19:33:43 +0800 Subject: [PATCH 06/13] fix layout --- pandas/tests/generic/test_finalize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/generic/test_finalize.py b/pandas/tests/generic/test_finalize.py index 0f688675ae441..d85de12566fb6 100644 --- a/pandas/tests/generic/test_finalize.py +++ b/pandas/tests/generic/test_finalize.py @@ -251,8 +251,8 @@ operator.methodcaller("isin", pd.DataFrame({"A": [1]})), ), ), - (pd.DataFrame, frame_mi_data, operator.methodcaller("droplevel", "A")), (pd.DataFrame, frame_data, operator.methodcaller("swapaxes", 0, 1)), + (pd.DataFrame, frame_mi_data, operator.methodcaller("droplevel", "A")), (pd.DataFrame, frame_data, operator.methodcaller("pop", "A")), pytest.param( (pd.DataFrame, frame_data, operator.methodcaller("squeeze")), From 532ee3adc826de806ef6742665bcb02cf9f500e1 Mon Sep 17 00:00:00 2001 From: luke <2736230899@qq.com> Date: Wed, 15 Mar 2023 08:32:23 +0800 Subject: [PATCH 07/13] delete swapaxes in test_finalize --- pandas/tests/generic/test_finalize.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/generic/test_finalize.py b/pandas/tests/generic/test_finalize.py index d85de12566fb6..cdab112e7ad86 100644 --- a/pandas/tests/generic/test_finalize.py +++ b/pandas/tests/generic/test_finalize.py @@ -251,7 +251,6 @@ operator.methodcaller("isin", pd.DataFrame({"A": [1]})), ), ), - (pd.DataFrame, frame_data, operator.methodcaller("swapaxes", 0, 1)), (pd.DataFrame, frame_mi_data, operator.methodcaller("droplevel", "A")), (pd.DataFrame, frame_data, operator.methodcaller("pop", "A")), pytest.param( From 3c027ec0b46e15efacde0af0d240f6130c3fb9d1 Mon Sep 17 00:00:00 2001 From: luke <2736230899@qq.com> Date: Wed, 15 Mar 2023 10:54:46 +0800 Subject: [PATCH 08/13] fix ci --- pandas/tests/copy_view/test_methods.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/tests/copy_view/test_methods.py b/pandas/tests/copy_view/test_methods.py index d8f6c7e5275b1..492e0b4b2eb0b 100644 --- a/pandas/tests/copy_view/test_methods.py +++ b/pandas/tests/copy_view/test_methods.py @@ -661,7 +661,9 @@ def test_swapaxes_single_block(using_copy_on_write): def test_swapaxes_read_only_array(): df = DataFrame({"a": [1, 2], "b": 3}) - df = df.swapaxes(axis1="index", axis2="columns") + msg = "'DataFrame.swapaxes' is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + df = df.swapaxes(axis1="index", axis2="columns") df.iloc[0, 0] = 100 expected = DataFrame({0: [100, 3], 1: [2, 3]}, index=["a", "b"]) tm.assert_frame_equal(df, expected) From a052f5e0889818e05409f90f22bd6faaca57c385 Mon Sep 17 00:00:00 2001 From: luke <2736230899@qq.com> Date: Thu, 16 Mar 2023 11:07:46 +0800 Subject: [PATCH 09/13] fix ci --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 756556a433035..cfd47cd007374 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -103,7 +103,7 @@ Deprecations - Deprecated accepting slices in :meth:`DataFrame.take`, call ``obj[slicer]`` or pass a sequence of integers instead (:issue:`51539`) - Deprecated 'method', 'limit', and 'fill_axis' keywords in :meth:`DataFrame.align` and :meth:`Series.align`, explicitly call ``fillna`` on the alignment results instead (:issue:`51856`) - Deprecated :meth:`DataFrame.swapaxes` and :meth:`Series.swapaxes`, use :meth:`DataFrame.transpose` or :meth:`Series.transpose` instead (:issue:`51946`) -- +- .. --------------------------------------------------------------------------- .. _whatsnew_210.performance: From 6647c6e4ffa42a88f84e886ad05c7c2e9de67daf Mon Sep 17 00:00:00 2001 From: luke <2736230899@qq.com> Date: Sun, 19 Mar 2023 21:02:58 +0800 Subject: [PATCH 10/13] fix msg --- pandas/core/generic.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 4b4786d68142d..0706d5ec6f01e 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -743,9 +743,9 @@ def swapaxes(self, axis1: Axis, axis2: Axis, copy: bool_t | None = None) -> Self """ warnings.warn( # GH#51946 - f"'{type(self).__name__}.swapaxes' is deprecated and \ - will be removed in a future version. \ - Please use '{type(self).__name__}.transpose' instead.", + f"'{type(self).__name__}.swapaxes' is deprecated and " + "will be removed in a future version. " + "Please use '{type(self).__name__}.transpose' instead.", FutureWarning, stacklevel=find_stack_level(), ) From d23f0dfd605585885af5018725e3aecbe6202329 Mon Sep 17 00:00:00 2001 From: luke <2736230899@qq.com> Date: Mon, 20 Mar 2023 10:37:02 +0800 Subject: [PATCH 11/13] fix ci numpydev --- pandas/tests/generic/test_generic.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pandas/tests/generic/test_generic.py b/pandas/tests/generic/test_generic.py index 74d8277c975e4..d23d97f43de03 100644 --- a/pandas/tests/generic/test_generic.py +++ b/pandas/tests/generic/test_generic.py @@ -230,8 +230,11 @@ def test_size_compat(self, frame_or_series): def test_split_compat(self, frame_or_series): # xref GH8846 o = construct(frame_or_series, shape=10) - assert len(np.array_split(o, 5)) == 5 - assert len(np.array_split(o, 2)) == 2 + with tm.assert_produces_warning( + FutureWarning, match=".swapaxes' is deprecated", check_stacklevel=False + ): + assert len(np.array_split(o, 5)) == 5 + assert len(np.array_split(o, 2)) == 2 # See gh-12301 def test_stat_unexpected_keyword(self, frame_or_series): From 09cf70aca5816981b4f4914c806a80d8a0bedbcf Mon Sep 17 00:00:00 2001 From: Zhengbo Wang <77875500+luke396@users.noreply.github.com> Date: Tue, 21 Mar 2023 08:25:30 +0800 Subject: [PATCH 12/13] Update pandas/core/generic.py Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- pandas/core/generic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 247fa15b09575..997f1b054037d 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -745,7 +745,7 @@ def swapaxes(self, axis1: Axis, axis2: Axis, copy: bool_t | None = None) -> Self # GH#51946 f"'{type(self).__name__}.swapaxes' is deprecated and " "will be removed in a future version. " - "Please use '{type(self).__name__}.transpose' instead.", + f"Please use '{type(self).__name__}.transpose' instead.", FutureWarning, stacklevel=find_stack_level(), ) From f1f661bd052c3a3294fcc502d5c2727c71f635d9 Mon Sep 17 00:00:00 2001 From: luke <2736230899@qq.com> Date: Tue, 21 Mar 2023 08:53:35 +0800 Subject: [PATCH 13/13] use request.node.callspec.id instead --- pandas/tests/copy_view/test_methods.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/tests/copy_view/test_methods.py b/pandas/tests/copy_view/test_methods.py index 3bb1cf5736faa..7b8bc35f016b1 100644 --- a/pandas/tests/copy_view/test_methods.py +++ b/pandas/tests/copy_view/test_methods.py @@ -1,5 +1,3 @@ -import inspect - import numpy as np import pytest @@ -118,7 +116,7 @@ def test_methods_copy_keyword( df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]}, index=index) - if "swapaxes" in inspect.getsource(method): + if "swapaxes" in request.node.callspec.id: msg = "'DataFrame.swapaxes' is deprecated" with tm.assert_produces_warning(FutureWarning, match=msg): df2 = method(df, copy=copy) @@ -193,7 +191,7 @@ def test_methods_series_copy_keyword(request, method, copy, using_copy_on_write) ser = Series([1, 2, 3], index=index) - if "swapaxes" in inspect.getsource(method): + if "swapaxes" in request.node.callspec.id: msg = "'Series.swapaxes' is deprecated" with tm.assert_produces_warning(FutureWarning, match=msg): ser2 = method(ser, copy=copy)