Skip to content

Commit 9bbb1c0

Browse files
attack68mroeschke
andauthored
CLN: remove redundant latex options (#50871)
* remove redundant latex options * remove redundant latex options * fix up tests and document defaults * CLN: remove redundant latex generation code * whats new text fix * add versionchanged * Update pandas/core/generic.py Co-authored-by: Matthew Roeschke <[email protected]> * Update pandas/core/generic.py * add to version changed --------- Co-authored-by: JHM Darbyshire (iMac) <[email protected]> Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 4510c6f commit 9bbb1c0

File tree

6 files changed

+77
-100
lines changed

6 files changed

+77
-100
lines changed

doc/source/whatsnew/v2.0.0.rst

+10-1
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,9 @@ The arguments signature is similar, albeit ``col_space`` has been removed since
629629
it is ignored by LaTeX engines. This render engine also requires ``jinja2`` as a
630630
dependency which needs to be installed, since rendering is based upon jinja2 templates.
631631

632-
The pandas options below are no longer used and will be removed in future releases.
632+
The pandas latex options below are no longer used and have been removed. The generic
633+
max rows and columns arguments remain but for this functionality should be replaced
634+
by the Styler equivalents.
633635
The alternative options giving similar functionality are indicated below:
634636

635637
- ``display.latex.escape``: replaced with ``styler.format.escape``,
@@ -643,6 +645,13 @@ The alternative options giving similar functionality are indicated below:
643645
``styler.render.max_rows``, ``styler.render.max_columns`` and
644646
``styler.render.max_elements``.
645647

648+
Note that due to this change some defaults have also changed:
649+
650+
- ``multirow`` now defaults to *True*.
651+
- ``multirow_align`` defaults to *"r"* instead of *"l"*.
652+
- ``multicol_align`` defaults to *"r"* instead of *"l"*.
653+
- ``escape`` now defaults to *False*.
654+
646655
Note that the behaviour of ``_repr_latex_`` is also changed. Previously
647656
setting ``display.latex.repr`` would generate LaTeX only when using nbconvert for a
648657
JupyterNotebook, and not when the user is running the notebook. Now the

pandas/core/config_init.py

-52
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,6 @@ def use_numba_cb(key) -> None:
210210
(default: False)
211211
"""
212212

213-
pc_latex_repr_doc = """
214-
: boolean
215-
Whether to produce a latex DataFrame representation for jupyter
216-
environments that support it.
217-
(default: False)
218-
"""
219-
220213
pc_table_schema_doc = """
221214
: boolean
222215
Whether to publish a Table Schema representation for frontends
@@ -292,41 +285,6 @@ def use_numba_cb(key) -> None:
292285
df.info() is called. Valid values True,False,'deep'
293286
"""
294287

295-
pc_latex_escape = """
296-
: bool
297-
This specifies if the to_latex method of a Dataframe uses escapes special
298-
characters.
299-
Valid values: False,True
300-
"""
301-
302-
pc_latex_longtable = """
303-
:bool
304-
This specifies if the to_latex method of a Dataframe uses the longtable
305-
format.
306-
Valid values: False,True
307-
"""
308-
309-
pc_latex_multicolumn = """
310-
: bool
311-
This specifies if the to_latex method of a Dataframe uses multicolumns
312-
to pretty-print MultiIndex columns.
313-
Valid values: False,True
314-
"""
315-
316-
pc_latex_multicolumn_format = """
317-
: string
318-
This specifies the format for multicolumn headers.
319-
Can be surrounded with '|'.
320-
Valid values: 'l', 'c', 'r', 'p{<width>}'
321-
"""
322-
323-
pc_latex_multirow = """
324-
: bool
325-
This specifies if the to_latex method of a Dataframe uses multirows
326-
to pretty-print MultiIndex rows.
327-
Valid values: False,True
328-
"""
329-
330288

331289
def table_schema_cb(key) -> None:
332290
from pandas.io.formats.printing import enable_data_resource_formatter
@@ -425,16 +383,6 @@ def is_terminal() -> bool:
425383
cf.register_option(
426384
"unicode.ambiguous_as_wide", False, pc_east_asian_width_doc, validator=is_bool
427385
)
428-
cf.register_option("latex.repr", False, pc_latex_repr_doc, validator=is_bool)
429-
cf.register_option("latex.escape", True, pc_latex_escape, validator=is_bool)
430-
cf.register_option("latex.longtable", False, pc_latex_longtable, validator=is_bool)
431-
cf.register_option(
432-
"latex.multicolumn", True, pc_latex_multicolumn, validator=is_bool
433-
)
434-
cf.register_option(
435-
"latex.multicolumn_format", "l", pc_latex_multicolumn, validator=is_text
436-
)
437-
cf.register_option("latex.multirow", False, pc_latex_multirow, validator=is_bool)
438386
cf.register_option(
439387
"html.table_schema",
440388
False,

pandas/core/generic.py

+40-14
Original file line numberDiff line numberDiff line change
@@ -3233,30 +3233,54 @@ def to_latex(
32333233
columns. By default, 'l' will be used for all columns except
32343234
columns of numbers, which default to 'r'.
32353235
longtable : bool, optional
3236-
By default, the value will be read from the pandas config
3237-
module. Use a longtable environment instead of tabular. Requires
3236+
Use a longtable environment instead of tabular. Requires
32383237
adding a \usepackage{{longtable}} to your LaTeX preamble.
3238+
By default, the value will be read from the pandas config
3239+
module, and set to `True` if the option ``styler.latex.environment`` is
3240+
`"longtable"`.
3241+
3242+
.. versionchanged:: 2.0.0
3243+
The pandas option affecting this argument has changed.
32393244
escape : bool, optional
32403245
By default, the value will be read from the pandas config
3241-
module. When set to False prevents from escaping latex special
3246+
module and set to `True` if the option ``styler.format.escape`` is
3247+
`"latex"`. When set to False prevents from escaping latex special
32423248
characters in column names.
3249+
3250+
.. versionchanged:: 2.0.0
3251+
The pandas option affecting this argument has changed, as has the
3252+
default value to `False`.
32433253
encoding : str, optional
32443254
A string representing the encoding to use in the output file,
32453255
defaults to 'utf-8'.
32463256
decimal : str, default '.'
32473257
Character recognized as decimal separator, e.g. ',' in Europe.
32483258
multicolumn : bool, default True
32493259
Use \multicolumn to enhance MultiIndex columns.
3250-
The default will be read from the config module.
3251-
multicolumn_format : str, default 'l'
3260+
The default will be read from the config module, and is set
3261+
as the option ``styler.sparse.columns``.
3262+
3263+
.. versionchanged:: 2.0.0
3264+
The pandas option affecting this argument has changed.
3265+
multicolumn_format : str, default 'r'
32523266
The alignment for multicolumns, similar to `column_format`
3253-
The default will be read from the config module.
3254-
multirow : bool, default False
3267+
The default will be read from the config module, and is set as the option
3268+
``styler.latex.multicol_align``.
3269+
3270+
.. versionchanged:: 2.0.0
3271+
The pandas option affecting this argument has changed, as has the
3272+
default value to "r".
3273+
multirow : bool, default True
32553274
Use \multirow to enhance MultiIndex rows. Requires adding a
32563275
\usepackage{{multirow}} to your LaTeX preamble. Will print
32573276
centered labels (instead of top-aligned) across the contained
32583277
rows, separating groups via clines. The default will be read
3259-
from the pandas config module.
3278+
from the pandas config module, and is set as the option
3279+
``styler.sparse.index``.
3280+
3281+
.. versionchanged:: 2.0.0
3282+
The pandas option affecting this argument has changed, as has the
3283+
default value to `True`.
32603284
caption : str or tuple, optional
32613285
Tuple (full_caption, short_caption),
32623286
which results in ``\caption[short_caption]{{full_caption}}``;
@@ -3324,15 +3348,15 @@ def to_latex(
33243348
if self.ndim == 1:
33253349
self = self.to_frame()
33263350
if longtable is None:
3327-
longtable = config.get_option("display.latex.longtable")
3351+
longtable = config.get_option("styler.latex.environment") == "longtable"
33283352
if escape is None:
3329-
escape = config.get_option("display.latex.escape")
3353+
escape = config.get_option("styler.format.escape") == "latex"
33303354
if multicolumn is None:
3331-
multicolumn = config.get_option("display.latex.multicolumn")
3355+
multicolumn = config.get_option("styler.sparse.columns")
33323356
if multicolumn_format is None:
3333-
multicolumn_format = config.get_option("display.latex.multicolumn_format")
3357+
multicolumn_format = config.get_option("styler.latex.multicol_align")
33343358
if multirow is None:
3335-
multirow = config.get_option("display.latex.multirow")
3359+
multirow = config.get_option("styler.sparse.index")
33363360

33373361
if column_format is not None and not isinstance(column_format, str):
33383362
raise ValueError("`column_format` must be str or unicode")
@@ -3418,7 +3442,9 @@ def _wrap(x, alt_format_):
34183442
"label": label,
34193443
"position": position,
34203444
"column_format": column_format,
3421-
"clines": "skip-last;data" if multirow else None,
3445+
"clines": "skip-last;data"
3446+
if (multirow and isinstance(self.index, MultiIndex))
3447+
else None,
34223448
"bold_rows": bold_rows,
34233449
}
34243450

pandas/tests/frame/test_repr_info.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def test_latex_repr(self):
292292
\end{tabular}
293293
"""
294294
with option_context(
295-
"display.latex.escape", False, "styler.render.repr", "latex"
295+
"styler.format.escape", None, "styler.render.repr", "latex"
296296
):
297297
df = DataFrame([[r"$\alpha$", "b", "c"], [1, 2, 3]])
298298
result = df._repr_latex_()

pandas/tests/io/formats/test_to_latex.py

+25-31
Original file line numberDiff line numberDiff line change
@@ -782,24 +782,14 @@ def test_to_latex_escape_false(self, df_with_symbols):
782782
assert result == expected
783783

784784
def test_to_latex_escape_default(self, df_with_symbols):
785-
result = df_with_symbols.to_latex() # default: escape=True
786-
expected = _dedent(
787-
r"""
788-
\begin{tabular}{lll}
789-
\toprule
790-
& co\$e\textasciicircum x\$ & co\textasciicircum l1 \\
791-
\midrule
792-
a & a & a \\
793-
b & b & b \\
794-
\bottomrule
795-
\end{tabular}
796-
"""
797-
)
798-
assert result == expected
785+
# gh50871: in v2.0 escape is False by default (styler.format.escape=None)
786+
default = df_with_symbols.to_latex()
787+
specified_true = df_with_symbols.to_latex(escape=True)
788+
assert default != specified_true
799789

800790
def test_to_latex_special_escape(self):
801791
df = DataFrame([r"a\b\c", r"^a^b^c", r"~a~b~c"])
802-
result = df.to_latex()
792+
result = df.to_latex(escape=True)
803793
expected = _dedent(
804794
r"""
805795
\begin{tabular}{ll}
@@ -818,7 +808,7 @@ def test_to_latex_special_escape(self):
818808
def test_to_latex_escape_special_chars(self):
819809
special_characters = ["&", "%", "$", "#", "_", "{", "}", "~", "^", "\\"]
820810
df = DataFrame(data=special_characters)
821-
result = df.to_latex()
811+
result = df.to_latex(escape=True)
822812
expected = _dedent(
823813
r"""
824814
\begin{tabular}{ll}
@@ -1039,7 +1029,7 @@ def test_to_latex_multindex_header(self):
10391029
# GH 16718
10401030
df = DataFrame({"a": [0], "b": [1], "c": [2], "d": [3]})
10411031
df = df.set_index(["a", "b"])
1042-
observed = df.to_latex(header=["r1", "r2"])
1032+
observed = df.to_latex(header=["r1", "r2"], multirow=False)
10431033
expected = _dedent(
10441034
r"""
10451035
\begin{tabular}{llrr}
@@ -1093,7 +1083,7 @@ def test_to_latex_multiindex_column_tabular(self):
10931083

10941084
def test_to_latex_multiindex_small_tabular(self):
10951085
df = DataFrame({("x", "y"): ["a"]}).T
1096-
result = df.to_latex()
1086+
result = df.to_latex(multirow=False)
10971087
expected = _dedent(
10981088
r"""
10991089
\begin{tabular}{lll}
@@ -1108,7 +1098,7 @@ def test_to_latex_multiindex_small_tabular(self):
11081098
assert result == expected
11091099

11101100
def test_to_latex_multiindex_tabular(self, multiindex_frame):
1111-
result = multiindex_frame.to_latex()
1101+
result = multiindex_frame.to_latex(multirow=False)
11121102
expected = _dedent(
11131103
r"""
11141104
\begin{tabular}{llrrrr}
@@ -1130,12 +1120,12 @@ def test_to_latex_multicolumn_tabular(self, multiindex_frame):
11301120
# GH 14184
11311121
df = multiindex_frame.T
11321122
df.columns.names = ["a", "b"]
1133-
result = df.to_latex()
1123+
result = df.to_latex(multirow=False)
11341124
expected = _dedent(
11351125
r"""
11361126
\begin{tabular}{lrrrrr}
11371127
\toprule
1138-
a & \multicolumn{2}{l}{c1} & \multicolumn{2}{l}{c2} & c3 \\
1128+
a & \multicolumn{2}{r}{c1} & \multicolumn{2}{r}{c2} & c3 \\
11391129
b & 0 & 1 & 0 & 1 & 0 \\
11401130
\midrule
11411131
0 & 0 & 4 & 0 & 4 & 0 \\
@@ -1151,7 +1141,7 @@ def test_to_latex_multicolumn_tabular(self, multiindex_frame):
11511141
def test_to_latex_index_has_name_tabular(self):
11521142
# GH 10660
11531143
df = DataFrame({"a": [0, 0, 1, 1], "b": list("abab"), "c": [1, 2, 3, 4]})
1154-
result = df.set_index(["a", "b"]).to_latex()
1144+
result = df.set_index(["a", "b"]).to_latex(multirow=False)
11551145
expected = _dedent(
11561146
r"""
11571147
\begin{tabular}{llr}
@@ -1172,12 +1162,16 @@ def test_to_latex_index_has_name_tabular(self):
11721162
def test_to_latex_groupby_tabular(self):
11731163
# GH 10660
11741164
df = DataFrame({"a": [0, 0, 1, 1], "b": list("abab"), "c": [1, 2, 3, 4]})
1175-
result = df.groupby("a").describe().to_latex(float_format="{:.1f}".format)
1165+
result = (
1166+
df.groupby("a")
1167+
.describe()
1168+
.to_latex(float_format="{:.1f}".format, escape=True)
1169+
)
11761170
expected = _dedent(
11771171
r"""
11781172
\begin{tabular}{lrrrrrrrr}
11791173
\toprule
1180-
& \multicolumn{8}{l}{c} \\
1174+
& \multicolumn{8}{r}{c} \\
11811175
& count & mean & std & min & 25\% & 50\% & 75\% & max \\
11821176
a & & & & & & & & \\
11831177
\midrule
@@ -1200,7 +1194,7 @@ def test_to_latex_multiindex_dupe_level(self):
12001194
df = DataFrame(
12011195
index=pd.MultiIndex.from_tuples([("A", "c"), ("B", "c")]), columns=["col"]
12021196
)
1203-
result = df.to_latex()
1197+
result = df.to_latex(multirow=False)
12041198
expected = _dedent(
12051199
r"""
12061200
\begin{tabular}{lll}
@@ -1221,7 +1215,7 @@ def test_to_latex_multicolumn_default(self, multicolumn_frame):
12211215
r"""
12221216
\begin{tabular}{lrrrrr}
12231217
\toprule
1224-
& \multicolumn{2}{l}{c1} & \multicolumn{2}{l}{c2} & c3 \\
1218+
& \multicolumn{2}{r}{c1} & \multicolumn{2}{r}{c2} & c3 \\
12251219
& 0 & 1 & 0 & 1 & 0 \\
12261220
\midrule
12271221
0 & 0 & 5 & 0 & 5 & 0 \\
@@ -1236,7 +1230,7 @@ def test_to_latex_multicolumn_default(self, multicolumn_frame):
12361230
assert result == expected
12371231

12381232
def test_to_latex_multicolumn_false(self, multicolumn_frame):
1239-
result = multicolumn_frame.to_latex(multicolumn=False)
1233+
result = multicolumn_frame.to_latex(multicolumn=False, multicolumn_format="l")
12401234
expected = _dedent(
12411235
r"""
12421236
\begin{tabular}{lrrrrr}
@@ -1323,11 +1317,11 @@ def test_to_latex_multiindex_names(self, name0, name1, axes):
13231317
else ""
13241318
)
13251319
col_names = [n if (bool(n) and 1 in axes) else "" for n in names]
1326-
observed = df.to_latex()
1320+
observed = df.to_latex(multirow=False)
13271321
# pylint: disable-next=consider-using-f-string
13281322
expected = r"""\begin{tabular}{llrrrr}
13291323
\toprule
1330-
& %s & \multicolumn{2}{l}{1} & \multicolumn{2}{l}{2} \\
1324+
& %s & \multicolumn{2}{r}{1} & \multicolumn{2}{r}{2} \\
13311325
& %s & 3 & 4 & 3 & 4 \\
13321326
%s\midrule
13331327
1 & 3 & -1 & -1 & -1 & -1 \\
@@ -1347,7 +1341,7 @@ def test_to_latex_multiindex_nans(self, one_row):
13471341
df = DataFrame({"a": [None, 1], "b": [2, 3], "c": [4, 5]})
13481342
if one_row:
13491343
df = df.iloc[[0]]
1350-
observed = df.set_index(["a", "b"]).to_latex()
1344+
observed = df.set_index(["a", "b"]).to_latex(multirow=False)
13511345
expected = _dedent(
13521346
r"""
13531347
\begin{tabular}{llr}
@@ -1369,7 +1363,7 @@ def test_to_latex_multiindex_nans(self, one_row):
13691363
def test_to_latex_non_string_index(self):
13701364
# GH 19981
13711365
df = DataFrame([[1, 2, 3]] * 2).set_index([0, 1])
1372-
result = df.to_latex()
1366+
result = df.to_latex(multirow=False)
13731367
expected = _dedent(
13741368
r"""
13751369
\begin{tabular}{llr}

pandas/tests/series/test_repr.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def test_latex_repr(self):
219219
\end{tabular}
220220
"""
221221
with option_context(
222-
"display.latex.escape", False, "styler.render.repr", "latex"
222+
"styler.format.escape", None, "styler.render.repr", "latex"
223223
):
224224
s = Series([r"$\alpha$", "b", "c"])
225225
assert result == s._repr_latex_()

0 commit comments

Comments
 (0)