Skip to content

BUG: ValueError when printing a DataFrame with DataFrame in its attrs #60459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 3, 2024
8 changes: 4 additions & 4 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,9 @@ def _truncate_horizontally(self) -> None:
assert self.max_cols_fitted is not None
col_num = self.max_cols_fitted // 2
if col_num >= 1:
left = self.tr_frame.iloc[:, :col_num]
right = self.tr_frame.iloc[:, -col_num:]
self.tr_frame = concat((left, right), axis=1)
_len = len(self.tr_frame.columns)
_slice = np.hstack([np.arange(col_num), np.arange(_len - col_num, _len)])
self.tr_frame = self.tr_frame.iloc[:, _slice]

# truncate formatter
if isinstance(self.formatters, (list, tuple)):
Expand All @@ -682,7 +682,7 @@ def _truncate_horizontally(self) -> None:
else:
col_num = cast(int, self.max_cols)
self.tr_frame = self.tr_frame.iloc[:, :col_num]
self.tr_col_num = col_num
self.tr_col_num: int = col_num

def _truncate_vertically(self) -> None:
"""Remove rows, which are not to be displayed.
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ def test_repr_truncation_preserves_na(self):
with option_context("display.max_rows", 2, "display.show_dimensions", False):
assert repr(df) == " a\n0 <NA>\n.. ...\n9 <NA>"

def test_repr_truncation_dataframe_attrs(self):
# 60455
df = DataFrame([[0] * 10])
df.attrs["b"] = DataFrame([])
with option_context("display.max_columns", 2, "display.show_dimensions", False):
assert repr(df) == " 0 ... 9\n0 0 ... 0"

def test_max_colwidth_negative_int_raises(self):
# Deprecation enforced from:
# https://github.com/pandas-dev/pandas/issues/31532
Expand Down
Loading