Skip to content

Commit 2f1465f

Browse files
authored
TST/REF: collect Index formatting tests (#38050)
1 parent 08913f8 commit 2f1465f

File tree

2 files changed

+134
-128
lines changed

2 files changed

+134
-128
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import numpy as np
2+
import pytest
3+
4+
import pandas._config.config as cf
5+
6+
from pandas import Index
7+
8+
9+
class TestIndexRendering:
10+
@pytest.mark.parametrize(
11+
"index,expected",
12+
[
13+
# ASCII
14+
# short
15+
(
16+
Index(["a", "bb", "ccc"]),
17+
"""Index(['a', 'bb', 'ccc'], dtype='object')""",
18+
),
19+
# multiple lines
20+
(
21+
Index(["a", "bb", "ccc"] * 10),
22+
"Index(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', "
23+
"'bb', 'ccc', 'a', 'bb', 'ccc',\n"
24+
" 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', "
25+
"'bb', 'ccc', 'a', 'bb', 'ccc',\n"
26+
" 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],\n"
27+
" dtype='object')",
28+
),
29+
# truncated
30+
(
31+
Index(["a", "bb", "ccc"] * 100),
32+
"Index(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a',\n"
33+
" ...\n"
34+
" 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],\n"
35+
" dtype='object', length=300)",
36+
),
37+
# Non-ASCII
38+
# short
39+
(
40+
Index(["あ", "いい", "ううう"]),
41+
"""Index(['あ', 'いい', 'ううう'], dtype='object')""",
42+
),
43+
# multiple lines
44+
(
45+
Index(["あ", "いい", "ううう"] * 10),
46+
(
47+
"Index(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', "
48+
"'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',\n"
49+
" 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', "
50+
"'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',\n"
51+
" 'あ', 'いい', 'ううう', 'あ', 'いい', "
52+
"'ううう'],\n"
53+
" dtype='object')"
54+
),
55+
),
56+
# truncated
57+
(
58+
Index(["あ", "いい", "ううう"] * 100),
59+
(
60+
"Index(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', "
61+
"'あ', 'いい', 'ううう', 'あ',\n"
62+
" ...\n"
63+
" 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', "
64+
"'ううう', 'あ', 'いい', 'ううう'],\n"
65+
" dtype='object', length=300)"
66+
),
67+
),
68+
],
69+
)
70+
def test_string_index_repr(self, index, expected):
71+
result = repr(index)
72+
assert result == expected
73+
74+
@pytest.mark.parametrize(
75+
"index,expected",
76+
[
77+
# short
78+
(
79+
Index(["あ", "いい", "ううう"]),
80+
("Index(['あ', 'いい', 'ううう'], dtype='object')"),
81+
),
82+
# multiple lines
83+
(
84+
Index(["あ", "いい", "ううう"] * 10),
85+
(
86+
"Index(['あ', 'いい', 'ううう', 'あ', 'いい', "
87+
"'ううう', 'あ', 'いい', 'ううう',\n"
88+
" 'あ', 'いい', 'ううう', 'あ', 'いい', "
89+
"'ううう', 'あ', 'いい', 'ううう',\n"
90+
" 'あ', 'いい', 'ううう', 'あ', 'いい', "
91+
"'ううう', 'あ', 'いい', 'ううう',\n"
92+
" 'あ', 'いい', 'ううう'],\n"
93+
" dtype='object')"
94+
""
95+
),
96+
),
97+
# truncated
98+
(
99+
Index(["あ", "いい", "ううう"] * 100),
100+
(
101+
"Index(['あ', 'いい', 'ううう', 'あ', 'いい', "
102+
"'ううう', 'あ', 'いい', 'ううう',\n"
103+
" 'あ',\n"
104+
" ...\n"
105+
" 'ううう', 'あ', 'いい', 'ううう', 'あ', "
106+
"'いい', 'ううう', 'あ', 'いい',\n"
107+
" 'ううう'],\n"
108+
" dtype='object', length=300)"
109+
),
110+
),
111+
],
112+
)
113+
def test_string_index_repr_with_unicode_option(self, index, expected):
114+
# Enable Unicode option -----------------------------------------
115+
with cf.option_context("display.unicode.east_asian_width", True):
116+
result = repr(index)
117+
assert result == expected
118+
119+
def test_repr_summary(self):
120+
with cf.option_context("display.max_seq_items", 10):
121+
result = repr(Index(np.arange(1000)))
122+
assert len(result) < 200
123+
assert "..." in result
124+
125+
def test_index_repr_bool_nan(self):
126+
# GH32146
127+
arr = Index([True, False, np.nan], dtype=object)
128+
exp1 = arr.format()
129+
out1 = ["True", "False", "NaN"]
130+
assert out1 == exp1
131+
132+
exp2 = repr(arr)
133+
out2 = "Index([True, False, nan], dtype='object')"
134+
assert out2 == exp2

pandas/tests/indexes/test_base.py

Lines changed: 0 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import numpy as np
99
import pytest
1010

11-
import pandas._config.config as cf
12-
1311
from pandas._libs.tslib import Timestamp
1412
from pandas.compat.numpy import np_datetime64_compat
1513
from pandas.util._test_decorators import async_mark
@@ -1907,115 +1905,6 @@ def test_dt_conversion_preserves_name(self, dt_conv):
19071905
index = Index(["01:02:03", "01:02:04"], name="label")
19081906
assert index.name == dt_conv(index).name
19091907

1910-
@pytest.mark.parametrize(
1911-
"index,expected",
1912-
[
1913-
# ASCII
1914-
# short
1915-
(
1916-
Index(["a", "bb", "ccc"]),
1917-
"""Index(['a', 'bb', 'ccc'], dtype='object')""",
1918-
),
1919-
# multiple lines
1920-
(
1921-
Index(["a", "bb", "ccc"] * 10),
1922-
"""\
1923-
Index(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc',
1924-
'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc',
1925-
'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],
1926-
dtype='object')""",
1927-
),
1928-
# truncated
1929-
(
1930-
Index(["a", "bb", "ccc"] * 100),
1931-
"""\
1932-
Index(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a',
1933-
...
1934-
'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],
1935-
dtype='object', length=300)""",
1936-
),
1937-
# Non-ASCII
1938-
# short
1939-
(
1940-
Index(["あ", "いい", "ううう"]),
1941-
"""Index(['あ', 'いい', 'ううう'], dtype='object')""",
1942-
),
1943-
# multiple lines
1944-
(
1945-
Index(["あ", "いい", "ううう"] * 10),
1946-
(
1947-
"Index(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', "
1948-
"'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',\n"
1949-
" 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', "
1950-
"'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',\n"
1951-
" 'あ', 'いい', 'ううう', 'あ', 'いい', "
1952-
"'ううう'],\n"
1953-
" dtype='object')"
1954-
),
1955-
),
1956-
# truncated
1957-
(
1958-
Index(["あ", "いい", "ううう"] * 100),
1959-
(
1960-
"Index(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', "
1961-
"'あ', 'いい', 'ううう', 'あ',\n"
1962-
" ...\n"
1963-
" 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', "
1964-
"'ううう', 'あ', 'いい', 'ううう'],\n"
1965-
" dtype='object', length=300)"
1966-
),
1967-
),
1968-
],
1969-
)
1970-
def test_string_index_repr(self, index, expected):
1971-
result = repr(index)
1972-
assert result == expected
1973-
1974-
@pytest.mark.parametrize(
1975-
"index,expected",
1976-
[
1977-
# short
1978-
(
1979-
Index(["あ", "いい", "ううう"]),
1980-
("Index(['あ', 'いい', 'ううう'], dtype='object')"),
1981-
),
1982-
# multiple lines
1983-
(
1984-
Index(["あ", "いい", "ううう"] * 10),
1985-
(
1986-
"Index(['あ', 'いい', 'ううう', 'あ', 'いい', "
1987-
"'ううう', 'あ', 'いい', 'ううう',\n"
1988-
" 'あ', 'いい', 'ううう', 'あ', 'いい', "
1989-
"'ううう', 'あ', 'いい', 'ううう',\n"
1990-
" 'あ', 'いい', 'ううう', 'あ', 'いい', "
1991-
"'ううう', 'あ', 'いい', 'ううう',\n"
1992-
" 'あ', 'いい', 'ううう'],\n"
1993-
" dtype='object')"
1994-
""
1995-
),
1996-
),
1997-
# truncated
1998-
(
1999-
Index(["あ", "いい", "ううう"] * 100),
2000-
(
2001-
"Index(['あ', 'いい', 'ううう', 'あ', 'いい', "
2002-
"'ううう', 'あ', 'いい', 'ううう',\n"
2003-
" 'あ',\n"
2004-
" ...\n"
2005-
" 'ううう', 'あ', 'いい', 'ううう', 'あ', "
2006-
"'いい', 'ううう', 'あ', 'いい',\n"
2007-
" 'ううう'],\n"
2008-
" dtype='object', length=300)"
2009-
),
2010-
),
2011-
],
2012-
)
2013-
def test_string_index_repr_with_unicode_option(self, index, expected):
2014-
# Enable Unicode option -----------------------------------------
2015-
with cf.option_context("display.unicode.east_asian_width", True):
2016-
result = repr(index)
2017-
assert result == expected
2018-
20191908
def test_cached_properties_not_settable(self):
20201909
index = Index([1, 2, 3])
20211910
with pytest.raises(AttributeError, match="Can't set attribute"):
@@ -2236,12 +2125,6 @@ def test_is_monotonic_na(self, index):
22362125
assert index._is_strictly_monotonic_increasing is False
22372126
assert index._is_strictly_monotonic_decreasing is False
22382127

2239-
def test_repr_summary(self):
2240-
with cf.option_context("display.max_seq_items", 10):
2241-
result = repr(Index(np.arange(1000)))
2242-
assert len(result) < 200
2243-
assert "..." in result
2244-
22452128
@pytest.mark.parametrize("klass", [Series, DataFrame])
22462129
def test_int_name_format(self, klass):
22472130
index = Index(["a", "b", "c"], name=0)
@@ -2265,17 +2148,6 @@ def test_intersect_str_dates(self):
22652148
expected = Index([], dtype=object)
22662149
tm.assert_index_equal(result, expected)
22672150

2268-
def test_index_repr_bool_nan(self):
2269-
# GH32146
2270-
arr = Index([True, False, np.nan], dtype=object)
2271-
exp1 = arr.format()
2272-
out1 = ["True", "False", "NaN"]
2273-
assert out1 == exp1
2274-
2275-
exp2 = repr(arr)
2276-
out2 = "Index([True, False, nan], dtype='object')"
2277-
assert out2 == exp2
2278-
22792151
@pytest.mark.filterwarnings("ignore:elementwise comparison failed:FutureWarning")
22802152
def test_index_with_tuple_bool(self):
22812153
# GH34123

0 commit comments

Comments
 (0)