1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
| import numpy as np
| import pytest
|
| import pandas as pd
| from pandas import (
| Index,
| MultiIndex,
| )
|
|
| def test_format(idx):
| idx.format()
| idx[:0].format()
|
|
| def test_format_integer_names():
| index = MultiIndex(
| levels=[[0, 1], [0, 1]], codes=[[0, 0, 1, 1], [0, 1, 0, 1]], names=[0, 1]
| )
| index.format(names=True)
|
|
| def test_format_sparse_config(idx):
| # GH1538
| with pd.option_context("display.multi_sparse", False):
| result = idx.format()
| assert result[1] == "foo two"
|
|
| def test_format_sparse_display():
| index = MultiIndex(
| levels=[[0, 1], [0, 1], [0, 1], [0]],
| codes=[
| [0, 0, 0, 1, 1, 1],
| [0, 0, 1, 0, 0, 1],
| [0, 1, 0, 0, 1, 0],
| [0, 0, 0, 0, 0, 0],
| ],
| )
|
| result = index.format()
| assert result[3] == "1 0 0 0"
|
|
| def test_repr_with_unicode_data():
| with pd.option_context("display.encoding", "UTF-8"):
| d = {"a": ["\u05d0", 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}
| index = pd.DataFrame(d).set_index(["a", "b"]).index
| assert "\\" not in repr(index) # we don't want unicode-escaped
|
|
| def test_repr_roundtrip_raises():
| mi = MultiIndex.from_product([list("ab"), range(3)], names=["first", "second"])
| msg = "Must pass both levels and codes"
| with pytest.raises(TypeError, match=msg):
| eval(repr(mi))
|
|
| def test_unicode_string_with_unicode():
| d = {"a": ["\u05d0", 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}
| idx = pd.DataFrame(d).set_index(["a", "b"]).index
| str(idx)
|
|
| def test_repr_max_seq_item_setting(idx):
| # GH10182
| idx = idx.repeat(50)
| with pd.option_context("display.max_seq_items", None):
| repr(idx)
| assert "..." not in str(idx)
|
|
| class TestRepr:
| def test_unicode_repr_issues(self):
| levels = [Index(["a/\u03c3", "b/\u03c3", "c/\u03c3"]), Index([0, 1])]
| codes = [np.arange(3).repeat(2), np.tile(np.arange(2), 3)]
| index = MultiIndex(levels=levels, codes=codes)
|
| repr(index.levels)
| 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 = """\
| MultiIndex([('foo', 'one')],
| names=['first', 'second'])"""
| assert result == expected
|
| result = idx.__repr__()
| expected = """\
| MultiIndex([('foo', 'one'),
| ('foo', 'two'),
| ('bar', 'one'),
| ('baz', 'two'),
| ('qux', 'one'),
| ('qux', 'two')],
| names=['first', 'second'])"""
| assert result == expected
|
| with pd.option_context("display.max_seq_items", 5):
| result = idx.__repr__()
| expected = """\
| MultiIndex([('foo', 'one'),
| ('foo', 'two'),
| ...
| ('qux', 'one'),
| ('qux', 'two')],
| 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__()
| expected = """\
| MultiIndex([('a', 9, '2000-01-01 00:00:00')],
| names=['a', 'b', 'dti'])"""
| assert result == expected
|
| result = mi[::500].__repr__()
| expected = """\
| MultiIndex([( 'a', 9, '2000-01-01 00:00:00'),
| ( 'a', 9, '2000-01-01 00:08:20'),
| ('abc', 10, '2000-01-01 00:16:40'),
| ('abc', 10, '2000-01-01 00:25:00')],
| names=['a', 'b', 'dti'])"""
| assert result == expected
|
| result = mi.__repr__()
| expected = """\
| MultiIndex([( 'a', 9, '2000-01-01 00:00:00'),
| ( 'a', 9, '2000-01-01 00:00:01'),
| ( 'a', 9, '2000-01-01 00:00:02'),
| ( 'a', 9, '2000-01-01 00:00:03'),
| ( 'a', 9, '2000-01-01 00:00:04'),
| ( 'a', 9, '2000-01-01 00:00:05'),
| ( 'a', 9, '2000-01-01 00:00:06'),
| ( 'a', 9, '2000-01-01 00:00:07'),
| ( 'a', 9, '2000-01-01 00:00:08'),
| ( 'a', 9, '2000-01-01 00:00:09'),
| ...
| ('abc', 10, '2000-01-01 00:33:10'),
| ('abc', 10, '2000-01-01 00:33:11'),
| ('abc', 10, '2000-01-01 00:33:12'),
| ('abc', 10, '2000-01-01 00:33:13'),
| ('abc', 10, '2000-01-01 00:33:14'),
| ('abc', 10, '2000-01-01 00:33:15'),
| ('abc', 10, '2000-01-01 00:33:16'),
| ('abc', 10, '2000-01-01 00:33:17'),
| ('abc', 10, '2000-01-01 00:33:18'),
| ('abc', 10, '2000-01-01 00:33:19')],
| names=['a', 'b', 'dti'], length=2000)"""
| assert result == expected
|
| def test_tuple_width(self, wide_multi_index):
| mi = wide_multi_index
| result = mi[:1].__repr__()
| expected = """MultiIndex([('a', 9, '2000-01-01 00:00:00', '2000-01-01 00:00:00', ...)],
| names=['a', 'b', 'dti_1', 'dti_2', 'dti_3'])""" # noqa:E501
| assert result == expected
|
| result = mi[:10].__repr__()
| expected = """\
| MultiIndex([('a', 9, '2000-01-01 00:00:00', '2000-01-01 00:00:00', ...),
| ('a', 9, '2000-01-01 00:00:01', '2000-01-01 00:00:01', ...),
| ('a', 9, '2000-01-01 00:00:02', '2000-01-01 00:00:02', ...),
| ('a', 9, '2000-01-01 00:00:03', '2000-01-01 00:00:03', ...),
| ('a', 9, '2000-01-01 00:00:04', '2000-01-01 00:00:04', ...),
| ('a', 9, '2000-01-01 00:00:05', '2000-01-01 00:00:05', ...),
| ('a', 9, '2000-01-01 00:00:06', '2000-01-01 00:00:06', ...),
| ('a', 9, '2000-01-01 00:00:07', '2000-01-01 00:00:07', ...),
| ('a', 9, '2000-01-01 00:00:08', '2000-01-01 00:00:08', ...),
| ('a', 9, '2000-01-01 00:00:09', '2000-01-01 00:00:09', ...)],
| names=['a', 'b', 'dti_1', 'dti_2', 'dti_3'])"""
| assert result == expected
|
| result = mi.__repr__()
| expected = """\
| MultiIndex([( 'a', 9, '2000-01-01 00:00:00', '2000-01-01 00:00:00', ...),
| ( 'a', 9, '2000-01-01 00:00:01', '2000-01-01 00:00:01', ...),
| ( 'a', 9, '2000-01-01 00:00:02', '2000-01-01 00:00:02', ...),
| ( 'a', 9, '2000-01-01 00:00:03', '2000-01-01 00:00:03', ...),
| ( 'a', 9, '2000-01-01 00:00:04', '2000-01-01 00:00:04', ...),
| ( 'a', 9, '2000-01-01 00:00:05', '2000-01-01 00:00:05', ...),
| ( 'a', 9, '2000-01-01 00:00:06', '2000-01-01 00:00:06', ...),
| ( 'a', 9, '2000-01-01 00:00:07', '2000-01-01 00:00:07', ...),
| ( 'a', 9, '2000-01-01 00:00:08', '2000-01-01 00:00:08', ...),
| ( 'a', 9, '2000-01-01 00:00:09', '2000-01-01 00:00:09', ...),
| ...
| ('abc', 10, '2000-01-01 00:33:10', '2000-01-01 00:33:10', ...),
| ('abc', 10, '2000-01-01 00:33:11', '2000-01-01 00:33:11', ...),
| ('abc', 10, '2000-01-01 00:33:12', '2000-01-01 00:33:12', ...),
| ('abc', 10, '2000-01-01 00:33:13', '2000-01-01 00:33:13', ...),
| ('abc', 10, '2000-01-01 00:33:14', '2000-01-01 00:33:14', ...),
| ('abc', 10, '2000-01-01 00:33:15', '2000-01-01 00:33:15', ...),
| ('abc', 10, '2000-01-01 00:33:16', '2000-01-01 00:33:16', ...),
| ('abc', 10, '2000-01-01 00:33:17', '2000-01-01 00:33:17', ...),
| ('abc', 10, '2000-01-01 00:33:18', '2000-01-01 00:33:18', ...),
| ('abc', 10, '2000-01-01 00:33:19', '2000-01-01 00:33:19', ...)],
| names=['a', 'b', 'dti_1', 'dti_2', 'dti_3'], length=2000)"""
| assert result == expected
|
|