Skip to content

Commit 8e119a7

Browse files
authored
BUG: fix ValueError when printing a Series with DataFrame in its attrs (#60574)
* Add test * BUG: fix ValueError when printing a Series with DataFrame in its attrs * Add note
1 parent d41884b commit 8e119a7

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,7 @@ Other
802802
- Bug in ``Series.list`` methods not preserving the original :class:`Index`. (:issue:`58425`)
803803
- Bug in ``Series.list`` methods not preserving the original name. (:issue:`60522`)
804804
- Bug in printing a :class:`DataFrame` with a :class:`DataFrame` stored in :attr:`DataFrame.attrs` raised a ``ValueError`` (:issue:`60455`)
805+
- Bug in printing a :class:`Series` with a :class:`DataFrame` stored in :attr:`Series.attrs` raised a ``ValueError`` (:issue:`60568`)
805806

806807
.. ***DO NOT USE THIS SECTION***
807808

pandas/io/formats/format.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
)
7979
from pandas.core.indexes.datetimes import DatetimeIndex
8080
from pandas.core.indexes.timedeltas import TimedeltaIndex
81-
from pandas.core.reshape.concat import concat
8281

8382
from pandas.io.common import (
8483
check_parent_directory,
@@ -245,7 +244,11 @@ def _chk_truncate(self) -> None:
245244
series = series.iloc[:max_rows]
246245
else:
247246
row_num = max_rows // 2
248-
series = concat((series.iloc[:row_num], series.iloc[-row_num:]))
247+
_len = len(series)
248+
_slice = np.hstack(
249+
[np.arange(row_num), np.arange(_len - row_num, _len)]
250+
)
251+
series = series.iloc[_slice]
249252
self.tr_row_num = row_num
250253
else:
251254
self.tr_row_num = None

pandas/tests/io/formats/test_format.py

+7
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ def test_repr_truncation_dataframe_attrs(self):
136136
with option_context("display.max_columns", 2, "display.show_dimensions", False):
137137
assert repr(df) == " 0 ... 9\n0 0 ... 0"
138138

139+
def test_repr_truncation_series_with_dataframe_attrs(self):
140+
# GH#60568
141+
ser = Series([0] * 10)
142+
ser.attrs["b"] = DataFrame([])
143+
with option_context("display.max_rows", 2, "display.show_dimensions", False):
144+
assert repr(ser) == "0 0\n ..\n9 0\ndtype: int64"
145+
139146
def test_max_colwidth_negative_int_raises(self):
140147
# Deprecation enforced from:
141148
# https://github.com/pandas-dev/pandas/issues/31532

0 commit comments

Comments
 (0)