From 846a239930717b0bb286cd0b547d6ef8e67a6b11 Mon Sep 17 00:00:00 2001 From: Sylvain Lannuzel Date: Wed, 15 Jul 2020 13:52:55 +0200 Subject: [PATCH 01/15] add position argument for to_latex method --- pandas/core/generic.py | 5 +++++ pandas/io/formats/format.py | 2 ++ pandas/io/formats/latex.py | 19 ++++++++++++++++--- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index eb55369d83593..4fbc1d4b6965e 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2840,6 +2840,7 @@ def to_latex( multirow=None, caption=None, label=None, + position=None, ): r""" Render object to a LaTeX tabular, longtable, or nested table/tabular. @@ -2925,6 +2926,9 @@ def to_latex( This is used with ``\ref{}`` in the main ``.tex`` file. .. versionadded:: 1.0.0 + position : str, optional + The LaTeX positional argument for tables, to be placed after + ``\begin{}`` in the output. %(returns)s See Also -------- @@ -2986,6 +2990,7 @@ def to_latex( multirow=multirow, caption=caption, label=label, + position=position, ) def to_csv( diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index fe85eab4bfbf5..bdb61974b15b1 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -931,6 +931,7 @@ def to_latex( multirow: bool = False, caption: Optional[str] = None, label: Optional[str] = None, + position: Optional[str] = None, ) -> Optional[str]: """ Render a DataFrame to a LaTeX tabular/longtable environment output. @@ -946,6 +947,7 @@ def to_latex( multirow=multirow, caption=caption, label=label, + position=position, ).get_result(buf=buf, encoding=encoding) def _format_col(self, i: int) -> List[str]: diff --git a/pandas/io/formats/latex.py b/pandas/io/formats/latex.py index 3a3ca84642d51..cd7aabbd8f8a3 100644 --- a/pandas/io/formats/latex.py +++ b/pandas/io/formats/latex.py @@ -38,6 +38,7 @@ def __init__( multirow: bool = False, caption: Optional[str] = None, label: Optional[str] = None, + position: Optional[str] = None, ): self.fmt = formatter self.frame = self.fmt.frame @@ -50,6 +51,7 @@ def __init__( self.caption = caption self.label = label self.escape = self.fmt.escape + self.position = position def write_result(self, buf: IO[str]) -> None: """ @@ -284,8 +286,10 @@ def _write_tabular_begin(self, buf, column_format: str): `__ e.g 'rcl' for 3 columns """ - if self.caption is not None or self.label is not None: + if self.caption is not None or self.label is not None \ + or self.position is not None: # then write output in a nested table/tabular environment + buf.write(f"\\begin{{table}}") if self.caption is None: caption_ = "" else: @@ -296,7 +300,12 @@ def _write_tabular_begin(self, buf, column_format: str): else: label_ = f"\n\\label{{{self.label}}}" - buf.write(f"\\begin{{table}}\n\\centering{caption_}{label_}\n") + if self.position is None: + position_ = "" + else: + position_ = f"[{self.position}]" + + buf.write(f"{position_}\n\\centering{caption_}{label_}\n") else: # then write output only in a tabular environment pass @@ -337,7 +346,11 @@ def _write_longtable_begin(self, buf, column_format: str): `__ e.g 'rcl' for 3 columns """ - buf.write(f"\\begin{{longtable}}{{{column_format}}}\n") + if self.position is None: + position_ = "" + else: + position_ = f"[{self.position}]" + buf.write(f"\\begin{{longtable}}{position_}{{{column_format}}}\n") if self.caption is not None or self.label is not None: if self.caption is None: From 704202fde2b8482cb7caa8e1d3d132cf1a190fa3 Mon Sep 17 00:00:00 2001 From: Sylvain Lannuzel Date: Wed, 15 Jul 2020 13:55:25 +0200 Subject: [PATCH 02/15] reformat or statement --- pandas/io/formats/latex.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pandas/io/formats/latex.py b/pandas/io/formats/latex.py index cd7aabbd8f8a3..abe9678af64ed 100644 --- a/pandas/io/formats/latex.py +++ b/pandas/io/formats/latex.py @@ -286,8 +286,11 @@ def _write_tabular_begin(self, buf, column_format: str): `__ e.g 'rcl' for 3 columns """ - if self.caption is not None or self.label is not None \ - or self.position is not None: + if ( + self.caption is not None + or self.label is not None + or self.position is not None + ): # then write output in a nested table/tabular environment buf.write(f"\\begin{{table}}") if self.caption is None: From d3518de4c408bc1385a63bd2cbe0376c6905aec7 Mon Sep 17 00:00:00 2001 From: Sylvain Lannuzel Date: Wed, 15 Jul 2020 14:13:40 +0200 Subject: [PATCH 03/15] add end table when position is not None --- pandas/io/formats/latex.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/io/formats/latex.py b/pandas/io/formats/latex.py index abe9678af64ed..384308b792281 100644 --- a/pandas/io/formats/latex.py +++ b/pandas/io/formats/latex.py @@ -329,7 +329,11 @@ def _write_tabular_end(self, buf): """ buf.write("\\bottomrule\n") buf.write("\\end{tabular}\n") - if self.caption is not None or self.label is not None: + if ( + self.caption is not None + or self.label is not None + or self.position is not None + ): buf.write("\\end{table}\n") else: pass From 8e30e11563c1fdd45ff7d4de6ed824bd6f9b9559 Mon Sep 17 00:00:00 2001 From: Sylvain Lannuzel Date: Wed, 15 Jul 2020 14:22:47 +0200 Subject: [PATCH 04/15] add tests for position argument --- pandas/tests/io/formats/test_to_latex.py | 44 ++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/pandas/tests/io/formats/test_to_latex.py b/pandas/tests/io/formats/test_to_latex.py index 509e5bcb33304..b65159bb8a3d3 100644 --- a/pandas/tests/io/formats/test_to_latex.py +++ b/pandas/tests/io/formats/test_to_latex.py @@ -438,10 +438,11 @@ def test_to_latex_longtable(self): with3columns_result = df.to_latex(index=False, longtable=True) assert r"\multicolumn{3}" in with3columns_result - def test_to_latex_caption_label(self): + def test_to_latex_caption_label_position(self): # GH 25436 the_caption = "a table in a \\texttt{table/tabular} environment" the_label = "tab:table_tabular" + the_position = "h" df = DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) @@ -481,6 +482,23 @@ def test_to_latex_caption_label(self): """ assert result_l == expected_l + # test when only the label is provided + result_p = df.to_latex(position=the_position) + + expected_p = r"""\begin{table}[h] +\centering +\begin{tabular}{lrl} +\toprule +{} & a & b \\ +\midrule +0 & 1 & b1 \\ +1 & 2 & b2 \\ +\bottomrule +\end{tabular} +\end{table} +""" + assert result_p == expected_p + # test when the caption and the label are provided result_cl = df.to_latex(caption=the_caption, label=the_label) @@ -500,10 +518,11 @@ def test_to_latex_caption_label(self): """ assert result_cl == expected_cl - def test_to_latex_longtable_caption_label(self): + def test_to_latex_longtable_caption_label_position(self): # GH 25436 the_caption = "a table in a \\texttt{longtable} environment" the_label = "tab:longtable" + the_position = "t" df = DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) @@ -551,6 +570,27 @@ def test_to_latex_longtable_caption_label(self): """ assert result_l == expected_l + # test when only the caption is provided + result_c = df.to_latex(longtable=True, position=the_position) + + expected_c = r"""\begin{longtable}[t]{lrl} +\toprule +{} & a & b \\ +\midrule +\endhead +\midrule +\multicolumn{3}{r}{{Continued on next page}} \\ +\midrule +\endfoot + +\bottomrule +\endlastfoot +0 & 1 & b1 \\ +1 & 2 & b2 \\ +\end{longtable} +""" + assert result_c == expected_c + # test when the caption and the label are provided result_cl = df.to_latex(longtable=True, caption=the_caption, label=the_label) From a0b1bc494d368f354e8d01bd6d67fafc442e32f6 Mon Sep 17 00:00:00 2001 From: Sylvain Lannuzel Date: Wed, 15 Jul 2020 14:26:41 +0200 Subject: [PATCH 05/15] change names --- pandas/tests/io/formats/test_to_latex.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/io/formats/test_to_latex.py b/pandas/tests/io/formats/test_to_latex.py index b65159bb8a3d3..2bce667630d04 100644 --- a/pandas/tests/io/formats/test_to_latex.py +++ b/pandas/tests/io/formats/test_to_latex.py @@ -571,9 +571,9 @@ def test_to_latex_longtable_caption_label_position(self): assert result_l == expected_l # test when only the caption is provided - result_c = df.to_latex(longtable=True, position=the_position) + result_p = df.to_latex(longtable=True, position=the_position) - expected_c = r"""\begin{longtable}[t]{lrl} + expected_p = r"""\begin{longtable}[t]{lrl} \toprule {} & a & b \\ \midrule @@ -589,7 +589,7 @@ def test_to_latex_longtable_caption_label_position(self): 1 & 2 & b2 \\ \end{longtable} """ - assert result_c == expected_c + assert result_p == expected_p # test when the caption and the label are provided result_cl = df.to_latex(longtable=True, caption=the_caption, label=the_label) From 940213ce8c174aa080115c42f0cef4592b4717ef Mon Sep 17 00:00:00 2001 From: Sylvain Lannuzel Date: Wed, 15 Jul 2020 14:48:09 +0200 Subject: [PATCH 06/15] reformat or statement --- pandas/io/formats/latex.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/io/formats/latex.py b/pandas/io/formats/latex.py index 384308b792281..32b52960e21c4 100644 --- a/pandas/io/formats/latex.py +++ b/pandas/io/formats/latex.py @@ -330,9 +330,9 @@ def _write_tabular_end(self, buf): buf.write("\\bottomrule\n") buf.write("\\end{tabular}\n") if ( - self.caption is not None - or self.label is not None - or self.position is not None + self.caption is not None + or self.label is not None + or self.position is not None ): buf.write("\\end{table}\n") else: From fff3559342dc0fcf269f3a15136184665d46a0d6 Mon Sep 17 00:00:00 2001 From: Sylvain Lannuzel Date: Wed, 22 Jul 2020 09:42:08 +0200 Subject: [PATCH 07/15] create own tests for position argument --- pandas/tests/io/formats/test_to_latex.py | 62 +++++++++++++----------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/pandas/tests/io/formats/test_to_latex.py b/pandas/tests/io/formats/test_to_latex.py index 2bce667630d04..98e58978a5b96 100644 --- a/pandas/tests/io/formats/test_to_latex.py +++ b/pandas/tests/io/formats/test_to_latex.py @@ -442,7 +442,6 @@ def test_to_latex_caption_label_position(self): # GH 25436 the_caption = "a table in a \\texttt{table/tabular} environment" the_label = "tab:table_tabular" - the_position = "h" df = DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) @@ -482,23 +481,6 @@ def test_to_latex_caption_label_position(self): """ assert result_l == expected_l - # test when only the label is provided - result_p = df.to_latex(position=the_position) - - expected_p = r"""\begin{table}[h] -\centering -\begin{tabular}{lrl} -\toprule -{} & a & b \\ -\midrule -0 & 1 & b1 \\ -1 & 2 & b2 \\ -\bottomrule -\end{tabular} -\end{table} -""" - assert result_p == expected_p - # test when the caption and the label are provided result_cl = df.to_latex(caption=the_caption, label=the_label) @@ -522,7 +504,6 @@ def test_to_latex_longtable_caption_label_position(self): # GH 25436 the_caption = "a table in a \\texttt{longtable} environment" the_label = "tab:longtable" - the_position = "t" df = DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) @@ -570,10 +551,11 @@ def test_to_latex_longtable_caption_label_position(self): """ assert result_l == expected_l - # test when only the caption is provided - result_p = df.to_latex(longtable=True, position=the_position) + # test when the caption and the label are provided + result_cl = df.to_latex(longtable=True, caption=the_caption, label=the_label) - expected_p = r"""\begin{longtable}[t]{lrl} + expected_cl = r"""\begin{longtable}{lrl} +\caption{a table in a \texttt{longtable} environment}\label{tab:longtable}\\ \toprule {} & a & b \\ \midrule @@ -588,14 +570,40 @@ def test_to_latex_longtable_caption_label_position(self): 0 & 1 & b1 \\ 1 & 2 & b2 \\ \end{longtable} +""" + assert result_cl == expected_cl + + def test_to_latex_position(self): + the_position = "h" + + df = DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) + + # test when only the position is provided + result_p = df.to_latex(position=the_position) + + expected_p = r"""\begin{table}[h] +\centering +\begin{tabular}{lrl} +\toprule +{} & a & b \\ +\midrule +0 & 1 & b1 \\ +1 & 2 & b2 \\ +\bottomrule +\end{tabular} +\end{table} """ assert result_p == expected_p - # test when the caption and the label are provided - result_cl = df.to_latex(longtable=True, caption=the_caption, label=the_label) + def test_to_latex_longtable_position(self): + the_position = "t" - expected_cl = r"""\begin{longtable}{lrl} -\caption{a table in a \texttt{longtable} environment}\label{tab:longtable}\\ + df = DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) + + # test when only the position is provided + result_p = df.to_latex(longtable=True, position=the_position) + + expected_p = r"""\begin{longtable}[t]{lrl} \toprule {} & a & b \\ \midrule @@ -611,7 +619,7 @@ def test_to_latex_longtable_caption_label_position(self): 1 & 2 & b2 \\ \end{longtable} """ - assert result_cl == expected_cl + assert result_p == expected_p def test_to_latex_escape_special_chars(self): special_characters = ["&", "%", "$", "#", "_", "{", "}", "~", "^", "\\"] From f3b98161a9bd0093a63c46ef29de411dc17a4a6a Mon Sep 17 00:00:00 2001 From: Sylvain Lannuzel Date: Wed, 22 Jul 2020 09:42:57 +0200 Subject: [PATCH 08/15] revert names --- pandas/tests/io/formats/test_to_latex.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/io/formats/test_to_latex.py b/pandas/tests/io/formats/test_to_latex.py index 98e58978a5b96..93ad3739e59c7 100644 --- a/pandas/tests/io/formats/test_to_latex.py +++ b/pandas/tests/io/formats/test_to_latex.py @@ -438,7 +438,7 @@ def test_to_latex_longtable(self): with3columns_result = df.to_latex(index=False, longtable=True) assert r"\multicolumn{3}" in with3columns_result - def test_to_latex_caption_label_position(self): + def test_to_latex_caption_label(self): # GH 25436 the_caption = "a table in a \\texttt{table/tabular} environment" the_label = "tab:table_tabular" @@ -500,7 +500,7 @@ def test_to_latex_caption_label_position(self): """ assert result_cl == expected_cl - def test_to_latex_longtable_caption_label_position(self): + def test_to_latex_longtable_caption_label(self): # GH 25436 the_caption = "a table in a \\texttt{longtable} environment" the_label = "tab:longtable" From c33bfcfb2d49dd2243e685aceacc6984abc1145c Mon Sep 17 00:00:00 2001 From: Sylvain Lannuzel Date: Sat, 25 Jul 2020 09:22:11 +0200 Subject: [PATCH 09/15] write begin on a single line --- pandas/io/formats/latex.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/io/formats/latex.py b/pandas/io/formats/latex.py index 32b52960e21c4..daf92eeb94e97 100644 --- a/pandas/io/formats/latex.py +++ b/pandas/io/formats/latex.py @@ -292,7 +292,6 @@ def _write_tabular_begin(self, buf, column_format: str): or self.position is not None ): # then write output in a nested table/tabular environment - buf.write(f"\\begin{{table}}") if self.caption is None: caption_ = "" else: @@ -308,7 +307,9 @@ def _write_tabular_begin(self, buf, column_format: str): else: position_ = f"[{self.position}]" - buf.write(f"{position_}\n\\centering{caption_}{label_}\n") + buf.write( + f"\\begin{{table}}{position_}\n" f"\\centering{caption_}{label_}\n" + ) else: # then write output only in a tabular environment pass From 4fd1539c4d274d2ff7fa0c174cccd4a4872073e3 Mon Sep 17 00:00:00 2001 From: Sylvain Lannuzel Date: Sat, 25 Jul 2020 09:25:51 +0200 Subject: [PATCH 10/15] group table condition --- pandas/io/formats/latex.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/pandas/io/formats/latex.py b/pandas/io/formats/latex.py index daf92eeb94e97..ecf99349eea61 100644 --- a/pandas/io/formats/latex.py +++ b/pandas/io/formats/latex.py @@ -52,6 +52,7 @@ def __init__( self.label = label self.escape = self.fmt.escape self.position = position + self._table_float = any([p is not None for p in (caption, label, position)]) def write_result(self, buf: IO[str]) -> None: """ @@ -286,11 +287,7 @@ def _write_tabular_begin(self, buf, column_format: str): `__ e.g 'rcl' for 3 columns """ - if ( - self.caption is not None - or self.label is not None - or self.position is not None - ): + if self._table_float: # then write output in a nested table/tabular environment if self.caption is None: caption_ = "" @@ -330,11 +327,7 @@ def _write_tabular_end(self, buf): """ buf.write("\\bottomrule\n") buf.write("\\end{tabular}\n") - if ( - self.caption is not None - or self.label is not None - or self.position is not None - ): + if self._table_float: buf.write("\\end{table}\n") else: pass From d22d7ee6e3cfdf51c3fc4c58dfad3509ddce58d1 Mon Sep 17 00:00:00 2001 From: Sylvain Lannuzel Date: Mon, 27 Jul 2020 08:26:50 +0200 Subject: [PATCH 11/15] linting --- pandas/io/formats/latex.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/io/formats/latex.py b/pandas/io/formats/latex.py index ecf99349eea61..f308c56f391e7 100644 --- a/pandas/io/formats/latex.py +++ b/pandas/io/formats/latex.py @@ -52,7 +52,7 @@ def __init__( self.label = label self.escape = self.fmt.escape self.position = position - self._table_float = any([p is not None for p in (caption, label, position)]) + self._table_float = any(p is not None for p in (caption, label, position)) def write_result(self, buf: IO[str]) -> None: """ @@ -305,7 +305,7 @@ def _write_tabular_begin(self, buf, column_format: str): position_ = f"[{self.position}]" buf.write( - f"\\begin{{table}}{position_}\n" f"\\centering{caption_}{label_}\n" + f"\\begin{{table}}{position_}\n\\centering{caption_}{label_}\n" ) else: # then write output only in a tabular environment From bd55f50b4765897b14e5320da9fda85fa779cdb3 Mon Sep 17 00:00:00 2001 From: Sylvain Lannuzel Date: Mon, 27 Jul 2020 09:10:55 +0200 Subject: [PATCH 12/15] reformat longtable to match table --- pandas/io/formats/latex.py | 41 +++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/pandas/io/formats/latex.py b/pandas/io/formats/latex.py index f308c56f391e7..7572b3e1700b9 100644 --- a/pandas/io/formats/latex.py +++ b/pandas/io/formats/latex.py @@ -304,9 +304,7 @@ def _write_tabular_begin(self, buf, column_format: str): else: position_ = f"[{self.position}]" - buf.write( - f"\\begin{{table}}{position_}\n\\centering{caption_}{label_}\n" - ) + buf.write(f"\\begin{{table}}{position_}\n\\centering{caption_}{label_}\n") else: # then write output only in a tabular environment pass @@ -347,29 +345,30 @@ def _write_longtable_begin(self, buf, column_format: str): `__ e.g 'rcl' for 3 columns """ + + if self.caption is None: + caption_ = "" + else: + caption_ = f"\\caption{{{self.caption}}}" + + if self.label is None: + label_ = "" + else: + label_ = f"\\label{{{self.label}}}" + if self.position is None: position_ = "" else: position_ = f"[{self.position}]" - buf.write(f"\\begin{{longtable}}{position_}{{{column_format}}}\n") - - if self.caption is not None or self.label is not None: - if self.caption is None: - pass - else: - buf.write(f"\\caption{{{self.caption}}}") - - if self.label is None: - pass - else: - buf.write(f"\\label{{{self.label}}}") - - # a double-backslash is required at the end of the line - # as discussed here: - # https://tex.stackexchange.com/questions/219138 - buf.write("\\\\\n") + if self.position is None: + position_ = "" else: - pass + position_ = f"[{self.position}]" + buf.write( + f"\\begin{{longtable}}{position_}{{{column_format}}}\n{caption_}{label_}" + ) + if self._table_float: + buf.write("\\\\\n") @staticmethod def _write_longtable_end(buf): From 92ee692d393626fb0f48af1be9a714d9bd69c8f4 Mon Sep 17 00:00:00 2001 From: Sylvain Lannuzel Date: Mon, 27 Jul 2020 09:11:15 +0200 Subject: [PATCH 13/15] update test to match _table_float --- pandas/tests/io/formats/test_to_latex.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/io/formats/test_to_latex.py b/pandas/tests/io/formats/test_to_latex.py index 93ad3739e59c7..20deca847bdb9 100644 --- a/pandas/tests/io/formats/test_to_latex.py +++ b/pandas/tests/io/formats/test_to_latex.py @@ -604,6 +604,7 @@ def test_to_latex_longtable_position(self): result_p = df.to_latex(longtable=True, position=the_position) expected_p = r"""\begin{longtable}[t]{lrl} +\\ \toprule {} & a & b \\ \midrule From 644bdf716f0d342a26cf539d1458d8b861a89ece Mon Sep 17 00:00:00 2001 From: Sylvain Lannuzel Date: Mon, 27 Jul 2020 09:24:12 +0200 Subject: [PATCH 14/15] remove double backlash for position argument --- pandas/io/formats/latex.py | 6 ++++-- pandas/tests/io/formats/test_to_latex.py | 1 - 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/io/formats/latex.py b/pandas/io/formats/latex.py index 7572b3e1700b9..819910fd2e81d 100644 --- a/pandas/io/formats/latex.py +++ b/pandas/io/formats/latex.py @@ -345,7 +345,6 @@ def _write_longtable_begin(self, buf, column_format: str): `__ e.g 'rcl' for 3 columns """ - if self.caption is None: caption_ = "" else: @@ -367,7 +366,10 @@ def _write_longtable_begin(self, buf, column_format: str): buf.write( f"\\begin{{longtable}}{position_}{{{column_format}}}\n{caption_}{label_}" ) - if self._table_float: + if self.caption is not None or self.label is not None: + # a double-backslash is required at the end of the line + # as discussed here: + # https://tex.stackexchange.com/questions/219138 buf.write("\\\\\n") @staticmethod diff --git a/pandas/tests/io/formats/test_to_latex.py b/pandas/tests/io/formats/test_to_latex.py index 20deca847bdb9..93ad3739e59c7 100644 --- a/pandas/tests/io/formats/test_to_latex.py +++ b/pandas/tests/io/formats/test_to_latex.py @@ -604,7 +604,6 @@ def test_to_latex_longtable_position(self): result_p = df.to_latex(longtable=True, position=the_position) expected_p = r"""\begin{longtable}[t]{lrl} -\\ \toprule {} & a & b \\ \midrule From 31330d27e20d5196a8f874c7a9a1ae422f4a193a Mon Sep 17 00:00:00 2001 From: Sylvain Lannuzel Date: Wed, 29 Jul 2020 08:08:22 +0200 Subject: [PATCH 15/15] duplicated lines --- pandas/io/formats/latex.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pandas/io/formats/latex.py b/pandas/io/formats/latex.py index 819910fd2e81d..5d6f0a08ef2b5 100644 --- a/pandas/io/formats/latex.py +++ b/pandas/io/formats/latex.py @@ -359,10 +359,7 @@ def _write_longtable_begin(self, buf, column_format: str): position_ = "" else: position_ = f"[{self.position}]" - if self.position is None: - position_ = "" - else: - position_ = f"[{self.position}]" + buf.write( f"\\begin{{longtable}}{position_}{{{column_format}}}\n{caption_}{label_}" )