@@ -132,12 +132,48 @@ def __array_finalize__(self, obj):
132
132
def _shallow_copy (self ):
133
133
return self .view ()
134
134
135
- def __repr__ (self ):
135
+ def __str__ (self ):
136
+ """
137
+ Return a string representation for a particular Index
138
+
139
+ Invoked by str(df) in both py2/py3.
140
+ Yields Bytestring in Py2, Unicode String in py3.
141
+ """
142
+
136
143
if py3compat .PY3 :
137
- prepr = com .pprint_thing (self )
144
+ return self .__unicode__ ()
145
+ return self .__bytes__ ()
146
+
147
+ def __bytes__ (self ):
148
+ """
149
+ Return a string representation for a particular Index
150
+
151
+ Invoked by bytes(df) in py3 only.
152
+ Yields a bytestring in both py2/py3.
153
+ """
154
+ return com .console_encode (self .__unicode__ ())
155
+
156
+ def __unicode__ (self ):
157
+ """
158
+ Return a string representation for a particular Index
159
+
160
+ Invoked by unicode(df) in py2 only. Yields a Unicode String in both py2/py3.
161
+ """
162
+ if len (self ) > 6 and len (self ) > np .get_printoptions ()['threshold' ]:
163
+ data = self [:3 ].tolist () + ["..." ] + self [- 3 :].tolist ()
138
164
else :
139
- prepr = com .pprint_thing_encoded (self )
140
- return 'Index(%s, dtype=%s)' % (prepr , self .dtype )
165
+ data = self
166
+
167
+ prepr = com .pprint_thing (data )
168
+ return '%s(%s, dtype=%s)' % (type (self ).__name__ , prepr , self .dtype )
169
+
170
+ def __repr__ (self ):
171
+ """
172
+ Return a string representation for a particular Index
173
+
174
+ Yields Bytestring in Py2, Unicode String in py3.
175
+ """
176
+ return str (self )
141
177
142
178
def astype (self , dtype ):
143
179
return Index (self .values .astype (dtype ), name = self .name ,
@@ -207,15 +243,6 @@ def summary(self, name=None):
207
243
name = type (self ).__name__
208
244
return '%s: %s entries%s' % (name , len (self ), index_summary )
209
245
210
- def __str__ (self ):
211
- try :
212
- return np .array_repr (self .values )
213
- except UnicodeError :
214
- converted = u',' .join (com .pprint_thing (x ) for x in self .values )
215
- result = u'%s([%s], dtype=' '%s' ')' % (type (self ).__name__ , converted ,
216
- str (self .values .dtype ))
217
- return com .console_encode (result )
218
-
219
246
def _mpl_repr (self ):
220
247
# how to represent ourselves to matplotlib
221
248
return self .values
@@ -1319,7 +1346,33 @@ def _array_values(self):
1319
1346
def dtype (self ):
1320
1347
return np .dtype ('O' )
1321
1348
1322
- def __repr__ (self ):
1349
+ def __str__ (self ):
1350
+ """
1351
+ Return a string representation for a particular Index
1352
+
1353
+ Invoked by str(df) in both py2/py3.
1354
+ Yields Bytestring in Py2, Unicode String in py3.
1355
+ """
1356
+
1357
+ if py3compat .PY3 :
1358
+ return self .__unicode__ ()
1359
+ return self .__bytes__ ()
1360
+
1361
+ def __bytes__ (self ):
1362
+ """
1363
+ Return a string representation for a particular Index
1364
+
1365
+ Invoked by bytes(df) in py3 only.
1366
+ Yields a bytestring in both py2/py3.
1367
+ """
1368
+ return com .console_encode (self .__unicode__ ())
1369
+
1370
+ def __unicode__ (self ):
1371
+ """
1372
+ Return a string representation for a particular Index
1373
+
1374
+ Invoked by unicode(df) in py2 only. Yields a Unicode String in both py2/py3.
1375
+ """
1323
1376
output = 'MultiIndex\n %s'
1324
1377
1325
1378
options = np .get_printoptions ()
@@ -1335,10 +1388,15 @@ def __repr__(self):
1335
1388
1336
1389
np .set_printoptions (threshold = options ['threshold' ])
1337
1390
1338
- if py3compat .PY3 :
1339
- return output % summary
1340
- else :
1341
- return com .console_encode (output % summary )
1391
+ return output % summary
1392
+
1393
+ def __repr__ (self ):
1394
+ """
1395
+ Return a string representation for a particular Index
1396
+
1397
+ Yields Bytestring in Py2, Unicode String in py3.
1398
+ """
1399
+ return str (self )
1342
1400
1343
1401
def __len__ (self ):
1344
1402
return len (self .labels [0 ])
0 commit comments