Skip to content

Commit 286f600

Browse files
committed
Enable builder pattern for various types of tables
This reverts ``pandas/io/formats/format.py`` to its nearly original state and enables polymorphism under the hood inside ``pandas/io/formats/latex.py``. Add ``pandas/tests/io/formats/test_latex.py`` to test lower-level functions/classes declared in ``pandas/io/formats/latex.py``
1 parent 83fae7d commit 286f600

File tree

3 files changed

+272
-143
lines changed

3 files changed

+272
-143
lines changed

pandas/io/formats/format.py

+4-19
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,10 @@ def to_latex(
936936
"""
937937
Render a DataFrame to a LaTeX tabular/longtable environment output.
938938
"""
939-
latex_formatter = self._create_latex_formatter(
939+
from pandas.io.formats.latex import LatexFormatter
940+
941+
latex_formatter = LatexFormatter(
942+
self,
940943
longtable=longtable,
941944
column_format=column_format,
942945
multicolumn=multicolumn,
@@ -948,24 +951,6 @@ def to_latex(
948951
)
949952
return latex_formatter.get_result(buf=buf, encoding=encoding)
950953

951-
def _create_latex_formatter(self, **kwargs):
952-
"""Create concrete instance of LatexFormatter."""
953-
from pandas.io.formats.latex import (
954-
LatexLongTableFormatter,
955-
LatexTableFormatter,
956-
LatexTabularFormatter,
957-
)
958-
959-
is_longtable = kwargs.pop("longtable")
960-
is_table = any(
961-
[kwargs.get("caption"), kwargs.get("label"), kwargs.get("position")]
962-
)
963-
if is_longtable:
964-
return LatexLongTableFormatter(self, **kwargs)
965-
if is_table:
966-
return LatexTableFormatter(self, **kwargs)
967-
return LatexTabularFormatter(self, **kwargs)
968-
969954
def _format_col(self, i: int) -> List[str]:
970955
frame = self.tr_frame
971956
formatter = self._get_formatter(i)

0 commit comments

Comments
 (0)