|
1 | 1 | # pylint: disable=E1101,E1103,W0232
|
2 | 2 | from functools import partial
|
3 |
| -from pandas.compat import range, zip, lrange, lzip |
| 3 | +from pandas.compat import range, zip, lrange, lzip, u |
4 | 4 | from pandas import compat
|
5 | 5 | import numpy as np
|
6 | 6 |
|
|
17 | 17 | from pandas.core.common import _values_from_object, is_float, is_integer
|
18 | 18 | from pandas.core.config import get_option
|
19 | 19 |
|
| 20 | +# simplify |
| 21 | +default_pprint = lambda x: com.pprint_thing(x, escape_chars=('\t', '\r', '\n'), |
| 22 | + quote_strings=True) |
| 23 | + |
20 | 24 |
|
21 | 25 | __all__ = ['Index']
|
22 | 26 |
|
@@ -2052,18 +2056,40 @@ def _array_values(self):
|
2052 | 2056 | def dtype(self):
|
2053 | 2057 | return np.dtype('O')
|
2054 | 2058 |
|
| 2059 | + def __repr__(self): |
| 2060 | + encoding = get_option('display.encoding') |
| 2061 | + attrs = [('levels', default_pprint(self.levels)), |
| 2062 | + ('labels', default_pprint(self.labels))] |
| 2063 | + if not all(name is None for name in self.names): |
| 2064 | + attrs.append(('names', default_pprint(self.names))) |
| 2065 | + if self.sortorder is not None: |
| 2066 | + attrs.append(('sortorder', default_pprint(self.sortorder))) |
| 2067 | + |
| 2068 | + space = ' ' * (len(self.__class__.__name__) + 1) |
| 2069 | + prepr = (u("\n%s") % space).join([u("%s=%s") % (k, v) |
| 2070 | + for k, v in attrs]) |
| 2071 | + res = u("%s(%s)") % (self.__class__.__name__, prepr) |
| 2072 | + |
| 2073 | + if not compat.PY3: |
| 2074 | + # needs to be str in Python 2 |
| 2075 | + res = res.encode(encoding) |
| 2076 | + return res |
| 2077 | + |
| 2078 | + |
2055 | 2079 | def __unicode__(self):
|
2056 | 2080 | """
|
2057 | 2081 | Return a string representation for a particular Index
|
2058 | 2082 |
|
2059 | 2083 | Invoked by unicode(df) in py2 only. Yields a Unicode String in both py2/py3.
|
2060 | 2084 | """
|
2061 |
| - output = 'MultiIndex\n%s' |
2062 |
| - |
2063 |
| - summary = com.pprint_thing(self, escape_chars=('\t', '\r', '\n'), |
2064 |
| - quote_strings=True) |
2065 |
| - |
2066 |
| - return output % summary |
| 2085 | + rows = self.format(names=True) |
| 2086 | + max_rows = get_option('display.max_rows') |
| 2087 | + if len(rows) > max_rows: |
| 2088 | + spaces = (len(rows[0]) - 3) // 2 |
| 2089 | + centered = ' ' * spaces |
| 2090 | + half = max_rows // 2 |
| 2091 | + rows = rows[:half] + [centered + '...' + centered] + rows[-half:] |
| 2092 | + return "\n".join(rows) |
2067 | 2093 |
|
2068 | 2094 | def __len__(self):
|
2069 | 2095 | return len(self.labels[0])
|
|
0 commit comments