Skip to content

STYLE: use option_context almost always #45407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions pandas/tests/indexes/multi/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
Index,
MultiIndex,
)
import pandas._testing as tm


def test_format(idx):
Expand All @@ -27,13 +26,10 @@ def test_format_sparse_config(idx):
warn_filters = warnings.filters
warnings.filterwarnings("ignore", category=FutureWarning, module=".*format")
# GH1538
pd.set_option("display.multi_sparse", False)

result = idx.format()
with pd.option_context("display.multi_sparse", False):
result = idx.format()
assert result[1] == "foo two"

tm.reset_display_options()

warnings.filters = warn_filters


Expand Down
19 changes: 10 additions & 9 deletions pandas/tests/indexing/test_chaining_and_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,17 @@ def test_setitem_chained_setfault(self):
@pytest.mark.arm_slow
def test_detect_chained_assignment(self):

pd.set_option("chained_assignment", "raise")

# work with the chain
expected = DataFrame([[-5, 1], [-6, 3]], columns=list("AB"))
df = DataFrame(np.arange(4).reshape(2, 2), columns=list("AB"), dtype="int64")
assert df._is_copy is None
with option_context("chained_assignment", "raise"):
# work with the chain
expected = DataFrame([[-5, 1], [-6, 3]], columns=list("AB"))
df = DataFrame(
np.arange(4).reshape(2, 2), columns=list("AB"), dtype="int64"
)
assert df._is_copy is None

df["A"][0] = -5
df["A"][1] = -6
tm.assert_frame_equal(df, expected)
df["A"][0] = -5
df["A"][1] = -6
tm.assert_frame_equal(df, expected)

@pytest.mark.arm_slow
def test_detect_chained_assignment_raises(self, using_array_manager):
Expand Down
11 changes: 4 additions & 7 deletions pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
DataFrame,
Index,
MultiIndex,
get_option,
set_option,
option_context,
)
import pandas._testing as tm

Expand Down Expand Up @@ -53,10 +52,8 @@ def set_engine(engine, ext):
the test it rolls back said change to the global option.
"""
option_name = f"io.excel.{ext.strip('.')}.writer"
prev_engine = get_option(option_name)
set_option(option_name, engine)
yield
set_option(option_name, prev_engine) # Roll back option change
with option_context(option_name, engine):
yield


@pytest.mark.parametrize(
Expand Down Expand Up @@ -1294,7 +1291,7 @@ def check_called(func):
del called_save[:]
del called_write_cells[:]

with pd.option_context("io.excel.xlsx.writer", "dummy"):
with option_context("io.excel.xlsx.writer", "dummy"):
path = "something.xlsx"
with tm.ensure_clean(path) as filepath:
register_writer(DummyClass)
Expand Down
51 changes: 20 additions & 31 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1146,20 +1146,18 @@ def test_wide_repr(self):
):
max_cols = get_option("display.max_columns")
df = DataFrame(tm.rands_array(25, size=(10, max_cols - 1)))
set_option("display.expand_frame_repr", False)
rep_str = repr(df)
with option_context("display.expand_frame_repr", False):
rep_str = repr(df)

assert f"10 rows x {max_cols - 1} columns" in rep_str
set_option("display.expand_frame_repr", True)
wide_repr = repr(df)
with option_context("display.expand_frame_repr", True):
wide_repr = repr(df)
assert rep_str != wide_repr

with option_context("display.width", 120):
wider_repr = repr(df)
assert len(wider_repr) < len(wide_repr)

reset_option("display.expand_frame_repr")

def test_wide_repr_wide_columns(self):
with option_context("mode.sim_interactive", True, "display.max_columns", 20):
df = DataFrame(
Expand All @@ -1174,11 +1172,10 @@ def test_wide_repr_named(self):
max_cols = get_option("display.max_columns")
df = DataFrame(tm.rands_array(25, size=(10, max_cols - 1)))
df.index.name = "DataFrame Index"
set_option("display.expand_frame_repr", False)

rep_str = repr(df)
set_option("display.expand_frame_repr", True)
wide_repr = repr(df)
with option_context("display.expand_frame_repr", False):
rep_str = repr(df)
with option_context("display.expand_frame_repr", True):
wide_repr = repr(df)
assert rep_str != wide_repr

with option_context("display.width", 150):
Expand All @@ -1188,18 +1185,16 @@ def test_wide_repr_named(self):
for line in wide_repr.splitlines()[1::13]:
assert "DataFrame Index" in line

reset_option("display.expand_frame_repr")

def test_wide_repr_multiindex(self):
with option_context("mode.sim_interactive", True, "display.max_columns", 20):
midx = MultiIndex.from_arrays(tm.rands_array(5, size=(2, 10)))
max_cols = get_option("display.max_columns")
df = DataFrame(tm.rands_array(25, size=(10, max_cols - 1)), index=midx)
df.index.names = ["Level 0", "Level 1"]
set_option("display.expand_frame_repr", False)
rep_str = repr(df)
set_option("display.expand_frame_repr", True)
wide_repr = repr(df)
with option_context("display.expand_frame_repr", False):
rep_str = repr(df)
with option_context("display.expand_frame_repr", True):
wide_repr = repr(df)
assert rep_str != wide_repr

with option_context("display.width", 150):
Expand All @@ -1209,8 +1204,6 @@ def test_wide_repr_multiindex(self):
for line in wide_repr.splitlines()[1::13]:
assert "Level 0 Level 1" in line

reset_option("display.expand_frame_repr")

def test_wide_repr_multiindex_cols(self):
with option_context("mode.sim_interactive", True, "display.max_columns", 20):
max_cols = get_option("display.max_columns")
Expand All @@ -1220,34 +1213,30 @@ def test_wide_repr_multiindex_cols(self):
tm.rands_array(25, (10, max_cols - 1)), index=midx, columns=mcols
)
df.index.names = ["Level 0", "Level 1"]
set_option("display.expand_frame_repr", False)
rep_str = repr(df)
set_option("display.expand_frame_repr", True)
wide_repr = repr(df)
with option_context("display.expand_frame_repr", False):
rep_str = repr(df)
with option_context("display.expand_frame_repr", True):
wide_repr = repr(df)
assert rep_str != wide_repr

with option_context("display.width", 150, "display.max_columns", 20):
wider_repr = repr(df)
assert len(wider_repr) < len(wide_repr)

reset_option("display.expand_frame_repr")

def test_wide_repr_unicode(self):
with option_context("mode.sim_interactive", True, "display.max_columns", 20):
max_cols = 20
df = DataFrame(tm.rands_array(25, size=(10, max_cols - 1)))
set_option("display.expand_frame_repr", False)
rep_str = repr(df)
set_option("display.expand_frame_repr", True)
wide_repr = repr(df)
with option_context("display.expand_frame_repr", False):
rep_str = repr(df)
with option_context("display.expand_frame_repr", True):
wide_repr = repr(df)
assert rep_str != wide_repr

with option_context("display.width", 150):
wider_repr = repr(df)
assert len(wider_repr) < len(wide_repr)

reset_option("display.expand_frame_repr")

def test_wide_repr_wide_long_columns(self):
with option_context("mode.sim_interactive", True):
df = DataFrame({"a": ["a" * 30, "b" * 30], "b": ["c" * 70, "d" * 80]})
Expand Down
107 changes: 54 additions & 53 deletions pandas/tests/io/pytables/test_append.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,66 +214,66 @@ def test_append_all_nans(setup_path):
tm.assert_frame_equal(store["df2"], df)

# tests the option io.hdf.dropna_table
pd.set_option("io.hdf.dropna_table", False)
_maybe_remove(store, "df3")
store.append("df3", df[:10])
store.append("df3", df[10:])
tm.assert_frame_equal(store["df3"], df)
with pd.option_context("io.hdf.dropna_table", False):
_maybe_remove(store, "df3")
store.append("df3", df[:10])
store.append("df3", df[10:])
tm.assert_frame_equal(store["df3"], df)

pd.set_option("io.hdf.dropna_table", True)
_maybe_remove(store, "df4")
store.append("df4", df[:10])
store.append("df4", df[10:])
tm.assert_frame_equal(store["df4"], df[-4:])
with pd.option_context("io.hdf.dropna_table", True):
_maybe_remove(store, "df4")
store.append("df4", df[:10])
store.append("df4", df[10:])
tm.assert_frame_equal(store["df4"], df[-4:])

# nan some entire rows (string are still written!)
df = DataFrame(
{
"A1": np.random.randn(20),
"A2": np.random.randn(20),
"B": "foo",
"C": "bar",
},
index=np.arange(20),
)
# nan some entire rows (string are still written!)
df = DataFrame(
{
"A1": np.random.randn(20),
"A2": np.random.randn(20),
"B": "foo",
"C": "bar",
},
index=np.arange(20),
)

df.loc[0:15, :] = np.nan
df.loc[0:15, :] = np.nan

_maybe_remove(store, "df")
store.append("df", df[:10], dropna=True)
store.append("df", df[10:], dropna=True)
tm.assert_frame_equal(store["df"], df)
_maybe_remove(store, "df")
store.append("df", df[:10], dropna=True)
store.append("df", df[10:], dropna=True)
tm.assert_frame_equal(store["df"], df)

_maybe_remove(store, "df2")
store.append("df2", df[:10], dropna=False)
store.append("df2", df[10:], dropna=False)
tm.assert_frame_equal(store["df2"], df)
_maybe_remove(store, "df2")
store.append("df2", df[:10], dropna=False)
store.append("df2", df[10:], dropna=False)
tm.assert_frame_equal(store["df2"], df)

# nan some entire rows (but since we have dates they are still
# written!)
df = DataFrame(
{
"A1": np.random.randn(20),
"A2": np.random.randn(20),
"B": "foo",
"C": "bar",
"D": Timestamp("20010101"),
"E": datetime.datetime(2001, 1, 2, 0, 0),
},
index=np.arange(20),
)
# nan some entire rows (but since we have dates they are still
# written!)
df = DataFrame(
{
"A1": np.random.randn(20),
"A2": np.random.randn(20),
"B": "foo",
"C": "bar",
"D": Timestamp("20010101"),
"E": datetime.datetime(2001, 1, 2, 0, 0),
},
index=np.arange(20),
)

df.loc[0:15, :] = np.nan
df.loc[0:15, :] = np.nan

_maybe_remove(store, "df")
store.append("df", df[:10], dropna=True)
store.append("df", df[10:], dropna=True)
tm.assert_frame_equal(store["df"], df)
_maybe_remove(store, "df")
store.append("df", df[:10], dropna=True)
store.append("df", df[10:], dropna=True)
tm.assert_frame_equal(store["df"], df)

_maybe_remove(store, "df2")
store.append("df2", df[:10], dropna=False)
store.append("df2", df[10:], dropna=False)
tm.assert_frame_equal(store["df2"], df)
_maybe_remove(store, "df2")
store.append("df2", df[:10], dropna=False)
store.append("df2", df[10:], dropna=False)
tm.assert_frame_equal(store["df2"], df)


def test_append_frame_column_oriented(setup_path):
Expand Down Expand Up @@ -898,8 +898,9 @@ def test_append_to_multiple_dropna_false(setup_path):
df1.iloc[1, df1.columns.get_indexer(["A", "B"])] = np.nan
df = concat([df1, df2], axis=1)

with ensure_clean_store(setup_path) as store:

with ensure_clean_store(setup_path) as store, pd.option_context(
"io.hdf.dropna_table", True
):
# dropna=False shouldn't synchronize row indexes
store.append_to_multiple(
{"df1a": ["A", "B"], "df2a": None}, df, selector="df1a", dropna=False
Expand Down
Loading