Skip to content

Commit

Permalink
Backport PR #49771 on branch 1.5.x (PERF: Use fastpath for accessing …
Browse files Browse the repository at this point in the history
…option value in internals) (#50128)

Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 13, 2023
1 parent 23a2699 commit 70a701d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -2427,5 +2427,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"]
31 changes: 31 additions & 0 deletions pandas/tests/copy_view/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -62,3 +63,33 @@ 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


@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
# (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

0 comments on commit 70a701d

Please sign in to comment.