Skip to content

Commit a80ae5e

Browse files
committed
revsised according to comments
1 parent 5570139 commit a80ae5e

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

doc/source/whatsnew/v0.16.1.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,7 @@ API changes
253253
Index Representation
254254
~~~~~~~~~~~~~~~~~~~~
255255

256-
The string representation of ``Index`` and its sub-classes have now been unified. ``Index, Int64Index, Float64Index, CategoricalIndex`` are single-line display. The datetimelikes ``DatetimeIndex, PeriodIndex, TimedeltaIndex`` & ``MultiIndex`` will display in a multi-line format showing much more of the index values. The display width responds to the option ``display.max_seq_items``,
257-
which is defaulted to 100. (:issue:`6482`)
256+
The string representation of ``Index`` and its sub-classes have now been unified. These will show a single-line display if there are few values; a wrapped multi-line display for a lot of values (but less than ``display.max_seq_items``; if lots of items (> ``display.max_seq_items``) will show a truncated display (the head and tail of the data). The formatting for ``MultiIndex`` is unchanges (a multi-line wrapped display). The display width responds to the option ``display.max_seq_items``, which is defaulted to 100. (:issue:`6482`)
258257

259258
Previous Behavior
260259

@@ -289,6 +288,11 @@ New Behavior
289288
pd.Index(range(4),name='foo')
290289
pd.Index(range(25),name='foo')
291290
pd.Index(range(104),name='foo')
291+
pd.CategoricalIndex(['a','bb','ccc','dddd'],ordered=True,name='foobar')
292+
pd.CategoricalIndex(['a','bb','ccc','dddd']*10,ordered=True,name='foobar')
293+
pd.CategoricalIndex(['a','bb','ccc','dddd']*100,ordered=True,name='foobar')
294+
pd.CategoricalIndex(np.arange(1000),ordered=True,name='foobar')
295+
pd.Index(['a','bb','ccc','dddd']*100)
292296
pd.date_range('20130101',periods=4,name='foo',tz='US/Eastern')
293297
pd.date_range('20130101',periods=25,name='foo',tz='US/Eastern')
294298
pd.date_range('20130101',periods=104,name='foo',tz='US/Eastern')

pandas/core/index.py

+24-10
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ def _format_data(self):
440440
sep = ','
441441
max_seq_items = get_option('display.max_seq_items')
442442
formatter = self._formatter_func
443+
needs_justify = self.inferred_type in ['string','categorical']
443444

444445
def best_len(values):
445446
return max([len(x) for x in values]) + 2
@@ -453,23 +454,27 @@ def best_rows(values, max_len):
453454
n_rows = int(ceil(len(values) / float(n_per_row)))
454455
return n_per_row, n_rows
455456

456-
def best_fit(values, max_len, justify=False):
457+
def best_fit(values, max_len, n_rows=None, justify=False):
457458

458459
# number of rows to generate
459-
n_per_row, n_rows = best_rows(values, max_len)
460+
if n_rows is None:
461+
n_per_row, n_rows = best_rows(values, max_len)
462+
else:
463+
n_per_row = len(values)
460464

461465
# adjust all values to max length if we have multi-lines
462-
if n_rows > 1 or justify:
466+
if justify:
463467
values = [values[0].rjust(max_len-2)] + [x.rjust(max_len-1) for x in values[1:]]
464-
sep_elements = sep
468+
multi_line_space = space1
465469
else:
466-
sep_elements = sep + ' '
470+
multi_line_space = space2
467471

472+
sep_elements = sep + ' '
468473
summary = ''
469474
for i in range(n_rows - 1):
470-
summary += sep.join(values[i*n_per_row:(i+1)*n_per_row])
475+
summary += sep_elements.join(values[i*n_per_row:(i+1)*n_per_row])
471476
summary += sep
472-
summary += space1
477+
summary += multi_line_space
473478
summary += sep_elements.join(values[(n_rows - 1)*n_per_row:n_rows*n_per_row])
474479

475480
return summary
@@ -491,10 +496,17 @@ def best_fit(values, max_len, justify=False):
491496
tail = [ formatter(x) for x in self[-n:] ]
492497
max_len = max(best_len(head),best_len(tail))
493498

499+
if needs_justify:
500+
n_rows = 1
501+
justify = False
502+
else:
503+
n_rows = None
504+
justify = True
505+
494506
summary = '['
495-
summary += best_fit(head, max_len, justify=True)
507+
summary += best_fit(head, max_len, n_rows=n_rows, justify=justify)
496508
summary += ',' + space1 + ' ...' + space2
497-
summary += best_fit(tail, max_len, justify=True)
509+
summary += best_fit(tail, max_len, n_rows=n_rows, justify=justify)
498510
summary += '],'
499511
summary += space1
500512

@@ -2936,7 +2948,9 @@ def _format_attrs(self):
29362948
"""
29372949
Return a list of tuples of the (attr,formatted_value)
29382950
"""
2939-
attrs = [('categories', default_pprint(self.categories)),
2951+
max_categories = (10 if get_option("display.max_categories") == 0
2952+
else get_option("display.max_categories"))
2953+
attrs = [('categories', default_pprint(self.categories, max_seq_items=max_categories)),
29402954
('ordered',self.ordered)]
29412955
if self.name is not None:
29422956
attrs.append(('name',default_pprint(self.name)))

pandas/tests/test_index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2463,7 +2463,7 @@ def test_print_unicode_columns(self):
24632463
def test_repr_summary(self):
24642464
with cf.option_context('display.max_seq_items', 10):
24652465
r = repr(pd.Index(np.arange(1000)))
2466-
self.assertTrue(len(r) < 150)
2466+
self.assertTrue(len(r) < 200)
24672467
self.assertTrue("..." in r)
24682468

24692469
def test_repr_roundtrip(self):

0 commit comments

Comments
 (0)