diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index d62356e45b723..a4260965db442 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -217,6 +217,7 @@ MultiIndex I/O ^^^ +- Bug in :meth:`Index.__repr__` when ``display.max_seq_items=1`` (:issue:`38415`) - Bug in :func:`read_csv` interpreting ``NA`` value as comment, when ``NA`` does contain the comment string fixed for ``engine="python"`` (:issue:`34002`) - Bug in :func:`read_csv` raising ``IndexError`` with multiple header columns and ``index_col`` specified when file has no data rows (:issue:`38292`) - Bug in :func:`read_csv` not accepting ``usecols`` with different length than ``names`` for ``engine="python"`` (:issue:`16469`) diff --git a/pandas/io/formats/printing.py b/pandas/io/formats/printing.py index 128e50d84657c..acb17aee50b76 100644 --- a/pandas/io/formats/printing.py +++ b/pandas/io/formats/printing.py @@ -382,7 +382,11 @@ def best_len(values: List[str]) -> int: summary = f"[{first}, {last}]{close}" else: - if n > max_seq_items: + if max_seq_items == 1: + # If max_seq_items=1 show only last element + head = [] + tail = [formatter(x) for x in obj[-1:]] + elif n > max_seq_items: n = min(max_seq_items // 2, 10) head = [formatter(x) for x in obj[:n]] tail = [formatter(x) for x in obj[-n:]] diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 7c179a79513fa..f8a9412d3036d 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2058,6 +2058,7 @@ def test_groupby_list_level(): [ (5, "{0: [0], 1: [1], 2: [2], 3: [3], 4: [4]}"), (4, "{0: [0], 1: [1], 2: [2], 3: [3], ...}"), + (1, "{0: [0], ...}"), ], ) def test_groups_repr_truncates(max_seq_items, expected): diff --git a/pandas/tests/indexes/multi/test_formats.py b/pandas/tests/indexes/multi/test_formats.py index c1de7f79c2d2e..4e56b7c73c64f 100644 --- a/pandas/tests/indexes/multi/test_formats.py +++ b/pandas/tests/indexes/multi/test_formats.py @@ -89,6 +89,20 @@ def test_unicode_repr_issues(self): # NumPy bug # repr(index.get_level_values(1)) + def test_repr_max_seq_items_equal_to_n(self, idx): + # display.max_seq_items == n + with pd.option_context("display.max_seq_items", 6): + result = idx.__repr__() + expected = """\ +MultiIndex([('foo', 'one'), + ('foo', 'two'), + ('bar', 'one'), + ('baz', 'two'), + ('qux', 'one'), + ('qux', 'two')], + names=['first', 'second'])""" + assert result == expected + def test_repr(self, idx): result = idx[:1].__repr__() expected = """\ @@ -118,6 +132,15 @@ def test_repr(self, idx): names=['first', 'second'], length=6)""" assert result == expected + # display.max_seq_items == 1 + with pd.option_context("display.max_seq_items", 1): + result = idx.__repr__() + expected = """\ +MultiIndex([... + ('qux', 'two')], + names=['first', ...], length=6)""" + assert result == expected + def test_rjust(self, narrow_multi_index): mi = narrow_multi_index result = mi[:1].__repr__() diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index fe85849c6dcca..02a0c78bb2d17 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -297,6 +297,9 @@ def test_repr_obeys_max_seq_limit(self): with option_context("display.max_seq_items", 5): assert len(printing.pprint_thing(list(range(1000)))) < 100 + with option_context("display.max_seq_items", 1): + assert len(printing.pprint_thing(list(range(1000)))) < 9 + def test_repr_set(self): assert printing.pprint_thing({1}) == "{1}"