Skip to content

Commit 53eeaff

Browse files
authored
WARN: Add FutureWarning for DataFrame.to_latex (#44411)
Co-authored-by: JHM Darbyshire (iMac) <[email protected]>
1 parent bc0c1d3 commit 53eeaff

File tree

8 files changed

+35
-2
lines changed

8 files changed

+35
-2
lines changed

doc/source/whatsnew/v1.2.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ and a short caption (:issue:`36267`).
150150
The keyword ``position`` has been added to set the position.
151151

152152
.. ipython:: python
153+
:okwarning:
153154
154155
data = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
155156
table = data.to_latex(position='ht')
@@ -161,6 +162,7 @@ one can optionally provide a tuple ``(full_caption, short_caption)``
161162
to add a short caption macro.
162163

163164
.. ipython:: python
165+
:okwarning:
164166
165167
data = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
166168
table = data.to_latex(caption=('the full long caption', 'short caption'))

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ Other Deprecations
466466
- Deprecated the 'errors' keyword argument in :meth:`Series.where`, :meth:`DataFrame.where`, :meth:`Series.mask`, and meth:`DataFrame.mask`; in a future version the argument will be removed (:issue:`44294`)
467467
- Deprecated :meth:`PeriodIndex.astype` to ``datetime64[ns]`` or ``DatetimeTZDtype``, use ``obj.to_timestamp(how).tz_localize(dtype.tz)`` instead (:issue:`44398`)
468468
- Deprecated :meth:`DateOffset.apply`, use ``offset + other`` instead (:issue:`44522`)
469+
- A deprecation warning is now shown for :meth:`DataFrame.to_latex` indicating the arguments signature may change and emulate more the arguments to :meth:`.Styler.to_latex` in future versions (:issue:`44411`)
469470
-
470471

471472
.. ---------------------------------------------------------------------------

pandas/core/generic.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -3272,6 +3272,7 @@ def to_latex(
32723272
{returns}
32733273
See Also
32743274
--------
3275+
Styler.to_latex : Render a DataFrame to LaTeX with conditional formatting.
32753276
DataFrame.to_string : Render a DataFrame to a console-friendly
32763277
tabular output.
32773278
DataFrame.to_html : Render a DataFrame as an HTML table.
@@ -3281,7 +3282,7 @@ def to_latex(
32813282
>>> df = pd.DataFrame(dict(name=['Raphael', 'Donatello'],
32823283
... mask=['red', 'purple'],
32833284
... weapon=['sai', 'bo staff']))
3284-
>>> print(df.to_latex(index=False)) # doctest: +NORMALIZE_WHITESPACE
3285+
>>> print(df.to_latex(index=False)) # doctest: +SKIP
32853286
\begin{{tabular}}{{lll}}
32863287
\toprule
32873288
name & mask & weapon \\
@@ -3291,6 +3292,15 @@ def to_latex(
32913292
\bottomrule
32923293
\end{{tabular}}
32933294
"""
3295+
msg = (
3296+
"In future versions `DataFrame.to_latex` is expected to utilise the base "
3297+
"implementation of `Styler.to_latex` for formatting and rendering. "
3298+
"The arguments signature may therefore change. It is recommended instead "
3299+
"to use `DataFrame.style.to_latex` which also contains additional "
3300+
"functionality."
3301+
)
3302+
warnings.warn(msg, FutureWarning, stacklevel=find_stack_level())
3303+
32943304
# Get defaults from the pandas config
32953305
if self.ndim == 1:
32963306
self = self.to_frame()

pandas/tests/frame/test_repr_info.py

+1
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ def test_repr_column_name_unicode_truncation_bug(self):
265265
with option_context("display.max_columns", 20):
266266
assert "StringCol" in repr(df)
267267

268+
@pytest.mark.filterwarnings("ignore::FutureWarning")
268269
def test_latex_repr(self):
269270
result = r"""\begin{tabular}{llll}
270271
\toprule

pandas/tests/io/formats/test_format.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -3298,6 +3298,7 @@ def test_repr_html_ipython_config(ip):
32983298
assert not result.error_in_exec
32993299

33003300

3301+
@pytest.mark.filterwarnings("ignore:In future versions `DataFrame.to_latex`")
33013302
@pytest.mark.parametrize("method", ["to_string", "to_html", "to_latex"])
33023303
@pytest.mark.parametrize(
33033304
"encoding, data",
@@ -3319,7 +3320,8 @@ def test_filepath_or_buffer_arg(
33193320
):
33203321
getattr(df, method)(buf=filepath_or_buffer, encoding=encoding)
33213322
elif encoding == "foo":
3322-
with tm.assert_produces_warning(None):
3323+
expected_warning = FutureWarning if method == "to_latex" else None
3324+
with tm.assert_produces_warning(expected_warning):
33233325
with pytest.raises(LookupError, match="unknown encoding"):
33243326
getattr(df, method)(buf=filepath_or_buffer, encoding=encoding)
33253327
else:
@@ -3328,6 +3330,7 @@ def test_filepath_or_buffer_arg(
33283330
assert_filepath_or_buffer_equals(expected)
33293331

33303332

3333+
@pytest.mark.filterwarnings("ignore::FutureWarning")
33313334
@pytest.mark.parametrize("method", ["to_string", "to_html", "to_latex"])
33323335
def test_filepath_or_buffer_bad_arg_raises(float_frame, method):
33333336
msg = "buf is not a file name and it has no write method"

pandas/tests/io/formats/test_to_latex.py

+14
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
RowStringConverter,
2020
)
2121

22+
pytestmark = pytest.mark.filterwarnings("ignore::FutureWarning")
23+
2224

2325
def _dedent(string):
2426
"""Dedent without new line in the beginning.
@@ -1514,3 +1516,15 @@ def test_get_strrow_multindex_multicolumn(self, row_num, expected):
15141516
)
15151517

15161518
assert row_string_converter.get_strrow(row_num=row_num) == expected
1519+
1520+
def test_future_warning(self):
1521+
df = DataFrame([[1]])
1522+
msg = (
1523+
"In future versions `DataFrame.to_latex` is expected to utilise the base "
1524+
"implementation of `Styler.to_latex` for formatting and rendering. "
1525+
"The arguments signature may therefore change. It is recommended instead "
1526+
"to use `DataFrame.style.to_latex` which also contains additional "
1527+
"functionality."
1528+
)
1529+
with tm.assert_produces_warning(FutureWarning, match=msg):
1530+
df.to_latex()

pandas/tests/io/test_common.py

+1
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ def test_read_fspath_all(self, reader, module, path, datapath):
348348
else:
349349
tm.assert_frame_equal(result, expected)
350350

351+
@pytest.mark.filterwarnings("ignore:In future versions `DataFrame.to_latex`")
351352
@pytest.mark.parametrize(
352353
"writer_name, writer_kwargs, module",
353354
[

pandas/tests/series/test_repr.py

+1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ def test_timeseries_repr_object_dtype(self):
196196
ts2 = ts.iloc[np.random.randint(0, len(ts) - 1, 400)]
197197
repr(ts2).splitlines()[-1]
198198

199+
@pytest.mark.filterwarnings("ignore::FutureWarning")
199200
def test_latex_repr(self):
200201
result = r"""\begin{tabular}{ll}
201202
\toprule

0 commit comments

Comments
 (0)