Skip to content

Commit

Permalink
BUG: fix ValueError when printing a Series with DataFrame in its attrs (
Browse files Browse the repository at this point in the history
#60574)

* Add test

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

* Add note
  • Loading branch information
yuanx749 authored Dec 16, 2024
1 parent d41884b commit 8e119a7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
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

0 comments on commit 8e119a7

Please sign in to comment.