From 1e81304bd0d9088b627a58f1c01a345f2c794958 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Sat, 16 Mar 2024 18:38:01 -0500 Subject: [PATCH 1/6] DEP: Deprecate CoW and no_silent_downcasting options --- doc/source/whatsnew/v3.0.0.rst | 3 ++- pandas/__init__.py | 13 ++++++++++ pandas/core/config_init.py | 18 +++++++++++++ .../copy_view/test_option_deprecation.py | 25 +++++++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 pandas/tests/copy_view/test_option_deprecation.py diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index aceef7a5d6923..98af4572378f8 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -182,7 +182,8 @@ Other Deprecations - Deprecated :meth:`Timestamp.utcnow`, use ``Timestamp.now("UTC")`` instead (:issue:`56680`) - Deprecated allowing non-keyword arguments in :meth:`Series.to_markdown` except ``buf``. (:issue:`57280`) - Deprecated allowing non-keyword arguments in :meth:`Series.to_string` except ``buf``. (:issue:`57280`) -- +- Deprecated the option ``future.no_silent_downcasting``. The opt-in behavior from the 2.x release series is already the default and this option has no effect anymore (:issue:`57872`) +- Deprecated the option ``mode.copy_on_write``. Copy-on-Write is already the default and this option has no effect anymore (:issue:`57872`) .. --------------------------------------------------------------------------- .. _whatsnew_300.prior_deprecations: diff --git a/pandas/__init__.py b/pandas/__init__.py index f7ae91dd847f7..36cdb1d55f574 100644 --- a/pandas/__init__.py +++ b/pandas/__init__.py @@ -2,6 +2,9 @@ __docformat__ = "restructuredtext" +import os +import warnings + # Let users know if they're missing any of our hard dependencies _hard_dependencies = ("numpy", "pytz", "dateutil") _missing_dependencies = [] @@ -189,6 +192,16 @@ del get_versions, v +if "PANDAS_COPY_ON_WRITE" in os.environ: + warnings.warn( + "The env variable PANDAS_COPY_ON_WRITE is set. The copy_on_write option is " + "deprecated and will be removed in a future version. Copy-on-Write " + "is already enabled by default.", + FutureWarning, + stacklevel=2, + ) + + # module level doc-string __doc__ = """ pandas - a powerful data analysis and manipulation library for Python diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py index 46c9139c3456c..b2fb20c550656 100644 --- a/pandas/core/config_init.py +++ b/pandas/core/config_init.py @@ -875,3 +875,21 @@ def register_converter_cb(key: str) -> None: "(at which point this option will be deprecated).", validator=is_one_of_factory([True, False]), ) + + +cf.deprecate_option( + # GH#55043 + "mode.copy_on_write", + "copy_on_write option is deprecated and will be removed in a future " + "version. Copy-on-Write is already enabled; this option has no effect " + "anymore.", +) + + +cf.deprecate_option( + # GH#55043 + "future.no_silent_downcasting", + "no_silent_downcasting option is deprecated and will be removed in a future " + "version. The deprecation is enforced and this option doesn't have any effect " + "anymore.", +) diff --git a/pandas/tests/copy_view/test_option_deprecation.py b/pandas/tests/copy_view/test_option_deprecation.py new file mode 100644 index 0000000000000..fe2c3c6abaccb --- /dev/null +++ b/pandas/tests/copy_view/test_option_deprecation.py @@ -0,0 +1,25 @@ +import importlib + +import pandas as pd +import pandas._testing as tm + + +def test_configs_deprecated(): + # GH#57872 + with tm.assert_produces_warning(FutureWarning, match="is deprecated"): + pd.options.mode.copy_on_write = True + + with tm.assert_produces_warning(FutureWarning, match="is deprecated"): + pd.options.future.no_silent_downcasting = True + + +def test_env_variable(): + # GH#57872 + import os + + os.environ["PANDAS_COPY_ON_WRITE"] = "1" + with tm.assert_produces_warning( + FutureWarning, match="deprecated", check_stacklevel=False + ): + importlib.reload(pd) + os.environ.pop("PANDAS_COPY_ON_WRITE") From 1aedba6c7568932f660da6a4068897d2f1e81cb2 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Sat, 16 Mar 2024 19:40:20 -0500 Subject: [PATCH 2/6] Fixup --- pandas/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/__init__.py b/pandas/__init__.py index 36cdb1d55f574..2335c9e0131e7 100644 --- a/pandas/__init__.py +++ b/pandas/__init__.py @@ -201,6 +201,8 @@ stacklevel=2, ) +del os, warnings + # module level doc-string __doc__ = """ From 9fb068f8bfeb8cf4f7657d6db1ed7acc650c16f5 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Sun, 17 Mar 2024 19:33:58 -0500 Subject: [PATCH 3/6] Update --- doc/source/whatsnew/v1.5.0.rst | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 43aa63c284f38..d4c4899dfbf17 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -346,13 +346,19 @@ Without copy on write, the parent :class:`DataFrame` is updated when updating a With copy on write enabled, df won't be updated anymore: -.. ipython:: python +.. code-block:: python - with pd.option_context("mode.copy_on_write", True): - df = pd.DataFrame({"foo": [1, 2, 3], "bar": 1}) - view = df["foo"] - view.iloc[0] - df + In [2]: with pd.option_context("mode.copy_on_write", True): + ...: df = pd.DataFrame({"foo": [1, 2, 3], "bar": 1}) + ...: view = df["foo"] + ...: view.iloc[0] + ...: df + + Out[3]: + foo bar + 0 1 1 + 1 2 1 + 2 3 1 A more detailed explanation can be found `here `_. From 75cb981d5d7b6db997e8a6bde437275b7a5c081c Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Sun, 17 Mar 2024 19:35:19 -0500 Subject: [PATCH 4/6] Fixup --- pandas/errors/__init__.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pandas/errors/__init__.py b/pandas/errors/__init__.py index 402bbdb872a18..8ef190bbdcd39 100644 --- a/pandas/errors/__init__.py +++ b/pandas/errors/__init__.py @@ -434,11 +434,9 @@ class ChainedAssignmentError(Warning): Examples -------- - >>> pd.options.mode.copy_on_write = True >>> df = pd.DataFrame({"A": [1, 1, 1, 2, 2]}, columns=["A"]) >>> df["A"][0:3] = 10 # doctest: +SKIP ... # ChainedAssignmentError: ... - >>> pd.options.mode.copy_on_write = False """ From 655db8548679c95865cc857aa2c9bdfeda4d8f6b Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Tue, 26 Mar 2024 19:39:21 -0500 Subject: [PATCH 5/6] Fixup --- pandas/tests/strings/test_api.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/tests/strings/test_api.py b/pandas/tests/strings/test_api.py index ff8c6a98e1819..194df11a6deb6 100644 --- a/pandas/tests/strings/test_api.py +++ b/pandas/tests/strings/test_api.py @@ -8,7 +8,6 @@ MultiIndex, Series, _testing as tm, - option_context, ) from pandas.core.strings.accessor import StringMethods @@ -164,8 +163,7 @@ def test_api_per_method( if inferred_dtype in allowed_types: # xref GH 23555, GH 23556 - with option_context("future.no_silent_downcasting", True): - method(*args, **kwargs) # works! + method(*args, **kwargs) # works! else: # GH 23011, GH 23163 msg = ( From 46aa3c27e30e7578686105b3c7f6412dea310da9 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Tue, 26 Mar 2024 19:42:27 -0500 Subject: [PATCH 6/6] Update --- pandas/tests/copy_view/test_option_deprecation.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pandas/tests/copy_view/test_option_deprecation.py b/pandas/tests/copy_view/test_option_deprecation.py index fe2c3c6abaccb..9e790a6734126 100644 --- a/pandas/tests/copy_view/test_option_deprecation.py +++ b/pandas/tests/copy_view/test_option_deprecation.py @@ -1,5 +1,7 @@ import importlib +import pytest + import pandas as pd import pandas._testing as tm @@ -15,11 +17,8 @@ def test_configs_deprecated(): def test_env_variable(): # GH#57872 - import os - - os.environ["PANDAS_COPY_ON_WRITE"] = "1" + pytest.MonkeyPatch().setenv("PANDAS_COPY_ON_WRITE", "1") with tm.assert_produces_warning( FutureWarning, match="deprecated", check_stacklevel=False ): importlib.reload(pd) - os.environ.pop("PANDAS_COPY_ON_WRITE")