Skip to content
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

BUG: fix ValueError when printing a Series with DataFrame in its attrs #60574

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ Other
- Bug in ``Series.list`` methods not preserving the original :class:`Index`. (:issue:`58425`)
- Bug in ``Series.list`` methods not preserving the original name. (:issue:`60522`)
- Bug in printing a :class:`DataFrame` with a :class:`DataFrame` stored in :attr:`DataFrame.attrs` raised a ``ValueError`` (:issue:`60455`)
- Bug in printing a :class:`Series` with a :class:`DataFrame` stored in :attr:`Series.attrs` raised a ``ValueError`` (:issue:`60568`)

.. ***DO NOT USE THIS SECTION***

Expand Down
7 changes: 5 additions & 2 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
)
from pandas.core.indexes.datetimes import DatetimeIndex
from pandas.core.indexes.timedeltas import TimedeltaIndex
from pandas.core.reshape.concat import concat

from pandas.io.common import (
check_parent_directory,
Expand Down Expand Up @@ -245,7 +244,11 @@ def _chk_truncate(self) -> None:
series = series.iloc[:max_rows]
else:
row_num = max_rows // 2
series = concat((series.iloc[:row_num], series.iloc[-row_num:]))
_len = len(series)
_slice = np.hstack(
[np.arange(row_num), np.arange(_len - row_num, _len)]
)
series = series.iloc[_slice]
self.tr_row_num = row_num
else:
self.tr_row_num = None
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 @@ -136,6 +136,13 @@ def test_repr_truncation_dataframe_attrs(self):
with option_context("display.max_columns", 2, "display.show_dimensions", False):
assert repr(df) == " 0 ... 9\n0 0 ... 0"

def test_repr_truncation_series_with_dataframe_attrs(self):
# GH#60568
ser = Series([0] * 10)
ser.attrs["b"] = DataFrame([])
with option_context("display.max_rows", 2, "display.show_dimensions", False):
assert repr(ser) == "0 0\n ..\n9 0\ndtype: int64"

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