3
3
4
4
import warnings
5
5
6
+ import pytest
7
+
6
8
import pandas as pd
7
9
import pandas .util .testing as tm
8
10
from pandas import MultiIndex , compat
9
- from pandas .compat import PY3 , range , u
11
+ from pandas .compat import PY3 , PY2 , u
10
12
11
13
12
14
def test_dtype_str (indices ):
@@ -57,49 +59,6 @@ def test_repr_with_unicode_data():
57
59
assert "\\ u" not in repr (index ) # we don't want unicode-escaped
58
60
59
61
60
- def test_repr_roundtrip ():
61
-
62
- mi = MultiIndex .from_product ([list ('ab' ), range (3 )],
63
- names = ['first' , 'second' ])
64
- str (mi )
65
-
66
- if PY3 :
67
- tm .assert_index_equal (eval (repr (mi )), mi , exact = True )
68
- else :
69
- result = eval (repr (mi ))
70
- # string coerces to unicode
71
- tm .assert_index_equal (result , mi , exact = False )
72
- assert mi .get_level_values ('first' ).inferred_type == 'string'
73
- assert result .get_level_values ('first' ).inferred_type == 'unicode'
74
-
75
- mi_u = MultiIndex .from_product (
76
- [list (u'ab' ), range (3 )], names = ['first' , 'second' ])
77
- result = eval (repr (mi_u ))
78
- tm .assert_index_equal (result , mi_u , exact = True )
79
-
80
- # formatting
81
- if PY3 :
82
- str (mi )
83
- else :
84
- compat .text_type (mi )
85
-
86
- # long format
87
- mi = MultiIndex .from_product ([list ('abcdefg' ), range (10 )],
88
- names = ['first' , 'second' ])
89
-
90
- if PY3 :
91
- tm .assert_index_equal (eval (repr (mi )), mi , exact = True )
92
- else :
93
- result = eval (repr (mi ))
94
- # string coerces to unicode
95
- tm .assert_index_equal (result , mi , exact = False )
96
- assert mi .get_level_values ('first' ).inferred_type == 'string'
97
- assert result .get_level_values ('first' ).inferred_type == 'unicode'
98
-
99
- result = eval (repr (mi_u ))
100
- tm .assert_index_equal (result , mi_u , exact = True )
101
-
102
-
103
62
def test_unicode_string_with_unicode ():
104
63
d = {"a" : [u ("\u05d0 " ), 2 , 3 ], "b" : [4 , 5 , 6 ], "c" : [7 , 8 , 9 ]}
105
64
idx = pd .DataFrame (d ).set_index (["a" , "b" ]).index
@@ -126,3 +85,133 @@ def test_repr_max_seq_item_setting(idx):
126
85
with pd .option_context ("display.max_seq_items" , None ):
127
86
repr (idx )
128
87
assert '...' not in str (idx )
88
+
89
+
90
+ @pytest .mark .skipif (PY2 , reason = "repr output is different for python2" )
91
+ class TestRepr (object ):
92
+
93
+ def setup_class (self ):
94
+ n = 1000
95
+ ci = pd .CategoricalIndex (list ('a' * n ) + (['abc' ] * n ))
96
+ dti = pd .date_range ('2000-01-01' , freq = 's' , periods = n * 2 )
97
+ self .narrow_mi = pd .MultiIndex .from_arrays ([ci , ci .codes + 9 , dti ],
98
+ names = ['a' , 'b' , 'dti' ])
99
+
100
+ levels = [ci , ci .codes + 9 , dti , dti , dti ]
101
+ names = ['a' , 'b' , 'dti_1' , 'dti_2' , 'dti_3' ]
102
+ self .wide_mi = pd .MultiIndex .from_arrays (levels , names = names )
103
+
104
+ def test_repr (self , idx ):
105
+ result = idx [:1 ].__repr__ ()
106
+ expected = """MultiIndex([('foo', 'one')],
107
+ dtype='object', names=['first', 'second'])"""
108
+ assert result == expected
109
+
110
+ result = idx .__repr__ ()
111
+ expected = """MultiIndex([('foo', 'one'),
112
+ ('foo', 'two'),
113
+ ('bar', 'one'),
114
+ ('baz', 'two'),
115
+ ('qux', 'one'),
116
+ ('qux', 'two')],
117
+ dtype='object', names=['first', 'second'])"""
118
+ assert result == expected
119
+
120
+ with pd .option_context ('display.max_seq_items' , 5 ):
121
+ result = idx .__repr__ ()
122
+ expected = """MultiIndex([('foo', 'one'),
123
+ ('foo', 'two'),
124
+ ...
125
+ ('qux', 'one'),
126
+ ('qux', 'two')],
127
+ dtype='object', names=['first', 'second'], length=6)"""
128
+ assert result == expected
129
+
130
+ def test_rjust (self ):
131
+ result = self .narrow_mi [:1 ].__repr__ ()
132
+ expected = """\
133
+ MultiIndex([('a', 9, '2000-01-01 00:00:00')],
134
+ dtype='object', names=['a', 'b', 'dti'])"""
135
+ assert result == expected
136
+
137
+ result = self .narrow_mi [::500 ].__repr__ ()
138
+ expected = """\
139
+ MultiIndex([( 'a', 9, '2000-01-01 00:00:00'),
140
+ ( 'a', 9, '2000-01-01 00:08:20'),
141
+ ('abc', 10, '2000-01-01 00:16:40'),
142
+ ('abc', 10, '2000-01-01 00:25:00')],
143
+ dtype='object', names=['a', 'b', 'dti'])"""
144
+ assert result == expected
145
+
146
+ result = self .narrow_mi .__repr__ ()
147
+ expected = """\
148
+ MultiIndex([( 'a', 9, '2000-01-01 00:00:00'),
149
+ ( 'a', 9, '2000-01-01 00:00:01'),
150
+ ( 'a', 9, '2000-01-01 00:00:02'),
151
+ ( 'a', 9, '2000-01-01 00:00:03'),
152
+ ( 'a', 9, '2000-01-01 00:00:04'),
153
+ ( 'a', 9, '2000-01-01 00:00:05'),
154
+ ( 'a', 9, '2000-01-01 00:00:06'),
155
+ ( 'a', 9, '2000-01-01 00:00:07'),
156
+ ( 'a', 9, '2000-01-01 00:00:08'),
157
+ ( 'a', 9, '2000-01-01 00:00:09'),
158
+ ...
159
+ ('abc', 10, '2000-01-01 00:33:10'),
160
+ ('abc', 10, '2000-01-01 00:33:11'),
161
+ ('abc', 10, '2000-01-01 00:33:12'),
162
+ ('abc', 10, '2000-01-01 00:33:13'),
163
+ ('abc', 10, '2000-01-01 00:33:14'),
164
+ ('abc', 10, '2000-01-01 00:33:15'),
165
+ ('abc', 10, '2000-01-01 00:33:16'),
166
+ ('abc', 10, '2000-01-01 00:33:17'),
167
+ ('abc', 10, '2000-01-01 00:33:18'),
168
+ ('abc', 10, '2000-01-01 00:33:19')],
169
+ dtype='object', names=['a', 'b', 'dti'], length=2000)"""
170
+ assert result == expected
171
+
172
+ def test_tuple_width (self ):
173
+ result = self .wide_mi [:1 ].__repr__ ()
174
+ expected = """MultiIndex([('a', 9, '2000-01-01 00:00:00', '2000-01-01 00:00:00', ...)],
175
+ dtype='object', names=['a', 'b', 'dti_1', 'dti_2', 'dti_3'])"""
176
+ assert result == expected
177
+
178
+ result = self .wide_mi [:10 ].__repr__ ()
179
+ expected = """\
180
+ MultiIndex([('a', 9, '2000-01-01 00:00:00', '2000-01-01 00:00:00', ...),
181
+ ('a', 9, '2000-01-01 00:00:01', '2000-01-01 00:00:01', ...),
182
+ ('a', 9, '2000-01-01 00:00:02', '2000-01-01 00:00:02', ...),
183
+ ('a', 9, '2000-01-01 00:00:03', '2000-01-01 00:00:03', ...),
184
+ ('a', 9, '2000-01-01 00:00:04', '2000-01-01 00:00:04', ...),
185
+ ('a', 9, '2000-01-01 00:00:05', '2000-01-01 00:00:05', ...),
186
+ ('a', 9, '2000-01-01 00:00:06', '2000-01-01 00:00:06', ...),
187
+ ('a', 9, '2000-01-01 00:00:07', '2000-01-01 00:00:07', ...),
188
+ ('a', 9, '2000-01-01 00:00:08', '2000-01-01 00:00:08', ...),
189
+ ('a', 9, '2000-01-01 00:00:09', '2000-01-01 00:00:09', ...)],
190
+ dtype='object', names=['a', 'b', 'dti_1', 'dti_2', 'dti_3'])"""
191
+ assert result == expected
192
+
193
+ result = self .wide_mi .__repr__ ()
194
+ expected = """\
195
+ MultiIndex([( 'a', 9, '2000-01-01 00:00:00', '2000-01-01 00:00:00', ...),
196
+ ( 'a', 9, '2000-01-01 00:00:01', '2000-01-01 00:00:01', ...),
197
+ ( 'a', 9, '2000-01-01 00:00:02', '2000-01-01 00:00:02', ...),
198
+ ( 'a', 9, '2000-01-01 00:00:03', '2000-01-01 00:00:03', ...),
199
+ ( 'a', 9, '2000-01-01 00:00:04', '2000-01-01 00:00:04', ...),
200
+ ( 'a', 9, '2000-01-01 00:00:05', '2000-01-01 00:00:05', ...),
201
+ ( 'a', 9, '2000-01-01 00:00:06', '2000-01-01 00:00:06', ...),
202
+ ( 'a', 9, '2000-01-01 00:00:07', '2000-01-01 00:00:07', ...),
203
+ ( 'a', 9, '2000-01-01 00:00:08', '2000-01-01 00:00:08', ...),
204
+ ( 'a', 9, '2000-01-01 00:00:09', '2000-01-01 00:00:09', ...),
205
+ ...
206
+ ('abc', 10, '2000-01-01 00:33:10', '2000-01-01 00:33:10', ...),
207
+ ('abc', 10, '2000-01-01 00:33:11', '2000-01-01 00:33:11', ...),
208
+ ('abc', 10, '2000-01-01 00:33:12', '2000-01-01 00:33:12', ...),
209
+ ('abc', 10, '2000-01-01 00:33:13', '2000-01-01 00:33:13', ...),
210
+ ('abc', 10, '2000-01-01 00:33:14', '2000-01-01 00:33:14', ...),
211
+ ('abc', 10, '2000-01-01 00:33:15', '2000-01-01 00:33:15', ...),
212
+ ('abc', 10, '2000-01-01 00:33:16', '2000-01-01 00:33:16', ...),
213
+ ('abc', 10, '2000-01-01 00:33:17', '2000-01-01 00:33:17', ...),
214
+ ('abc', 10, '2000-01-01 00:33:18', '2000-01-01 00:33:18', ...),
215
+ ('abc', 10, '2000-01-01 00:33:19', '2000-01-01 00:33:19', ...)],
216
+ dtype='object', names=['a', 'b', 'dti_1', 'dti_2', 'dti_3'], length=2000)""" # noqa
217
+ assert result == expected
0 commit comments