Skip to content

Commit 436bf36

Browse files
author
y-p
committed
ENH: py2/py3 support for str(x)/unicode(x) and bytes(x)/str(x) for Index,MultiIndex
1 parent f0deaa6 commit 436bf36

File tree

1 file changed

+76
-18
lines changed

1 file changed

+76
-18
lines changed

pandas/core/index.py

+76-18
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,48 @@ def __array_finalize__(self, obj):
132132
def _shallow_copy(self):
133133
return self.view()
134134

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+
136143
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()
138164
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)
141177

142178
def astype(self, dtype):
143179
return Index(self.values.astype(dtype), name=self.name,
@@ -207,15 +243,6 @@ def summary(self, name=None):
207243
name = type(self).__name__
208244
return '%s: %s entries%s' % (name, len(self), index_summary)
209245

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-
219246
def _mpl_repr(self):
220247
# how to represent ourselves to matplotlib
221248
return self.values
@@ -1319,7 +1346,33 @@ def _array_values(self):
13191346
def dtype(self):
13201347
return np.dtype('O')
13211348

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+
"""
13231376
output = 'MultiIndex\n%s'
13241377

13251378
options = np.get_printoptions()
@@ -1335,10 +1388,15 @@ def __repr__(self):
13351388

13361389
np.set_printoptions(threshold=options['threshold'])
13371390

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)
13421400

13431401
def __len__(self):
13441402
return len(self.labels[0])

0 commit comments

Comments
 (0)