22
22
import pytest
23
23
import pytz
24
24
25
+ from pandas ._config import config
26
+
25
27
from pandas .compat import (
26
28
IS64 ,
27
29
is_platform_windows ,
40
42
option_context ,
41
43
read_csv ,
42
44
reset_option ,
43
- set_option ,
44
45
)
45
46
import pandas ._testing as tm
46
47
@@ -57,6 +58,17 @@ def get_local_am_pm():
57
58
return am_local , pm_local
58
59
59
60
61
+ @pytest .fixture (autouse = True )
62
+ def clean_config ():
63
+ curr_deprecated_options = config ._deprecated_options .copy ()
64
+ curr_registered_options = config ._registered_options .copy ()
65
+ curr_global_config = config ._global_config .copy ()
66
+ yield
67
+ config ._deprecated_options = curr_deprecated_options
68
+ config ._registered_options = curr_registered_options
69
+ config ._global_config = curr_global_config
70
+
71
+
60
72
@pytest .fixture (params = ["string" , "pathlike" , "buffer" ])
61
73
def filepath_or_buffer_id (request ):
62
74
"""
@@ -234,12 +246,11 @@ def test_repr_truncation(self):
234
246
def test_max_colwidth_negative_int_raises (self ):
235
247
# Deprecation enforced from:
236
248
# https://github.com/pandas-dev/pandas/issues/31532
237
- width = get_option ("display.max_colwidth" )
238
249
with pytest .raises (
239
250
ValueError , match = "Value must be a nonnegative integer or None"
240
251
):
241
- set_option ("display.max_colwidth" , - 1 )
242
- set_option ( "display.max_colwidth" , width )
252
+ with option_context ("display.max_colwidth" , - 1 ):
253
+ pass
243
254
244
255
def test_repr_chop_threshold (self ):
245
256
df = DataFrame ([[0.1 , 0.5 ], [0.5 , - 0.1 ]])
@@ -1113,11 +1124,10 @@ def test_repr_corner(self):
1113
1124
1114
1125
def test_frame_info_encoding (self ):
1115
1126
index = ["'Til There Was You (1997)" , "ldum klaka (Cold Fever) (1994)" ]
1116
- fmt .set_option ("display.max_rows" , 1 )
1117
- df = DataFrame (columns = ["a" , "b" , "c" ], index = index )
1118
- repr (df )
1119
- repr (df .T )
1120
- fmt .set_option ("display.max_rows" , 200 )
1127
+ with option_context ("display.max_rows" , 1 ):
1128
+ df = DataFrame (columns = ["a" , "b" , "c" ], index = index )
1129
+ repr (df )
1130
+ repr (df .T )
1121
1131
1122
1132
def test_wide_repr (self ):
1123
1133
with option_context (
@@ -1421,40 +1431,39 @@ def test_to_string_line_width_no_index(self):
1421
1431
1422
1432
def test_to_string_float_formatting (self ):
1423
1433
tm .reset_display_options ()
1424
- fmt . set_option (
1434
+ with option_context (
1425
1435
"display.precision" ,
1426
1436
5 ,
1427
1437
"display.notebook_repr_html" ,
1428
1438
False ,
1429
- )
1430
-
1431
- df = DataFrame (
1432
- {"x" : [0 , 0.25 , 3456.000 , 12e45 , 1.64e6 , 1.7e8 , 1.253456 , np .pi , - 1e6 ]}
1433
- )
1439
+ ):
1440
+ df = DataFrame (
1441
+ {"x" : [0 , 0.25 , 3456.000 , 12e45 , 1.64e6 , 1.7e8 , 1.253456 , np .pi , - 1e6 ]}
1442
+ )
1434
1443
1435
- df_s = df .to_string ()
1444
+ df_s = df .to_string ()
1436
1445
1437
- if _three_digit_exp ():
1438
- expected = (
1439
- " x\n 0 0.00000e+000\n 1 2.50000e-001\n "
1440
- "2 3.45600e+003\n 3 1.20000e+046\n 4 1.64000e+006\n "
1441
- "5 1.70000e+008\n 6 1.25346e+000\n 7 3.14159e+000\n "
1442
- "8 -1.00000e+006"
1443
- )
1444
- else :
1445
- expected = (
1446
- " x\n 0 0.00000e+00\n 1 2.50000e-01\n "
1447
- "2 3.45600e+03\n 3 1.20000e+46\n 4 1.64000e+06\n "
1448
- "5 1.70000e+08\n 6 1.25346e+00\n 7 3.14159e+00\n "
1449
- "8 -1.00000e+06"
1450
- )
1451
- assert df_s == expected
1446
+ if _three_digit_exp ():
1447
+ expected = (
1448
+ " x\n 0 0.00000e+000\n 1 2.50000e-001\n "
1449
+ "2 3.45600e+003\n 3 1.20000e+046\n 4 1.64000e+006\n "
1450
+ "5 1.70000e+008\n 6 1.25346e+000\n 7 3.14159e+000\n "
1451
+ "8 -1.00000e+006"
1452
+ )
1453
+ else :
1454
+ expected = (
1455
+ " x\n 0 0.00000e+00\n 1 2.50000e-01\n "
1456
+ "2 3.45600e+03\n 3 1.20000e+46\n 4 1.64000e+06\n "
1457
+ "5 1.70000e+08\n 6 1.25346e+00\n 7 3.14159e+00\n "
1458
+ "8 -1.00000e+06"
1459
+ )
1460
+ assert df_s == expected
1452
1461
1453
- df = DataFrame ({"x" : [3234 , 0.253 ]})
1454
- df_s = df .to_string ()
1462
+ df = DataFrame ({"x" : [3234 , 0.253 ]})
1463
+ df_s = df .to_string ()
1455
1464
1456
- expected = " x\n 0 3234.000\n 1 0.253"
1457
- assert df_s == expected
1465
+ expected = " x\n 0 3234.000\n 1 0.253"
1466
+ assert df_s == expected
1458
1467
1459
1468
tm .reset_display_options ()
1460
1469
assert get_option ("display.precision" ) == 6
@@ -1746,19 +1755,19 @@ def test_repr_html(self, float_frame):
1746
1755
df = float_frame
1747
1756
df ._repr_html_ ()
1748
1757
1749
- fmt . set_option ("display.max_rows" , 1 , "display.max_columns" , 1 )
1750
- df ._repr_html_ ()
1758
+ with option_context ("display.max_rows" , 1 , "display.max_columns" , 1 ):
1759
+ df ._repr_html_ ()
1751
1760
1752
- fmt . set_option ("display.notebook_repr_html" , False )
1753
- df ._repr_html_ ()
1761
+ with option_context ("display.notebook_repr_html" , False ):
1762
+ df ._repr_html_ ()
1754
1763
1755
1764
tm .reset_display_options ()
1756
1765
1757
1766
df = DataFrame ([[1 , 2 ], [3 , 4 ]])
1758
- fmt . set_option ("display.show_dimensions" , True )
1759
- assert "2 rows" in df ._repr_html_ ()
1760
- fmt . set_option ("display.show_dimensions" , False )
1761
- assert "2 rows" not in df ._repr_html_ ()
1767
+ with option_context ("display.show_dimensions" , True ):
1768
+ assert "2 rows" in df ._repr_html_ ()
1769
+ with option_context ("display.show_dimensions" , False ):
1770
+ assert "2 rows" not in df ._repr_html_ ()
1762
1771
1763
1772
tm .reset_display_options ()
1764
1773
@@ -1922,7 +1931,7 @@ def test_info_repr_max_cols(self):
1922
1931
assert not has_non_verbose_info_repr (df )
1923
1932
1924
1933
# test verbose overrides
1925
- # fmt. set_option('display.max_info_columns', 4) # exceeded
1934
+ # set_option('display.max_info_columns', 4) # exceeded
1926
1935
1927
1936
def test_info_repr_html (self ):
1928
1937
max_rows = 60
@@ -1952,8 +1961,8 @@ def get_ipython():
1952
1961
repstr = df ._repr_html_ ()
1953
1962
assert repstr is not None
1954
1963
1955
- fmt . set_option ("display.max_rows" , 5 , "display.max_columns" , 2 )
1956
- repstr = df ._repr_html_ ()
1964
+ with option_context ("display.max_rows" , 5 , "display.max_columns" , 2 ):
1965
+ repstr = df ._repr_html_ ()
1957
1966
1958
1967
assert "class" in repstr # info fallback
1959
1968
tm .reset_display_options ()
0 commit comments