From b0b363d97e6d5ca2060c4f3ca8fa930589da724e Mon Sep 17 00:00:00 2001 From: Rahul Sathanapalli Date: Sun, 13 Dec 2020 14:00:14 +0530 Subject: [PATCH 1/9] Handles case in printing summary when max_seq_items=0 --- pandas/io/formats/printing.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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:]] From bbb19bd9b9e058a7a796fe0387a007cecf2ee1b2 Mon Sep 17 00:00:00 2001 From: Rahul Sathanapalli Date: Sun, 13 Dec 2020 14:38:55 +0530 Subject: [PATCH 2/9] Adds whatsnew entry --- doc/source/whatsnew/v1.3.0.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index ab9f303bec6aa..6d43d51291794 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -75,8 +75,10 @@ Performance improvements Bug fixes ~~~~~~~~~ -- -- +I/O +^^^ + +- Bug in :meth:`Index.__repr__` when ``display.max_seq_items=1`` (:issue:`38415`) Categorical ^^^^^^^^^^^ From cb829fbbc13aaf54ece8b011b31a8bbb858ada45 Mon Sep 17 00:00:00 2001 From: Rahul Sathanapalli Date: Sun, 13 Dec 2020 23:28:19 +0530 Subject: [PATCH 3/9] Removes duplicate I/O section --- doc/source/whatsnew/v1.3.0.rst | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 6d43d51291794..9105225a17404 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -75,10 +75,6 @@ Performance improvements Bug fixes ~~~~~~~~~ -I/O -^^^ - -- Bug in :meth:`Index.__repr__` when ``display.max_seq_items=1`` (:issue:`38415`) Categorical ^^^^^^^^^^^ @@ -149,7 +145,7 @@ MultiIndex I/O ^^^ -- +- Bug in :meth:`Index.__repr__` when ``display.max_seq_items=1`` (:issue:`38415`) - Period From 62506cb6f3490c7a29c0e62776a75df56a59ae68 Mon Sep 17 00:00:00 2001 From: Rahul Sathanapalli Date: Mon, 14 Dec 2020 15:05:07 +0530 Subject: [PATCH 4/9] Adds tests for 'display.max_seq_items' = 1 and 'display.max_seq_items' = n --- pandas/tests/indexes/multi/test_formats.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pandas/tests/indexes/multi/test_formats.py b/pandas/tests/indexes/multi/test_formats.py index c1de7f79c2d2e..a10a1ea022bf9 100644 --- a/pandas/tests/indexes/multi/test_formats.py +++ b/pandas/tests/indexes/multi/test_formats.py @@ -118,6 +118,28 @@ def test_repr(self, idx): names=['first', 'second'], length=6)""" assert result == expected + # 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 + + # 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__() From 539a055678ad4ca5ecd22774c487b436a83d4631 Mon Sep 17 00:00:00 2001 From: Rahul Sathanapalli Date: Wed, 16 Dec 2020 00:43:32 +0530 Subject: [PATCH 5/9] Adds additional tests --- pandas/tests/groupby/test_groupby.py | 1 + pandas/tests/io/formats/test_format.py | 4 ++++ 2 files changed, 5 insertions(+) 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/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index fe85849c6dcca..1ffd022f123b0 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -297,6 +297,10 @@ 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}" From ceefa4538f6a8db77f966c0fbc606c55309501f2 Mon Sep 17 00:00:00 2001 From: Rahul Sathanapalli Date: Wed, 16 Dec 2020 00:46:40 +0530 Subject: [PATCH 6/9] Moves display.max_seq_items == n tests to own test --- pandas/tests/indexes/multi/test_formats.py | 27 +++++++++++----------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/pandas/tests/indexes/multi/test_formats.py b/pandas/tests/indexes/multi/test_formats.py index a10a1ea022bf9..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,19 +132,6 @@ def test_repr(self, idx): names=['first', 'second'], length=6)""" assert result == expected - # 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 - # display.max_seq_items == 1 with pd.option_context("display.max_seq_items", 1): result = idx.__repr__() From 3191e6f1e405fb623edeb15c59867f558876d682 Mon Sep 17 00:00:00 2001 From: Rahul Sathanapalli Date: Wed, 16 Dec 2020 01:01:53 +0530 Subject: [PATCH 7/9] run black --- pandas/tests/io/formats/test_format.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 1ffd022f123b0..02a0c78bb2d17 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -298,8 +298,7 @@ def test_repr_obeys_max_seq_limit(self): 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 - + assert len(printing.pprint_thing(list(range(1000)))) < 9 def test_repr_set(self): assert printing.pprint_thing({1}) == "{1}" From 280518ab0fca0dcf56c21b09b819ad5e767b57e2 Mon Sep 17 00:00:00 2001 From: Rahul Sathanapalli Date: Sat, 26 Dec 2020 20:21:29 +0530 Subject: [PATCH 8/9] Update v1.3.0.rst Revert deleted bullet points --- doc/source/whatsnew/v1.3.0.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 9e71dbcc4ea07..2b4c7fb5e7915 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -143,7 +143,9 @@ Performance improvements Bug fixes ~~~~~~~~~ - + +- +- Categorical ^^^^^^^^^^^ From 4e381bfe53d7115d826a400042462fafcb56e429 Mon Sep 17 00:00:00 2001 From: Rahul Sathanapalli Date: Sat, 26 Dec 2020 20:23:12 +0530 Subject: [PATCH 9/9] Update v1.3.0.rst --- doc/source/whatsnew/v1.3.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 2b4c7fb5e7915..a4260965db442 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -143,7 +143,7 @@ Performance improvements Bug fixes ~~~~~~~~~ - + - -