From 759ce15d91e553bd2a4ce3a7d9c6952d8c9ee74e Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 24 Nov 2020 16:11:33 -0800 Subject: [PATCH 1/2] TST: implement test_formats for Index --- .../tests/indexes/base_class/test_formats.py | 123 ++++++++++++++++++ pandas/tests/indexes/test_base.py | 117 ----------------- 2 files changed, 123 insertions(+), 117 deletions(-) create mode 100644 pandas/tests/indexes/base_class/test_formats.py diff --git a/pandas/tests/indexes/base_class/test_formats.py b/pandas/tests/indexes/base_class/test_formats.py new file mode 100644 index 0000000000000..b02e5ec977536 --- /dev/null +++ b/pandas/tests/indexes/base_class/test_formats.py @@ -0,0 +1,123 @@ +import numpy as np +import pytest + +import pandas._config.config as cf + +from pandas import Index + + +class TestIndexRendering: + @pytest.mark.parametrize( + "index,expected", + [ + # ASCII + # short + ( + Index(["a", "bb", "ccc"]), + """Index(['a', 'bb', 'ccc'], dtype='object')""", + ), + # multiple lines + ( + Index(["a", "bb", "ccc"] * 10), + "Index(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', " + "'bb', 'ccc', 'a', 'bb', 'ccc',\n" + " 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', " + "'bb', 'ccc', 'a', 'bb', 'ccc',\n" + " 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],\n" + " dtype='object')", + ), + # truncated + ( + Index(["a", "bb", "ccc"] * 100), + "Index(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a',\n" + " ...\n" + " 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],\n" + " dtype='object', length=300)", + ), + # Non-ASCII + # short + ( + Index(["あ", "いい", "ううう"]), + """Index(['あ', 'いい', 'ううう'], dtype='object')""", + ), + # multiple lines + ( + Index(["あ", "いい", "ううう"] * 10), + ( + "Index(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', " + "'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',\n" + " 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', " + "'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',\n" + " 'あ', 'いい', 'ううう', 'あ', 'いい', " + "'ううう'],\n" + " dtype='object')" + ), + ), + # truncated + ( + Index(["あ", "いい", "ううう"] * 100), + ( + "Index(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', " + "'あ', 'いい', 'ううう', 'あ',\n" + " ...\n" + " 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', " + "'ううう', 'あ', 'いい', 'ううう'],\n" + " dtype='object', length=300)" + ), + ), + ], + ) + def test_string_index_repr(self, index, expected): + result = repr(index) + assert result == expected + + @pytest.mark.parametrize( + "index,expected", + [ + # short + ( + Index(["あ", "いい", "ううう"]), + ("Index(['あ', 'いい', 'ううう'], dtype='object')"), + ), + # multiple lines + ( + Index(["あ", "いい", "ううう"] * 10), + ( + "Index(['あ', 'いい', 'ううう', 'あ', 'いい', " + "'ううう', 'あ', 'いい', 'ううう',\n" + " 'あ', 'いい', 'ううう', 'あ', 'いい', " + "'ううう', 'あ', 'いい', 'ううう',\n" + " 'あ', 'いい', 'ううう', 'あ', 'いい', " + "'ううう', 'あ', 'いい', 'ううう',\n" + " 'あ', 'いい', 'ううう'],\n" + " dtype='object')" + "" + ), + ), + # truncated + ( + Index(["あ", "いい", "ううう"] * 100), + ( + "Index(['あ', 'いい', 'ううう', 'あ', 'いい', " + "'ううう', 'あ', 'いい', 'ううう',\n" + " 'あ',\n" + " ...\n" + " 'ううう', 'あ', 'いい', 'ううう', 'あ', " + "'いい', 'ううう', 'あ', 'いい',\n" + " 'ううう'],\n" + " dtype='object', length=300)" + ), + ), + ], + ) + def test_string_index_repr_with_unicode_option(self, index, expected): + # Enable Unicode option ----------------------------------------- + with cf.option_context("display.unicode.east_asian_width", True): + result = repr(index) + assert result == expected + + def test_repr_summary(self): + with cf.option_context("display.max_seq_items", 10): + result = repr(Index(np.arange(1000))) + assert len(result) < 200 + assert "..." in result diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 2e3a70e8c2215..b147f5bff3206 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -8,8 +8,6 @@ import numpy as np import pytest -import pandas._config.config as cf - from pandas._libs.tslib import Timestamp from pandas.compat.numpy import np_datetime64_compat from pandas.util._test_decorators import async_mark @@ -1907,115 +1905,6 @@ def test_dt_conversion_preserves_name(self, dt_conv): index = Index(["01:02:03", "01:02:04"], name="label") assert index.name == dt_conv(index).name - @pytest.mark.parametrize( - "index,expected", - [ - # ASCII - # short - ( - Index(["a", "bb", "ccc"]), - """Index(['a', 'bb', 'ccc'], dtype='object')""", - ), - # multiple lines - ( - Index(["a", "bb", "ccc"] * 10), - """\ -Index(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', - 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', - 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'], - dtype='object')""", - ), - # truncated - ( - Index(["a", "bb", "ccc"] * 100), - """\ -Index(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', - ... - 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'], - dtype='object', length=300)""", - ), - # Non-ASCII - # short - ( - Index(["あ", "いい", "ううう"]), - """Index(['あ', 'いい', 'ううう'], dtype='object')""", - ), - # multiple lines - ( - Index(["あ", "いい", "ううう"] * 10), - ( - "Index(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', " - "'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',\n" - " 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', " - "'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',\n" - " 'あ', 'いい', 'ううう', 'あ', 'いい', " - "'ううう'],\n" - " dtype='object')" - ), - ), - # truncated - ( - Index(["あ", "いい", "ううう"] * 100), - ( - "Index(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', " - "'あ', 'いい', 'ううう', 'あ',\n" - " ...\n" - " 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', " - "'ううう', 'あ', 'いい', 'ううう'],\n" - " dtype='object', length=300)" - ), - ), - ], - ) - def test_string_index_repr(self, index, expected): - result = repr(index) - assert result == expected - - @pytest.mark.parametrize( - "index,expected", - [ - # short - ( - Index(["あ", "いい", "ううう"]), - ("Index(['あ', 'いい', 'ううう'], dtype='object')"), - ), - # multiple lines - ( - Index(["あ", "いい", "ううう"] * 10), - ( - "Index(['あ', 'いい', 'ううう', 'あ', 'いい', " - "'ううう', 'あ', 'いい', 'ううう',\n" - " 'あ', 'いい', 'ううう', 'あ', 'いい', " - "'ううう', 'あ', 'いい', 'ううう',\n" - " 'あ', 'いい', 'ううう', 'あ', 'いい', " - "'ううう', 'あ', 'いい', 'ううう',\n" - " 'あ', 'いい', 'ううう'],\n" - " dtype='object')" - "" - ), - ), - # truncated - ( - Index(["あ", "いい", "ううう"] * 100), - ( - "Index(['あ', 'いい', 'ううう', 'あ', 'いい', " - "'ううう', 'あ', 'いい', 'ううう',\n" - " 'あ',\n" - " ...\n" - " 'ううう', 'あ', 'いい', 'ううう', 'あ', " - "'いい', 'ううう', 'あ', 'いい',\n" - " 'ううう'],\n" - " dtype='object', length=300)" - ), - ), - ], - ) - def test_string_index_repr_with_unicode_option(self, index, expected): - # Enable Unicode option ----------------------------------------- - with cf.option_context("display.unicode.east_asian_width", True): - result = repr(index) - assert result == expected - def test_cached_properties_not_settable(self): index = Index([1, 2, 3]) with pytest.raises(AttributeError, match="Can't set attribute"): @@ -2236,12 +2125,6 @@ def test_is_monotonic_na(self, index): assert index._is_strictly_monotonic_increasing is False assert index._is_strictly_monotonic_decreasing is False - def test_repr_summary(self): - with cf.option_context("display.max_seq_items", 10): - result = repr(Index(np.arange(1000))) - assert len(result) < 200 - assert "..." in result - @pytest.mark.parametrize("klass", [Series, DataFrame]) def test_int_name_format(self, klass): index = Index(["a", "b", "c"], name=0) From 4897b23b18570e2ec0214c0f59065e68d57825e6 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 24 Nov 2020 17:21:32 -0800 Subject: [PATCH 2/2] TST/REF: collect Index formatting tests --- pandas/tests/indexes/base_class/test_formats.py | 11 +++++++++++ pandas/tests/indexes/test_base.py | 11 ----------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pandas/tests/indexes/base_class/test_formats.py b/pandas/tests/indexes/base_class/test_formats.py index b02e5ec977536..f07b06acbfbdb 100644 --- a/pandas/tests/indexes/base_class/test_formats.py +++ b/pandas/tests/indexes/base_class/test_formats.py @@ -121,3 +121,14 @@ def test_repr_summary(self): result = repr(Index(np.arange(1000))) assert len(result) < 200 assert "..." in result + + def test_index_repr_bool_nan(self): + # GH32146 + arr = Index([True, False, np.nan], dtype=object) + exp1 = arr.format() + out1 = ["True", "False", "NaN"] + assert out1 == exp1 + + exp2 = repr(arr) + out2 = "Index([True, False, nan], dtype='object')" + assert out2 == exp2 diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index b147f5bff3206..53467819c3ba0 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -2148,17 +2148,6 @@ def test_intersect_str_dates(self): expected = Index([], dtype=object) tm.assert_index_equal(result, expected) - def test_index_repr_bool_nan(self): - # GH32146 - arr = Index([True, False, np.nan], dtype=object) - exp1 = arr.format() - out1 = ["True", "False", "NaN"] - assert out1 == exp1 - - exp2 = repr(arr) - out2 = "Index([True, False, nan], dtype='object')" - assert out2 == exp2 - @pytest.mark.filterwarnings("ignore:elementwise comparison failed:FutureWarning") def test_index_with_tuple_bool(self): # GH34123