Skip to content

Commit 712b7a0

Browse files
skvrahulluckyvs1
authored andcommitted
BUG: Fix Index.__repr__ when display.max_seq_items = 1 (pandas-dev#38443)
1 parent 04e6113 commit 712b7a0

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ MultiIndex
250250
I/O
251251
^^^
252252

253+
- Bug in :meth:`Index.__repr__` when ``display.max_seq_items=1`` (:issue:`38415`)
253254
- Bug in :func:`read_csv` interpreting ``NA`` value as comment, when ``NA`` does contain the comment string fixed for ``engine="python"`` (:issue:`34002`)
254255
- Bug in :func:`read_csv` raising ``IndexError`` with multiple header columns and ``index_col`` specified when file has no data rows (:issue:`38292`)
255256
- Bug in :func:`read_csv` not accepting ``usecols`` with different length than ``names`` for ``engine="python"`` (:issue:`16469`)

pandas/io/formats/printing.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,11 @@ def best_len(values: List[str]) -> int:
382382
summary = f"[{first}, {last}]{close}"
383383
else:
384384

385-
if n > max_seq_items:
385+
if max_seq_items == 1:
386+
# If max_seq_items=1 show only last element
387+
head = []
388+
tail = [formatter(x) for x in obj[-1:]]
389+
elif n > max_seq_items:
386390
n = min(max_seq_items // 2, 10)
387391
head = [formatter(x) for x in obj[:n]]
388392
tail = [formatter(x) for x in obj[-n:]]

pandas/tests/groupby/test_groupby.py

+1
Original file line numberDiff line numberDiff line change
@@ -2058,6 +2058,7 @@ def test_groupby_list_level():
20582058
[
20592059
(5, "{0: [0], 1: [1], 2: [2], 3: [3], 4: [4]}"),
20602060
(4, "{0: [0], 1: [1], 2: [2], 3: [3], ...}"),
2061+
(1, "{0: [0], ...}"),
20612062
],
20622063
)
20632064
def test_groups_repr_truncates(max_seq_items, expected):

pandas/tests/indexes/multi/test_formats.py

+23
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,20 @@ def test_unicode_repr_issues(self):
8989
# NumPy bug
9090
# repr(index.get_level_values(1))
9191

92+
def test_repr_max_seq_items_equal_to_n(self, idx):
93+
# display.max_seq_items == n
94+
with pd.option_context("display.max_seq_items", 6):
95+
result = idx.__repr__()
96+
expected = """\
97+
MultiIndex([('foo', 'one'),
98+
('foo', 'two'),
99+
('bar', 'one'),
100+
('baz', 'two'),
101+
('qux', 'one'),
102+
('qux', 'two')],
103+
names=['first', 'second'])"""
104+
assert result == expected
105+
92106
def test_repr(self, idx):
93107
result = idx[:1].__repr__()
94108
expected = """\
@@ -118,6 +132,15 @@ def test_repr(self, idx):
118132
names=['first', 'second'], length=6)"""
119133
assert result == expected
120134

135+
# display.max_seq_items == 1
136+
with pd.option_context("display.max_seq_items", 1):
137+
result = idx.__repr__()
138+
expected = """\
139+
MultiIndex([...
140+
('qux', 'two')],
141+
names=['first', ...], length=6)"""
142+
assert result == expected
143+
121144
def test_rjust(self, narrow_multi_index):
122145
mi = narrow_multi_index
123146
result = mi[:1].__repr__()

pandas/tests/io/formats/test_format.py

+3
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ def test_repr_obeys_max_seq_limit(self):
297297
with option_context("display.max_seq_items", 5):
298298
assert len(printing.pprint_thing(list(range(1000)))) < 100
299299

300+
with option_context("display.max_seq_items", 1):
301+
assert len(printing.pprint_thing(list(range(1000)))) < 9
302+
300303
def test_repr_set(self):
301304
assert printing.pprint_thing({1}) == "{1}"
302305

0 commit comments

Comments
 (0)