From 7d51e13676326b19cac400dc715793c576944944 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Thu, 8 Dec 2022 16:23:14 +0100 Subject: [PATCH 1/2] Backport PR #49771: PERF: Use fastpath for accessing option value in internals --- pandas/core/internals/managers.py | 7 ++++-- pandas/tests/copy_view/test_internals.py | 30 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index f55fcead61fae..9641cdc9999ed 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -15,7 +15,7 @@ import numpy as np -from pandas._config import get_option +from pandas._config.config import _global_config from pandas._libs import ( algos as libalgos, @@ -2424,5 +2424,8 @@ def _preprocess_slice_or_indexer( return "fancy", indexer, len(indexer) +_mode_options = _global_config["mode"] + + def _using_copy_on_write(): - return get_option("mode.copy_on_write") + return _mode_options["copy_on_write"] diff --git a/pandas/tests/copy_view/test_internals.py b/pandas/tests/copy_view/test_internals.py index edfa7f843f17f..7a2965f2e1c61 100644 --- a/pandas/tests/copy_view/test_internals.py +++ b/pandas/tests/copy_view/test_internals.py @@ -3,6 +3,7 @@ import pandas.util._test_decorators as td +import pandas as pd from pandas import DataFrame from pandas.tests.copy_view.util import get_array @@ -62,3 +63,32 @@ def test_clear_parent(using_copy_on_write): # when losing the last reference, also the parent should be reset subset["b"] = 0 assert subset._mgr.parent is None + + +@td.skip_array_manager_invalid_test +def test_switch_options(): + # ensure we can switch the value of the option within one session + # (assuming data is constructed after switching) + + # using the option_context to ensure we set back to global option value + # after running the test + with pd.option_context("mode.copy_on_write", False): + df = DataFrame({"a": [1, 2, 3], "b": [0.1, 0.2, 0.3]}) + subset = df[:] + subset.iloc[0, 0] = 0 + # df updated with CoW disabled + assert df.iloc[0, 0] == 0 + + pd.options.mode.copy_on_write = True + df = DataFrame({"a": [1, 2, 3], "b": [0.1, 0.2, 0.3]}) + subset = df[:] + subset.iloc[0, 0] = 0 + # df not updated with CoW enabled + assert df.iloc[0, 0] == 1 + + pd.options.mode.copy_on_write = False + df = DataFrame({"a": [1, 2, 3], "b": [0.1, 0.2, 0.3]}) + subset = df[:] + subset.iloc[0, 0] = 0 + # df updated with CoW disabled + assert df.iloc[0, 0] == 0 From a9920df1463e2cb4f2f8a7a2e0a31301a4a35441 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 13 Jan 2023 10:08:02 +0100 Subject: [PATCH 2/2] don't run options tests in parallel --- pandas/tests/copy_view/test_internals.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/copy_view/test_internals.py b/pandas/tests/copy_view/test_internals.py index 7a2965f2e1c61..1938a1c58fe3d 100644 --- a/pandas/tests/copy_view/test_internals.py +++ b/pandas/tests/copy_view/test_internals.py @@ -65,6 +65,7 @@ def test_clear_parent(using_copy_on_write): assert subset._mgr.parent is None +@pytest.mark.single_cpu @td.skip_array_manager_invalid_test def test_switch_options(): # ensure we can switch the value of the option within one session