Skip to content

Commit b2ac307

Browse files
committed
formatting MultiIndex
1 parent 8ffd70d commit b2ac307

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

pandas/core/index.py

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -413,21 +413,33 @@ def __unicode__(self):
413413
klass = self.__class__.__name__
414414
data = self._format_data()
415415
attrs = self._format_attrs()
416-
max_seq_items = get_option('display.max_seq_items')
417-
if len(self) > max_seq_items:
418-
space = "\n%s" % (' ' * (len(klass) + 1))
419-
else:
420-
space = " "
416+
space = self._format_space()
421417

422418
prepr = (u(",%s") % space).join([u("%s=%s") % (k, v)
423419
for k, v in attrs])
424-
res = u("%s(%s,%s%s)") % (klass,
425-
data,
426-
space,
427-
prepr)
420+
421+
# no data provided, just attributes
422+
if data is None:
423+
data = ''
424+
else:
425+
data = "%s,%s" % (data, space)
426+
427+
res = u("%s(%s%s)") % (klass,
428+
data,
429+
prepr)
428430

429431
return res
430432

433+
def _format_space(self):
434+
435+
# using space here controls if the attributes
436+
# are line separated or not (the default)
437+
438+
#max_seq_items = get_option('display.max_seq_items')
439+
#if len(self) > max_seq_items:
440+
# space = "\n%s" % (' ' * (len(klass) + 1))
441+
return " "
442+
431443
@property
432444
def _formatter_func(self):
433445
"""
@@ -439,7 +451,6 @@ def _format_data(self):
439451
"""
440452
Return the formatted data as a unicode string
441453
"""
442-
443454
max_seq_items = get_option('display.max_seq_items')
444455
formatter = self._formatter_func
445456
n = len(self)
@@ -3280,40 +3291,24 @@ def nbytes(self):
32803291
names_nbytes = sum(( getsizeof(i) for i in self.names ))
32813292
return level_nbytes + label_nbytes + names_nbytes
32823293

3283-
def __repr__(self):
3284-
encoding = get_option('display.encoding')
3294+
def _format_attrs(self):
3295+
"""
3296+
Return a list of tuples of the (attr,formatted_value)
3297+
"""
32853298
attrs = [('levels', default_pprint(self.levels)),
32863299
('labels', default_pprint(self.labels))]
32873300
if not all(name is None for name in self.names):
32883301
attrs.append(('names', default_pprint(self.names)))
32893302
if self.sortorder is not None:
32903303
attrs.append(('sortorder', default_pprint(self.sortorder)))
3304+
return attrs
32913305

3292-
space = ' ' * (len(self.__class__.__name__) + 1)
3293-
prepr = (u(",\n%s") % space).join([u("%s=%s") % (k, v)
3294-
for k, v in attrs])
3295-
res = u("%s(%s)") % (self.__class__.__name__, prepr)
3306+
def _format_space(self):
3307+
return "\n%s" % (' ' * (len(self.__class__.__name__) + 1))
32963308

3297-
if not compat.PY3:
3298-
# needs to be str in Python 2
3299-
res = res.encode(encoding)
3300-
return res
3301-
3302-
def __unicode__(self):
3303-
"""
3304-
Return a string representation for a particular Index
3305-
3306-
Invoked by unicode(df) in py2 only. Yields a Unicode String in both
3307-
py2/py3.
3308-
"""
3309-
rows = self.format(names=True)
3310-
max_rows = get_option('display.max_rows')
3311-
if len(rows) > max_rows:
3312-
spaces = (len(rows[0]) - 3) // 2
3313-
centered = ' ' * spaces
3314-
half = max_rows // 2
3315-
rows = rows[:half] + [centered + '...' + centered] + rows[-half:]
3316-
return "\n".join(rows)
3309+
def _format_data(self):
3310+
# we are formatting thru the attributes
3311+
return None
33173312

33183313
def __len__(self):
33193314
return len(self.labels[0])

0 commit comments

Comments
 (0)