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..f07b06acbfbdb --- /dev/null +++ b/pandas/tests/indexes/base_class/test_formats.py @@ -0,0 +1,134 @@ +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 + + 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 2e3a70e8c2215..53467819c3ba0 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) @@ -2265,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