|
57 | 57 | import pandas.core.missing as missing
|
58 | 58 | import pandas.core.algorithms as algos
|
59 | 59 | import pandas.core.sorting as sorting
|
60 |
| -from pandas.io.formats.printing import pprint_thing |
| 60 | +from pandas.io.formats.printing import ( |
| 61 | + pprint_thing, default_pprint, format_object_summary, format_object_attrs) |
61 | 62 | from pandas.core.ops import make_invalid_op
|
62 |
| -from pandas.core.config import get_option |
63 | 63 | from pandas.core.strings import StringMethods
|
64 | 64 |
|
65 |
| - |
66 |
| -# simplify |
67 |
| -default_pprint = lambda x, max_seq_items=None: \ |
68 |
| - pprint_thing(x, escape_chars=('\t', '\r', '\n'), quote_strings=True, |
69 |
| - max_seq_items=max_seq_items) |
70 |
| - |
71 | 65 | __all__ = ['Index']
|
72 | 66 |
|
73 | 67 | _unsortable_types = frozenset(('mixed', 'mixed-integer'))
|
@@ -1034,133 +1028,28 @@ def _format_space(self):
|
1034 | 1028 | @property
|
1035 | 1029 | def _formatter_func(self):
|
1036 | 1030 | """
|
1037 |
| - Return the formatted data as a unicode string |
| 1031 | + Return the formatter function |
1038 | 1032 | """
|
1039 | 1033 | return default_pprint
|
1040 | 1034 |
|
1041 | 1035 | def _format_data(self, name=None):
|
1042 | 1036 | """
|
1043 | 1037 | Return the formatted data as a unicode string
|
1044 | 1038 | """
|
1045 |
| - from pandas.io.formats.console import get_console_size |
1046 |
| - from pandas.io.formats.format import _get_adjustment |
1047 |
| - display_width, _ = get_console_size() |
1048 |
| - if display_width is None: |
1049 |
| - display_width = get_option('display.width') or 80 |
1050 |
| - if name is None: |
1051 |
| - name = self.__class__.__name__ |
1052 |
| - |
1053 |
| - space1 = "\n%s" % (' ' * (len(name) + 1)) |
1054 |
| - space2 = "\n%s" % (' ' * (len(name) + 2)) |
1055 |
| - |
1056 |
| - n = len(self) |
1057 |
| - sep = ',' |
1058 |
| - max_seq_items = get_option('display.max_seq_items') or n |
1059 |
| - formatter = self._formatter_func |
1060 | 1039 |
|
1061 | 1040 | # do we want to justify (only do so for non-objects)
|
1062 | 1041 | is_justify = not (self.inferred_type in ('string', 'unicode') or
|
1063 | 1042 | (self.inferred_type == 'categorical' and
|
1064 | 1043 | is_object_dtype(self.categories)))
|
1065 | 1044 |
|
1066 |
| - # are we a truncated display |
1067 |
| - is_truncated = n > max_seq_items |
1068 |
| - |
1069 |
| - # adj can optionally handle unicode eastern asian width |
1070 |
| - adj = _get_adjustment() |
1071 |
| - |
1072 |
| - def _extend_line(s, line, value, display_width, next_line_prefix): |
1073 |
| - |
1074 |
| - if (adj.len(line.rstrip()) + adj.len(value.rstrip()) >= |
1075 |
| - display_width): |
1076 |
| - s += line.rstrip() |
1077 |
| - line = next_line_prefix |
1078 |
| - line += value |
1079 |
| - return s, line |
1080 |
| - |
1081 |
| - def best_len(values): |
1082 |
| - if values: |
1083 |
| - return max(adj.len(x) for x in values) |
1084 |
| - else: |
1085 |
| - return 0 |
1086 |
| - |
1087 |
| - if n == 0: |
1088 |
| - summary = '[], ' |
1089 |
| - elif n == 1: |
1090 |
| - first = formatter(self[0]) |
1091 |
| - summary = '[%s], ' % first |
1092 |
| - elif n == 2: |
1093 |
| - first = formatter(self[0]) |
1094 |
| - last = formatter(self[-1]) |
1095 |
| - summary = '[%s, %s], ' % (first, last) |
1096 |
| - else: |
1097 |
| - |
1098 |
| - if n > max_seq_items: |
1099 |
| - n = min(max_seq_items // 2, 10) |
1100 |
| - head = [formatter(x) for x in self[:n]] |
1101 |
| - tail = [formatter(x) for x in self[-n:]] |
1102 |
| - else: |
1103 |
| - head = [] |
1104 |
| - tail = [formatter(x) for x in self] |
1105 |
| - |
1106 |
| - # adjust all values to max length if needed |
1107 |
| - if is_justify: |
1108 |
| - |
1109 |
| - # however, if we are not truncated and we are only a single |
1110 |
| - # line, then don't justify |
1111 |
| - if (is_truncated or |
1112 |
| - not (len(', '.join(head)) < display_width and |
1113 |
| - len(', '.join(tail)) < display_width)): |
1114 |
| - max_len = max(best_len(head), best_len(tail)) |
1115 |
| - head = [x.rjust(max_len) for x in head] |
1116 |
| - tail = [x.rjust(max_len) for x in tail] |
1117 |
| - |
1118 |
| - summary = "" |
1119 |
| - line = space2 |
1120 |
| - |
1121 |
| - for i in range(len(head)): |
1122 |
| - word = head[i] + sep + ' ' |
1123 |
| - summary, line = _extend_line(summary, line, word, |
1124 |
| - display_width, space2) |
1125 |
| - |
1126 |
| - if is_truncated: |
1127 |
| - # remove trailing space of last line |
1128 |
| - summary += line.rstrip() + space2 + '...' |
1129 |
| - line = space2 |
1130 |
| - |
1131 |
| - for i in range(len(tail) - 1): |
1132 |
| - word = tail[i] + sep + ' ' |
1133 |
| - summary, line = _extend_line(summary, line, word, |
1134 |
| - display_width, space2) |
1135 |
| - |
1136 |
| - # last value: no sep added + 1 space of width used for trailing ',' |
1137 |
| - summary, line = _extend_line(summary, line, tail[-1], |
1138 |
| - display_width - 2, space2) |
1139 |
| - summary += line |
1140 |
| - summary += '],' |
1141 |
| - |
1142 |
| - if len(summary) > (display_width): |
1143 |
| - summary += space1 |
1144 |
| - else: # one row |
1145 |
| - summary += ' ' |
1146 |
| - |
1147 |
| - # remove initial space |
1148 |
| - summary = '[' + summary[len(space2):] |
1149 |
| - |
1150 |
| - return summary |
| 1045 | + return format_object_summary(self, self._formatter_func, |
| 1046 | + is_justify=is_justify, name=name) |
1151 | 1047 |
|
1152 | 1048 | def _format_attrs(self):
|
1153 | 1049 | """
|
1154 | 1050 | Return a list of tuples of the (attr,formatted_value)
|
1155 | 1051 | """
|
1156 |
| - attrs = [] |
1157 |
| - attrs.append(('dtype', "'%s'" % self.dtype)) |
1158 |
| - if self.name is not None: |
1159 |
| - attrs.append(('name', default_pprint(self.name))) |
1160 |
| - max_seq_items = get_option('display.max_seq_items') or len(self) |
1161 |
| - if len(self) > max_seq_items: |
1162 |
| - attrs.append(('length', len(self))) |
1163 |
| - return attrs |
| 1052 | + return format_object_attrs(self) |
1164 | 1053 |
|
1165 | 1054 | def to_series(self, index=None, name=None):
|
1166 | 1055 | """
|
|
0 commit comments