diff --git a/pandas/_testing/__init__.py b/pandas/_testing/__init__.py index 51de242522074..6f9766f32b894 100644 --- a/pandas/_testing/__init__.py +++ b/pandas/_testing/__init__.py @@ -291,13 +291,6 @@ comparison_dunder_methods = ["__eq__", "__ne__", "__le__", "__lt__", "__ge__", "__gt__"] -def reset_display_options() -> None: - """ - Reset the display options for printing and representing objects. - """ - pd.reset_option("^display.", silent=True) - - # ----------------------------------------------------------------------------- # Comparators @@ -1174,7 +1167,6 @@ def shares_memory(left, right) -> bool: "NULL_OBJECTS", "OBJECT_DTYPES", "raise_assert_detail", - "reset_display_options", "raises_chained_assignment_error", "round_trip_localpath", "round_trip_pathlib", diff --git a/pandas/tests/frame/test_repr.py b/pandas/tests/frame/test_repr.py index f750074a36e91..98962b3003b6d 100644 --- a/pandas/tests/frame/test_repr.py +++ b/pandas/tests/frame/test_repr.py @@ -24,8 +24,6 @@ ) import pandas._testing as tm -import pandas.io.formats.format as fmt - class TestDataFrameRepr: def test_repr_should_return_str(self): @@ -220,16 +218,14 @@ def test_repr_unsortable(self): def test_repr_float_frame_options(self, float_frame): repr(float_frame) - fmt.set_option("display.precision", 3) - repr(float_frame) + with option_context("display.precision", 3): + repr(float_frame) - fmt.set_option("display.max_rows", 10, "display.max_columns", 2) - repr(float_frame) - - fmt.set_option("display.max_rows", 1000, "display.max_columns", 1000) - repr(float_frame) + with option_context("display.max_rows", 10, "display.max_columns", 2): + repr(float_frame) - tm.reset_display_options() + with option_context("display.max_rows", 1000, "display.max_columns", 1000): + repr(float_frame) def test_repr_unicode(self): uval = "\u03c3\u03c3\u03c3\u03c3" diff --git a/pandas/tests/io/formats/test_eng_formatting.py b/pandas/tests/io/formats/test_eng_formatting.py index 75e63cb1f6b54..6d581b5b92e0c 100644 --- a/pandas/tests/io/formats/test_eng_formatting.py +++ b/pandas/tests/io/formats/test_eng_formatting.py @@ -1,9 +1,19 @@ import numpy as np +import pytest -from pandas import DataFrame -import pandas._testing as tm +from pandas import ( + DataFrame, + reset_option, + set_eng_float_format, +) -import pandas.io.formats.format as fmt +from pandas.io.formats.format import EngFormatter + + +@pytest.fixture(autouse=True) +def reset_float_format(): + yield + reset_option("display.float_format") class TestEngFormatter: @@ -11,20 +21,19 @@ def test_eng_float_formatter2(self, float_frame): df = float_frame df.loc[5] = 0 - fmt.set_eng_float_format() + set_eng_float_format() repr(df) - fmt.set_eng_float_format(use_eng_prefix=True) + set_eng_float_format(use_eng_prefix=True) repr(df) - fmt.set_eng_float_format(accuracy=0) + set_eng_float_format(accuracy=0) repr(df) - tm.reset_display_options() def test_eng_float_formatter(self): df = DataFrame({"A": [1.41, 141.0, 14100, 1410000.0]}) - fmt.set_eng_float_format() + set_eng_float_format() result = df.to_string() expected = ( " A\n" @@ -35,18 +44,16 @@ def test_eng_float_formatter(self): ) assert result == expected - fmt.set_eng_float_format(use_eng_prefix=True) + set_eng_float_format(use_eng_prefix=True) result = df.to_string() expected = " A\n0 1.410\n1 141.000\n2 14.100k\n3 1.410M" assert result == expected - fmt.set_eng_float_format(accuracy=0) + set_eng_float_format(accuracy=0) result = df.to_string() expected = " A\n0 1E+00\n1 141E+00\n2 14E+03\n3 1E+06" assert result == expected - tm.reset_display_options() - def compare(self, formatter, input, output): formatted_input = formatter(input) assert formatted_input == output @@ -67,7 +74,7 @@ def compare_all(self, formatter, in_out): self.compare(formatter, -input, "-" + output[1:]) def test_exponents_with_eng_prefix(self): - formatter = fmt.EngFormatter(accuracy=3, use_eng_prefix=True) + formatter = EngFormatter(accuracy=3, use_eng_prefix=True) f = np.sqrt(2) in_out = [ (f * 10**-24, " 1.414y"), @@ -125,7 +132,7 @@ def test_exponents_with_eng_prefix(self): self.compare_all(formatter, in_out) def test_exponents_without_eng_prefix(self): - formatter = fmt.EngFormatter(accuracy=4, use_eng_prefix=False) + formatter = EngFormatter(accuracy=4, use_eng_prefix=False) f = np.pi in_out = [ (f * 10**-24, " 3.1416E-24"), @@ -183,7 +190,7 @@ def test_exponents_without_eng_prefix(self): self.compare_all(formatter, in_out) def test_rounding(self): - formatter = fmt.EngFormatter(accuracy=3, use_eng_prefix=True) + formatter = EngFormatter(accuracy=3, use_eng_prefix=True) in_out = [ (5.55555, " 5.556"), (55.5555, " 55.556"), @@ -194,7 +201,7 @@ def test_rounding(self): ] self.compare_all(formatter, in_out) - formatter = fmt.EngFormatter(accuracy=1, use_eng_prefix=True) + formatter = EngFormatter(accuracy=1, use_eng_prefix=True) in_out = [ (5.55555, " 5.6"), (55.5555, " 55.6"), @@ -205,7 +212,7 @@ def test_rounding(self): ] self.compare_all(formatter, in_out) - formatter = fmt.EngFormatter(accuracy=0, use_eng_prefix=True) + formatter = EngFormatter(accuracy=0, use_eng_prefix=True) in_out = [ (5.55555, " 6"), (55.5555, " 56"), @@ -216,14 +223,14 @@ def test_rounding(self): ] self.compare_all(formatter, in_out) - formatter = fmt.EngFormatter(accuracy=3, use_eng_prefix=True) + formatter = EngFormatter(accuracy=3, use_eng_prefix=True) result = formatter(0) assert result == " 0.000" def test_nan(self): # Issue #11981 - formatter = fmt.EngFormatter(accuracy=1, use_eng_prefix=True) + formatter = EngFormatter(accuracy=1, use_eng_prefix=True) result = formatter(np.nan) assert result == "NaN" @@ -235,14 +242,13 @@ def test_nan(self): } ) pt = df.pivot_table(values="a", index="b", columns="c") - fmt.set_eng_float_format(accuracy=1) + set_eng_float_format(accuracy=1) result = pt.to_string() assert "NaN" in result - tm.reset_display_options() def test_inf(self): # Issue #11981 - formatter = fmt.EngFormatter(accuracy=1, use_eng_prefix=True) + formatter = EngFormatter(accuracy=1, use_eng_prefix=True) result = formatter(np.inf) assert result == "inf" diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index 8cfcc26b95b4c..a7648cf1c471a 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -914,7 +914,6 @@ def get_ipython(): repstr = df._repr_html_() assert "class" in repstr # info fallback - tm.reset_display_options() def test_repr_html(self, float_frame): df = float_frame @@ -926,16 +925,12 @@ def test_repr_html(self, float_frame): with option_context("display.notebook_repr_html", False): df._repr_html_() - tm.reset_display_options() - df = DataFrame([[1, 2], [3, 4]]) 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() - def test_repr_html_mathjax(self): df = DataFrame([[1, 2], [3, 4]]) assert "tex2jax_ignore" not in df._repr_html_() diff --git a/pandas/tests/io/formats/test_to_string.py b/pandas/tests/io/formats/test_to_string.py index 3a8dcb339b063..e607b6eb454a1 100644 --- a/pandas/tests/io/formats/test_to_string.py +++ b/pandas/tests/io/formats/test_to_string.py @@ -412,7 +412,6 @@ def test_to_string_complex_float_formatting(self): def test_to_string_format_inf(self): # GH#24861 - tm.reset_display_options() df = DataFrame( { "A": [-np.inf, np.inf, -1, -2.1234, 3, 4], @@ -460,7 +459,6 @@ def test_to_string_int_formatting(self): assert output == expected def test_to_string_float_formatting(self): - tm.reset_display_options() with option_context( "display.precision", 5, @@ -495,7 +493,6 @@ def test_to_string_float_formatting(self): expected = " x\n0 3234.000\n1 0.253" assert df_s == expected - tm.reset_display_options() assert get_option("display.precision") == 6 df = DataFrame({"x": [1e9, 0.2512]}) @@ -516,14 +513,12 @@ def test_to_string_decimal(self): assert df.to_string(decimal=",") == expected def test_to_string_left_justify_cols(self): - tm.reset_display_options() df = DataFrame({"x": [3234, 0.253]}) df_s = df.to_string(justify="left") expected = " x \n0 3234.000\n1 0.253" assert df_s == expected def test_to_string_format_na(self): - tm.reset_display_options() df = DataFrame( { "A": [np.nan, -1, -2.1234, 3, 4],