Skip to content

Commit 70a701d

Browse files
meeseeksmachinejorisvandenbosschephofl
authored
Backport PR #49771 on branch 1.5.x (PERF: Use fastpath for accessing option value in internals) (#50128)
Co-authored-by: Joris Van den Bossche <[email protected]> Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 23a2699 commit 70a701d

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

pandas/core/internals/managers.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import numpy as np
1717

18-
from pandas._config import get_option
18+
from pandas._config.config import _global_config
1919

2020
from pandas._libs import (
2121
algos as libalgos,
@@ -2427,5 +2427,8 @@ def _preprocess_slice_or_indexer(
24272427
return "fancy", indexer, len(indexer)
24282428

24292429

2430+
_mode_options = _global_config["mode"]
2431+
2432+
24302433
def _using_copy_on_write():
2431-
return get_option("mode.copy_on_write")
2434+
return _mode_options["copy_on_write"]

pandas/tests/copy_view/test_internals.py

+31
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import pandas.util._test_decorators as td
55

6+
import pandas as pd
67
from pandas import DataFrame
78
from pandas.tests.copy_view.util import get_array
89

@@ -62,3 +63,33 @@ def test_clear_parent(using_copy_on_write):
6263
# when losing the last reference, also the parent should be reset
6364
subset["b"] = 0
6465
assert subset._mgr.parent is None
66+
67+
68+
@pytest.mark.single_cpu
69+
@td.skip_array_manager_invalid_test
70+
def test_switch_options():
71+
# ensure we can switch the value of the option within one session
72+
# (assuming data is constructed after switching)
73+
74+
# using the option_context to ensure we set back to global option value
75+
# after running the test
76+
with pd.option_context("mode.copy_on_write", False):
77+
df = DataFrame({"a": [1, 2, 3], "b": [0.1, 0.2, 0.3]})
78+
subset = df[:]
79+
subset.iloc[0, 0] = 0
80+
# df updated with CoW disabled
81+
assert df.iloc[0, 0] == 0
82+
83+
pd.options.mode.copy_on_write = True
84+
df = DataFrame({"a": [1, 2, 3], "b": [0.1, 0.2, 0.3]})
85+
subset = df[:]
86+
subset.iloc[0, 0] = 0
87+
# df not updated with CoW enabled
88+
assert df.iloc[0, 0] == 1
89+
90+
pd.options.mode.copy_on_write = False
91+
df = DataFrame({"a": [1, 2, 3], "b": [0.1, 0.2, 0.3]})
92+
subset = df[:]
93+
subset.iloc[0, 0] = 0
94+
# df updated with CoW disabled
95+
assert df.iloc[0, 0] == 0

0 commit comments

Comments
 (0)