From 2fa2246bd8fdb83722d66c8c627c81473b0650a2 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Fri, 12 Nov 2021 18:18:25 +0100 Subject: [PATCH 01/10] add warning --- pandas/core/generic.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 23608cf0192df..a4dfa850be4e2 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3278,6 +3278,15 @@ def to_latex( \bottomrule \end{{tabular}} """ + msg = ( + "In future versions `DataFrame.to_latex` is expected to utilise the base " + "implementation of `Styler.to_latex` for formatting and rendering. " + "The arguments signature may therefore change. It is recommended instead to" + "use `DataFrame.style.to_latex` which also contains additional " + "functionality." + ) + warnings.warn(msg, FutureWarning, stacklevel=2) + # Get defaults from the pandas config if self.ndim == 1: self = self.to_frame() From 89225de96196858a764c05cb0e203d8efe55f39e Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Fri, 12 Nov 2021 18:23:07 +0100 Subject: [PATCH 02/10] add test --- pandas/tests/io/formats/test_to_latex.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pandas/tests/io/formats/test_to_latex.py b/pandas/tests/io/formats/test_to_latex.py index 10c8ccae67fb2..b1cf9cbe8867d 100644 --- a/pandas/tests/io/formats/test_to_latex.py +++ b/pandas/tests/io/formats/test_to_latex.py @@ -1514,3 +1514,15 @@ def test_get_strrow_multindex_multicolumn(self, row_num, expected): ) assert row_string_converter.get_strrow(row_num=row_num) == expected + + def test_future_warning(self): + df = DataFrame([[1]]) + msg = ( + "In future versions `DataFrame.to_latex` is expected to utilise the base " + "implementation of `Styler.to_latex` for formatting and rendering. " + "The arguments signature may therefore change. It is recommended instead to" + "use `DataFrame.style.to_latex` which also contains additional " + "functionality." + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + df.to_latex() From 1119a27b6d83279952fbbc98292d2bdabe7482b6 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Fri, 12 Nov 2021 20:28:37 +0100 Subject: [PATCH 03/10] fix doc warnings --- doc/source/whatsnew/v1.2.0.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index 36b591c3c3142..3d3ec53948a01 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -150,6 +150,7 @@ and a short caption (:issue:`36267`). The keyword ``position`` has been added to set the position. .. ipython:: python + :okwarning: data = pd.DataFrame({'a': [1, 2], 'b': [3, 4]}) table = data.to_latex(position='ht') @@ -161,6 +162,7 @@ one can optionally provide a tuple ``(full_caption, short_caption)`` to add a short caption macro. .. ipython:: python + :okwarning: data = pd.DataFrame({'a': [1, 2], 'b': [3, 4]}) table = data.to_latex(caption=('the full long caption', 'short caption')) From 93d92887e329e37c69769e375965b53b67bd6ce3 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Fri, 12 Nov 2021 22:47:25 +0100 Subject: [PATCH 04/10] fix test for warning --- pandas/tests/io/formats/test_format.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index d9bd8f6809c73..af62e2872997b 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -3319,7 +3319,8 @@ def test_filepath_or_buffer_arg( ): getattr(df, method)(buf=filepath_or_buffer, encoding=encoding) elif encoding == "foo": - with tm.assert_produces_warning(None): + expected_warning = FutureWarning if method == "to_latex" else None + with tm.assert_produces_warning(expected_warning): with pytest.raises(LookupError, match="unknown encoding"): getattr(df, method)(buf=filepath_or_buffer, encoding=encoding) else: From 2e653fec05c338f359bc1b04751363c34f52575e Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 13 Nov 2021 08:53:37 +0100 Subject: [PATCH 05/10] filterwarnings --- pandas/tests/io/formats/test_format.py | 2 ++ pandas/tests/io/formats/test_to_latex.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index af62e2872997b..3d8578833fd7c 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -3298,6 +3298,7 @@ def test_repr_html_ipython_config(ip): assert not result.error_in_exec +@pytest.mark.filterwarnings("ignore::FutureWarning") @pytest.mark.parametrize("method", ["to_string", "to_html", "to_latex"]) @pytest.mark.parametrize( "encoding, data", @@ -3329,6 +3330,7 @@ def test_filepath_or_buffer_arg( assert_filepath_or_buffer_equals(expected) +@pytest.mark.filterwarnings("ignore::FutureWarning") @pytest.mark.parametrize("method", ["to_string", "to_html", "to_latex"]) def test_filepath_or_buffer_bad_arg_raises(float_frame, method): msg = "buf is not a file name and it has no write method" diff --git a/pandas/tests/io/formats/test_to_latex.py b/pandas/tests/io/formats/test_to_latex.py index b1cf9cbe8867d..bc672eeb80d3d 100644 --- a/pandas/tests/io/formats/test_to_latex.py +++ b/pandas/tests/io/formats/test_to_latex.py @@ -19,6 +19,8 @@ RowStringConverter, ) +pytestmark = pytest.mark.filterwarnings("ignore::FutureWarning") + def _dedent(string): """Dedent without new line in the beginning. From 8e10574203af062551298769f2ec761009e6bb28 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 13 Nov 2021 12:04:31 +0100 Subject: [PATCH 06/10] skip doctest --- pandas/core/generic.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index a4dfa850be4e2..62798a5dc5d31 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3259,6 +3259,7 @@ def to_latex( {returns} See Also -------- + Styler.to_latex : Render a DataFrame to LaTeX with conditional formatting. DataFrame.to_string : Render a DataFrame to a console-friendly tabular output. DataFrame.to_html : Render a DataFrame as an HTML table. @@ -3268,7 +3269,7 @@ def to_latex( >>> df = pd.DataFrame(dict(name=['Raphael', 'Donatello'], ... mask=['red', 'purple'], ... weapon=['sai', 'bo staff'])) - >>> print(df.to_latex(index=False)) # doctest: +NORMALIZE_WHITESPACE + >>> print(df.to_latex(index=False)) # doctest: +SKIP \begin{{tabular}}{{lll}} \toprule name & mask & weapon \\ From e4fa4ef246f5a53a50ad7fd4ac13a3f93bf85544 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 13 Nov 2021 19:50:45 +0100 Subject: [PATCH 07/10] test warnings --- pandas/core/generic.py | 2 +- pandas/tests/frame/test_repr_info.py | 1 + pandas/tests/io/test_common.py | 1 + pandas/tests/series/test_repr.py | 1 + 4 files changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 62798a5dc5d31..4ffcd68cd517a 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3286,7 +3286,7 @@ def to_latex( "use `DataFrame.style.to_latex` which also contains additional " "functionality." ) - warnings.warn(msg, FutureWarning, stacklevel=2) + warnings.warn(msg, FutureWarning, stacklevel=find_stack_level()) # Get defaults from the pandas config if self.ndim == 1: diff --git a/pandas/tests/frame/test_repr_info.py b/pandas/tests/frame/test_repr_info.py index b288fafd8f7f6..bb80bd12c1958 100644 --- a/pandas/tests/frame/test_repr_info.py +++ b/pandas/tests/frame/test_repr_info.py @@ -265,6 +265,7 @@ def test_repr_column_name_unicode_truncation_bug(self): with option_context("display.max_columns", 20): assert "StringCol" in repr(df) + @pytest.mark.filterwarnings("ignore::FutureWarning") def test_latex_repr(self): result = r"""\begin{tabular}{llll} \toprule diff --git a/pandas/tests/io/test_common.py b/pandas/tests/io/test_common.py index 699459ab3666d..19735702c2fbe 100644 --- a/pandas/tests/io/test_common.py +++ b/pandas/tests/io/test_common.py @@ -348,6 +348,7 @@ def test_read_fspath_all(self, reader, module, path, datapath): else: tm.assert_frame_equal(result, expected) + @pytest.mark.filterwarnings("ignore:In future versions `DataFrame.to_latex`") @pytest.mark.parametrize( "writer_name, writer_kwargs, module", [ diff --git a/pandas/tests/series/test_repr.py b/pandas/tests/series/test_repr.py index 555342dd39005..73159bda02226 100644 --- a/pandas/tests/series/test_repr.py +++ b/pandas/tests/series/test_repr.py @@ -196,6 +196,7 @@ def test_timeseries_repr_object_dtype(self): ts2 = ts.iloc[np.random.randint(0, len(ts) - 1, 400)] repr(ts2).splitlines()[-1] + @pytest.mark.filterwarnings("ignore::FutureWarning") def test_latex_repr(self): result = r"""\begin{tabular}{ll} \toprule From e20c5d98becda94d02a3ed90bfb28224b2239e1d Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 13 Nov 2021 19:53:11 +0100 Subject: [PATCH 08/10] test warnings --- pandas/tests/io/formats/test_format.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 3d8578833fd7c..ab0199dca3f24 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -3298,7 +3298,7 @@ def test_repr_html_ipython_config(ip): assert not result.error_in_exec -@pytest.mark.filterwarnings("ignore::FutureWarning") +@pytest.mark.filterwarnings("ignore:In future versions `DataFrame.to_latex`") @pytest.mark.parametrize("method", ["to_string", "to_html", "to_latex"]) @pytest.mark.parametrize( "encoding, data", From 32534d503992cfc7314b36957aeadb08646c3a9c Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sun, 21 Nov 2021 22:25:54 +0100 Subject: [PATCH 09/10] whats new --- doc/source/whatsnew/v1.4.0.rst | 1 + pandas/core/generic.py | 4 ++-- pandas/tests/io/formats/test_to_latex.py | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 62ab6f934baec..e51ed094c7e75 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -465,6 +465,7 @@ Other Deprecations - 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`) - Deprecated :meth:`PeriodIndex.astype` to ``datetime64[ns]`` or ``DatetimeTZDtype``, use ``obj.to_timestamp(how).tz_localize(dtype.tz)`` instead (:issue:`44398`) - Deprecated :meth:`DateOffset.apply`, use ``offset + other`` instead (:issue:`44522`) +- A deprecation warning is now shown for :meth:`DataFrame.to_html` indicating the arguments signature may change and emulate more the arguments to :meth:`.Styler.to_html` in future versions (:issue:`44411`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 290ce376ca0d4..601b8dcd504d6 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3295,8 +3295,8 @@ def to_latex( msg = ( "In future versions `DataFrame.to_latex` is expected to utilise the base " "implementation of `Styler.to_latex` for formatting and rendering. " - "The arguments signature may therefore change. It is recommended instead to" - "use `DataFrame.style.to_latex` which also contains additional " + "The arguments signature may therefore change. It is recommended instead " + "to use `DataFrame.style.to_latex` which also contains additional " "functionality." ) warnings.warn(msg, FutureWarning, stacklevel=find_stack_level()) diff --git a/pandas/tests/io/formats/test_to_latex.py b/pandas/tests/io/formats/test_to_latex.py index bc672eeb80d3d..01bc94bf594d9 100644 --- a/pandas/tests/io/formats/test_to_latex.py +++ b/pandas/tests/io/formats/test_to_latex.py @@ -1522,8 +1522,8 @@ def test_future_warning(self): msg = ( "In future versions `DataFrame.to_latex` is expected to utilise the base " "implementation of `Styler.to_latex` for formatting and rendering. " - "The arguments signature may therefore change. It is recommended instead to" - "use `DataFrame.style.to_latex` which also contains additional " + "The arguments signature may therefore change. It is recommended instead " + "to use `DataFrame.style.to_latex` which also contains additional " "functionality." ) with tm.assert_produces_warning(FutureWarning, match=msg): From c51298b54efe41093dc77ad8d911a839abbded35 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Mon, 22 Nov 2021 07:15:32 +0100 Subject: [PATCH 10/10] method name --- doc/source/whatsnew/v1.4.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index e51ed094c7e75..3624cb651ecce 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -465,7 +465,7 @@ Other Deprecations - 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`) - Deprecated :meth:`PeriodIndex.astype` to ``datetime64[ns]`` or ``DatetimeTZDtype``, use ``obj.to_timestamp(how).tz_localize(dtype.tz)`` instead (:issue:`44398`) - Deprecated :meth:`DateOffset.apply`, use ``offset + other`` instead (:issue:`44522`) -- A deprecation warning is now shown for :meth:`DataFrame.to_html` indicating the arguments signature may change and emulate more the arguments to :meth:`.Styler.to_html` in future versions (:issue:`44411`) +- 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`) - .. ---------------------------------------------------------------------------