Skip to content

Commit f0e07f5

Browse files
committed
adjust index display to max_seq_items
1 parent 8744104 commit f0e07f5

File tree

3 files changed

+42
-32
lines changed

3 files changed

+42
-32
lines changed

pandas/core/index.py

+41-8
Original file line numberDiff line numberDiff line change
@@ -393,24 +393,57 @@ def __unicode__(self):
393393
py2/py3.
394394
"""
395395
klass = self.__class__.__name__
396-
space = ' ' * (len(klass) + 1)
397396
data = self._format_data()
398397
attrs = self._format_attrs()
399-
prepr = (u(",\n%s") % space).join([u("%s=%s") % (k, v)
398+
max_seq_items = get_option('display.max_seq_items')
399+
if len(self) > max_seq_items:
400+
space = "\n%s" % (' ' * (len(klass) + 1))
401+
else:
402+
space = " "
403+
404+
prepr = (u(",%s") % space).join([u("%s=%s") % (k, v)
400405
for k, v in attrs])
401-
res = u("%s(%s,\n%s%s)") % (klass,
402-
data,
403-
space,
404-
prepr)
406+
res = u("%s(%s,%s%s)") % (klass,
407+
data,
408+
space,
409+
prepr)
405410

406411
return res
407412

413+
@property
414+
def _formatter_func(self):
415+
"""
416+
Return the formatted data as a unicode string
417+
"""
418+
return default_pprint
419+
408420
def _format_data(self):
409421
"""
410422
Return the formatted data as a unicode string
411423
"""
412-
return com.pprint_thing(self, escape_chars=('\t', '\r', '\n'),
413-
quote_strings=True)
424+
425+
max_seq_items = get_option('display.max_seq_items')
426+
formatter = self._formatter_func
427+
n = len(self)
428+
if n == 0:
429+
summary = '[]'
430+
elif n == 1:
431+
first = formatter(self[0])
432+
summary = '[%s]' % first
433+
elif n == 2:
434+
first = formatter(self[0])
435+
last = formatter(self[-1])
436+
summary = '[%s, %s]' % (first, last)
437+
elif n > max_seq_items:
438+
n = min(max_seq_items//2,2)
439+
head = ', '.join([ formatter(x) for x in self[:n] ])
440+
tail = ', '.join([ formatter(x) for x in self[-n:] ])
441+
summary = '[%s, ..., %s]' % (head, tail)
442+
else:
443+
summary = "[%s]" % ', '.join([ formatter(x) for x in self ])
444+
445+
return summary
446+
414447

415448
def _format_attrs(self):
416449
"""

pandas/tseries/base.py

-23
Original file line numberDiff line numberDiff line change
@@ -260,29 +260,6 @@ def _formatter_func(self):
260260
"""
261261
return str
262262

263-
def _format_data(self):
264-
"""
265-
Return the formatted data as a unicode string
266-
"""
267-
268-
formatter = self._formatter_func
269-
n = len(self)
270-
if n == 0:
271-
summary = '[]'
272-
elif n == 1:
273-
first = formatter(self[0])
274-
summary = '[%s]' % first
275-
elif n == 2:
276-
first = formatter(self[0])
277-
last = formatter(self[-1])
278-
summary = '[%s, %s]' % (first, last)
279-
else:
280-
first = formatter(self[0])
281-
last = formatter(self[-1])
282-
summary = '[%s, ..., %s]' % (first, last)
283-
284-
return summary
285-
286263
def _format_attrs(self):
287264
"""
288265
Return a list of tuples of the (attr,formatted_value)

pandas/tseries/index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ def _is_dates_only(self):
597597
def _formatter_func(self):
598598
from pandas.core.format import _get_format_datetime64
599599
formatter = _get_format_datetime64(is_dates_only=self._is_dates_only)
600-
return lambda x: formatter(x, tz=self.tz)
600+
return lambda x: "'%s'" % formatter(x, tz=self.tz)
601601

602602
def __reduce__(self):
603603

0 commit comments

Comments
 (0)