Skip to content

Commit 0119bdc

Browse files
PERF: Use fastpath for accessing option value in internals (#49771)
1 parent 3ece807 commit 0119bdc

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-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,
@@ -2349,5 +2349,8 @@ def _preprocess_slice_or_indexer(
23492349
return "fancy", indexer, len(indexer)
23502350

23512351

2352+
_mode_options = _global_config["mode"]
2353+
2354+
23522355
def _using_copy_on_write():
2353-
return get_option("mode.copy_on_write")
2356+
return _mode_options["copy_on_write"]

pandas/tests/copy_view/test_internals.py

+30
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,32 @@ 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+
@td.skip_array_manager_invalid_test
69+
def test_switch_options():
70+
# ensure we can switch the value of the option within one session
71+
# (assuming data is constructed after switching)
72+
73+
# using the option_context to ensure we set back to global option value
74+
# after running the test
75+
with pd.option_context("mode.copy_on_write", False):
76+
df = DataFrame({"a": [1, 2, 3], "b": [0.1, 0.2, 0.3]})
77+
subset = df[:]
78+
subset.iloc[0, 0] = 0
79+
# df updated with CoW disabled
80+
assert df.iloc[0, 0] == 0
81+
82+
pd.options.mode.copy_on_write = True
83+
df = DataFrame({"a": [1, 2, 3], "b": [0.1, 0.2, 0.3]})
84+
subset = df[:]
85+
subset.iloc[0, 0] = 0
86+
# df not updated with CoW enabled
87+
assert df.iloc[0, 0] == 1
88+
89+
pd.options.mode.copy_on_write = False
90+
df = DataFrame({"a": [1, 2, 3], "b": [0.1, 0.2, 0.3]})
91+
subset = df[:]
92+
subset.iloc[0, 0] = 0
93+
# df updated with CoW disabled
94+
assert df.iloc[0, 0] == 0

0 commit comments

Comments
 (0)