Skip to content

Commit d04b343

Browse files
authored
PERF: fix long string representation (#36638)
1 parent 8142651 commit d04b343

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

pandas/io/formats/format.py

+11-17
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from functools import partial
1111
from io import StringIO
1212
import math
13-
from operator import itemgetter
1413
import re
1514
from shutil import get_terminal_size
1615
from typing import (
@@ -592,6 +591,7 @@ def __init__(
592591
self.max_cols_fitted = self._calc_max_cols_fitted()
593592
self.max_rows_fitted = self._calc_max_rows_fitted()
594593

594+
self.tr_frame = self.frame
595595
self._truncate()
596596
self.adj = get_adjustment()
597597

@@ -730,8 +730,6 @@ def _truncate(self) -> None:
730730
"""
731731
Check whether the frame should be truncated. If so, slice the frame up.
732732
"""
733-
self.tr_frame = self.frame.copy()
734-
735733
if self.is_truncated_horizontally:
736734
self._truncate_horizontally()
737735

@@ -749,17 +747,16 @@ def _truncate_horizontally(self) -> None:
749747
assert self.max_cols_fitted is not None
750748
col_num = self.max_cols_fitted // 2
751749
if col_num >= 1:
752-
cols_to_keep = [
753-
x
754-
for x in range(self.frame.shape[1])
755-
if x < col_num or x >= len(self.frame.columns) - col_num
756-
]
757-
self.tr_frame = self.tr_frame.iloc[:, cols_to_keep]
750+
left = self.tr_frame.iloc[:, :col_num]
751+
right = self.tr_frame.iloc[:, -col_num:]
752+
self.tr_frame = concat((left, right), axis=1)
758753

759754
# truncate formatter
760755
if isinstance(self.formatters, (list, tuple)):
761-
slicer = itemgetter(*cols_to_keep)
762-
self.formatters = slicer(self.formatters)
756+
self.formatters = [
757+
*self.formatters[:col_num],
758+
*self.formatters[-col_num:],
759+
]
763760
else:
764761
col_num = cast(int, self.max_cols)
765762
self.tr_frame = self.tr_frame.iloc[:, :col_num]
@@ -775,12 +772,9 @@ def _truncate_vertically(self) -> None:
775772
assert self.max_rows_fitted is not None
776773
row_num = self.max_rows_fitted // 2
777774
if row_num >= 1:
778-
rows_to_keep = [
779-
x
780-
for x in range(self.frame.shape[0])
781-
if x < row_num or x >= len(self.frame) - row_num
782-
]
783-
self.tr_frame = self.tr_frame.iloc[rows_to_keep, :]
775+
head = self.tr_frame.iloc[:row_num, :]
776+
tail = self.tr_frame.iloc[-row_num:, :]
777+
self.tr_frame = concat((head, tail))
784778
else:
785779
row_num = cast(int, self.max_rows)
786780
self.tr_frame = self.tr_frame.iloc[:row_num, :]

0 commit comments

Comments
 (0)