Skip to content

Commit 104f968

Browse files
committed
Move strcols from property to attr for performance
1 parent 280da38 commit 104f968

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

pandas/io/formats/latex.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,17 @@ def __init__(
9292
self.label = label
9393
self.position = position
9494

95+
self.strcols = self._get_strcols()
96+
self.strrows = list(zip(*self.strcols))
97+
9598
def write_result(self, buf: IO[str]) -> None:
9699
"""
97100
Render a DataFrame to a LaTeX tabular, longtable, or table/tabular
98101
environment output.
99102
"""
100103
buf.write(self._compose_string())
101104

102-
@property
103-
def strcols(self):
105+
def _get_strcols(self):
104106
"""String representation of the columns."""
105107
if len(self.frame.columns) == 0 or len(self.frame.index) == 0:
106108
info_line = (
@@ -145,10 +147,6 @@ def pad_empties(x):
145147
strcols = out + strcols[1:]
146148
return strcols
147149

148-
@property
149-
def strrows(self):
150-
return list(zip(*self.strcols))
151-
152150
@property
153151
def column_format(self):
154152
return self._column_format
@@ -242,8 +240,7 @@ def _compose_env_body(self):
242240
return "\n".join(body_list)
243241

244242
def _compose_row(self, row_num):
245-
strrows = self.strrows
246-
row = strrows[row_num]
243+
row = self.strrows[row_num]
247244

248245
is_multicol = row_num < self._clevels and self.fmt.header and self.multicolumn
249246

@@ -261,7 +258,7 @@ def _compose_row(self, row_num):
261258
if is_multicol:
262259
crow = self._format_multicolumn(crow)
263260
if is_multirow:
264-
crow = self._format_multirow(crow, row_num, strrows)
261+
crow = self._format_multirow(crow, row_num)
265262

266263
lst = []
267264
lst.append(" & ".join(crow))
@@ -347,9 +344,7 @@ def append_col():
347344
append_col()
348345
return row2
349346

350-
def _format_multirow(
351-
self, row: List[str], i: int, rows: List[Tuple[str, ...]]
352-
) -> List[str]:
347+
def _format_multirow(self, row: List[str], i: int) -> List[str]:
353348
r"""
354349
Check following rows, whether row should be a multirow
355350
@@ -362,7 +357,7 @@ def _format_multirow(
362357
for j in range(self._ilevels):
363358
if row[j].strip():
364359
nrow = 1
365-
for r in rows[i + 1 :]:
360+
for r in self.strrows[i + 1 :]:
366361
if not r[j].strip():
367362
nrow += 1
368363
else:

0 commit comments

Comments
 (0)