Skip to content

Commit 8911238

Browse files
yuanx749mroeschke
andauthored
BUG: ValueError when printing a DataFrame with DataFrame in its attrs (#60459)
* Add test * replace concat with np * Revert "replace concat with np" This reverts commit b48fc35. * Revert "Revert "replace concat with np"" This reverts commit 6b45ac5. * try fixing mypy error * Add whatsnew --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent d589839 commit 8911238

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,7 @@ Other
797797
- Bug in :meth:`read_csv` where chained fsspec TAR file and ``compression="infer"`` fails with ``tarfile.ReadError`` (:issue:`60028`)
798798
- Bug in Dataframe Interchange Protocol implementation was returning incorrect results for data buffers' associated dtype, for string and datetime columns (:issue:`54781`)
799799
- Bug in ``Series.list`` methods not preserving the original :class:`Index`. (:issue:`58425`)
800+
- Bug in printing a :class:`DataFrame` with a :class:`DataFrame` stored in :attr:`DataFrame.attrs` raised a ``ValueError`` (:issue:`60455`)
800801

801802
.. ***DO NOT USE THIS SECTION***
802803

pandas/io/formats/format.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,9 @@ def _truncate_horizontally(self) -> None:
669669
assert self.max_cols_fitted is not None
670670
col_num = self.max_cols_fitted // 2
671671
if col_num >= 1:
672-
left = self.tr_frame.iloc[:, :col_num]
673-
right = self.tr_frame.iloc[:, -col_num:]
674-
self.tr_frame = concat((left, right), axis=1)
672+
_len = len(self.tr_frame.columns)
673+
_slice = np.hstack([np.arange(col_num), np.arange(_len - col_num, _len)])
674+
self.tr_frame = self.tr_frame.iloc[:, _slice]
675675

676676
# truncate formatter
677677
if isinstance(self.formatters, (list, tuple)):
@@ -682,7 +682,7 @@ def _truncate_horizontally(self) -> None:
682682
else:
683683
col_num = cast(int, self.max_cols)
684684
self.tr_frame = self.tr_frame.iloc[:, :col_num]
685-
self.tr_col_num = col_num
685+
self.tr_col_num: int = col_num
686686

687687
def _truncate_vertically(self) -> None:
688688
"""Remove rows, which are not to be displayed.

pandas/tests/io/formats/test_format.py

+7
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ def test_repr_truncation_preserves_na(self):
129129
with option_context("display.max_rows", 2, "display.show_dimensions", False):
130130
assert repr(df) == " a\n0 <NA>\n.. ...\n9 <NA>"
131131

132+
def test_repr_truncation_dataframe_attrs(self):
133+
# GH#60455
134+
df = DataFrame([[0] * 10])
135+
df.attrs["b"] = DataFrame([])
136+
with option_context("display.max_columns", 2, "display.show_dimensions", False):
137+
assert repr(df) == " 0 ... 9\n0 0 ... 0"
138+
132139
def test_max_colwidth_negative_int_raises(self):
133140
# Deprecation enforced from:
134141
# https://github.com/pandas-dev/pandas/issues/31532

0 commit comments

Comments
 (0)