Skip to content

CLN: Reset global config state in tests #51506

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 3 commits into from
Feb 21, 2023
Merged
Changes from 1 commit
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
88 changes: 42 additions & 46 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
option_context,
read_csv,
reset_option,
set_option,
)
import pandas._testing as tm

Expand Down Expand Up @@ -235,12 +234,11 @@ def test_repr_truncation(self):
def test_max_colwidth_negative_int_raises(self):
# Deprecation enforced from:
# https://github.com/pandas-dev/pandas/issues/31532
width = get_option("display.max_colwidth")
with pytest.raises(
ValueError, match="Value must be a nonnegative integer or None"
):
set_option("display.max_colwidth", -1)
set_option("display.max_colwidth", width)
with option_context("display.max_colwidth", -1):
pass

def test_repr_chop_threshold(self):
df = DataFrame([[0.1, 0.5], [0.5, -0.1]])
Expand Down Expand Up @@ -1114,11 +1112,10 @@ def test_repr_corner(self):

def test_frame_info_encoding(self):
index = ["'Til There Was You (1997)", "ldum klaka (Cold Fever) (1994)"]
fmt.set_option("display.max_rows", 1)
df = DataFrame(columns=["a", "b", "c"], index=index)
repr(df)
repr(df.T)
fmt.set_option("display.max_rows", 200)
with option_context("display.max_rows", 1):
df = DataFrame(columns=["a", "b", "c"], index=index)
repr(df)
repr(df.T)

def test_wide_repr(self):
with option_context(
Expand Down Expand Up @@ -1422,40 +1419,39 @@ def test_to_string_line_width_no_index(self):

def test_to_string_float_formatting(self):
tm.reset_display_options()
fmt.set_option(
with option_context(
"display.precision",
5,
"display.notebook_repr_html",
False,
)

df = DataFrame(
{"x": [0, 0.25, 3456.000, 12e45, 1.64e6, 1.7e8, 1.253456, np.pi, -1e6]}
)
):
df = DataFrame(
{"x": [0, 0.25, 3456.000, 12e45, 1.64e6, 1.7e8, 1.253456, np.pi, -1e6]}
)

df_s = df.to_string()
df_s = df.to_string()

if _three_digit_exp():
expected = (
" x\n0 0.00000e+000\n1 2.50000e-001\n"
"2 3.45600e+003\n3 1.20000e+046\n4 1.64000e+006\n"
"5 1.70000e+008\n6 1.25346e+000\n7 3.14159e+000\n"
"8 -1.00000e+006"
)
else:
expected = (
" x\n0 0.00000e+00\n1 2.50000e-01\n"
"2 3.45600e+03\n3 1.20000e+46\n4 1.64000e+06\n"
"5 1.70000e+08\n6 1.25346e+00\n7 3.14159e+00\n"
"8 -1.00000e+06"
)
assert df_s == expected
if _three_digit_exp():
expected = (
" x\n0 0.00000e+000\n1 2.50000e-001\n"
"2 3.45600e+003\n3 1.20000e+046\n4 1.64000e+006\n"
"5 1.70000e+008\n6 1.25346e+000\n7 3.14159e+000\n"
"8 -1.00000e+006"
)
else:
expected = (
" x\n0 0.00000e+00\n1 2.50000e-01\n"
"2 3.45600e+03\n3 1.20000e+46\n4 1.64000e+06\n"
"5 1.70000e+08\n6 1.25346e+00\n7 3.14159e+00\n"
"8 -1.00000e+06"
)
assert df_s == expected

df = DataFrame({"x": [3234, 0.253]})
df_s = df.to_string()
df = DataFrame({"x": [3234, 0.253]})
df_s = df.to_string()

expected = " x\n0 3234.000\n1 0.253"
assert df_s == expected
expected = " x\n0 3234.000\n1 0.253"
assert df_s == expected

tm.reset_display_options()
assert get_option("display.precision") == 6
Expand Down Expand Up @@ -1747,19 +1743,19 @@ def test_repr_html(self, float_frame):
df = float_frame
df._repr_html_()

fmt.set_option("display.max_rows", 1, "display.max_columns", 1)
df._repr_html_()
with option_context("display.max_rows", 1, "display.max_columns", 1):
df._repr_html_()

fmt.set_option("display.notebook_repr_html", False)
df._repr_html_()
with option_context("display.notebook_repr_html", False):
df._repr_html_()

tm.reset_display_options()

df = DataFrame([[1, 2], [3, 4]])
fmt.set_option("display.show_dimensions", True)
assert "2 rows" in df._repr_html_()
fmt.set_option("display.show_dimensions", False)
assert "2 rows" not in df._repr_html_()
with option_context("display.show_dimensions", True):
assert "2 rows" in df._repr_html_()
with option_context("display.show_dimensions", False):
assert "2 rows" not in df._repr_html_()

tm.reset_display_options()

Expand Down Expand Up @@ -1923,7 +1919,7 @@ def test_info_repr_max_cols(self):
assert not has_non_verbose_info_repr(df)

# test verbose overrides
# fmt.set_option('display.max_info_columns', 4) # exceeded
# set_option('display.max_info_columns', 4) # exceeded

def test_info_repr_html(self):
max_rows = 60
Expand Down Expand Up @@ -1953,8 +1949,8 @@ def get_ipython():
repstr = df._repr_html_()
assert repstr is not None

fmt.set_option("display.max_rows", 5, "display.max_columns", 2)
repstr = df._repr_html_()
with option_context("display.max_rows", 5, "display.max_columns", 2):
repstr = df._repr_html_()

assert "class" in repstr # info fallback
tm.reset_display_options()
Expand Down