Skip to content

Commit b043274

Browse files
BUG: show series length in repr when truncated (GH15962)
Also follow the display.show_dimensions option.
1 parent b4701a6 commit b043274

File tree

3 files changed

+40
-35
lines changed

3 files changed

+40
-35
lines changed

pandas/core/series.py

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -978,9 +978,10 @@ def __unicode__(self):
978978
width, height = get_terminal_size()
979979
max_rows = (height if get_option("display.max_rows") == 0 else
980980
get_option("display.max_rows"))
981+
show_dimensions = get_option("display.show_dimensions")
981982

982983
self.to_string(buf=buf, name=self.name, dtype=self.dtype,
983-
max_rows=max_rows)
984+
max_rows=max_rows, length=show_dimensions)
984985
result = buf.getvalue()
985986

986987
return result
@@ -1019,44 +1020,27 @@ def to_string(self, buf=None, na_rep='NaN', float_format=None, header=True,
10191020
formatted : string (if not buffer passed)
10201021
"""
10211022

1022-
the_repr = self._get_repr(float_format=float_format, na_rep=na_rep,
1023-
header=header, index=index, length=length,
1024-
dtype=dtype, name=name, max_rows=max_rows)
1025-
1026-
# catch contract violations
1027-
if not isinstance(the_repr, compat.text_type):
1028-
raise AssertionError("result must be of type unicode, type"
1029-
" of result is {0!r}"
1030-
"".format(the_repr.__class__.__name__))
1031-
1032-
if buf is None:
1033-
return the_repr
1034-
else:
1035-
try:
1036-
buf.write(the_repr)
1037-
except AttributeError:
1038-
with open(buf, 'w') as f:
1039-
f.write(the_repr)
1040-
1041-
def _get_repr(self, name=False, header=True, index=True, length=True,
1042-
dtype=True, na_rep='NaN', float_format=None, max_rows=None):
1043-
"""
1044-
1045-
Internal function, should always return unicode string
1046-
"""
10471023
formatter = fmt.SeriesFormatter(self, name=name, length=length,
10481024
header=header, index=index,
10491025
dtype=dtype, na_rep=na_rep,
10501026
float_format=float_format,
10511027
max_rows=max_rows)
10521028
result = formatter.to_string()
10531029

1054-
# TODO: following check prob. not neces.
1030+
# catch contract violations
10551031
if not isinstance(result, compat.text_type):
10561032
raise AssertionError("result must be of type unicode, type"
10571033
" of result is {0!r}"
10581034
"".format(result.__class__.__name__))
1059-
return result
1035+
1036+
if buf is None:
1037+
return result
1038+
else:
1039+
try:
1040+
buf.write(result)
1041+
except AttributeError:
1042+
with open(buf, 'w') as f:
1043+
f.write(result)
10601044

10611045
def __iter__(self):
10621046
""" provide iteration over the values of the Series

pandas/formats/format.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ def _get_footer(self):
197197
escape_chars=('\t', '\r', '\n'))
198198
footer += ("Name: %s" % series_name) if name is not None else ""
199199

200-
if self.length:
200+
if (self.length is True or
201+
(self.length == 'truncate' and self.truncate_v)):
201202
if footer:
202203
footer += ', '
203204
footer += 'Length: %d' % len(self.series)

pandas/tests/formats/test_format.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,12 +1767,14 @@ def test_east_asian_unicode_series(self):
17671767
name=u'おおおおおおお')
17681768

17691769
expected = (u"0 あ\n ... \n"
1770-
u"3 ええええ\nName: おおおおおおお, dtype: object")
1770+
u"3 ええええ\n"
1771+
u"Name: おおおおおおお, Length: 4, dtype: object")
17711772
self.assertEqual(_rep(s), expected)
17721773

17731774
s.index = [u'ああ', u'いいいい', u'う', u'えええ']
17741775
expected = (u"ああ あ\n ... \n"
1775-
u"えええ ええええ\nName: おおおおおおお, dtype: object")
1776+
u"えええ ええええ\n"
1777+
u"Name: おおおおおおお, Length: 4, dtype: object")
17761778
self.assertEqual(_rep(s), expected)
17771779

17781780
# Emable Unicode option -----------------------------------------
@@ -1843,14 +1845,15 @@ def test_east_asian_unicode_series(self):
18431845
s = Series([u'あ', u'いい', u'ううう', u'ええええ'],
18441846
name=u'おおおおおおお')
18451847
expected = (u"0 あ\n ... \n"
1846-
u"3 ええええ\nName: おおおおおおお, dtype: object")
1848+
u"3 ええええ\n"
1849+
u"Name: おおおおおおお, Length: 4, dtype: object")
18471850
self.assertEqual(_rep(s), expected)
18481851

18491852
s.index = [u'ああ', u'いいいい', u'う', u'えええ']
18501853
expected = (u"ああ あ\n"
18511854
u" ... \n"
18521855
u"えええ ええええ\n"
1853-
u"Name: おおおおおおお, dtype: object")
1856+
u"Name: おおおおおおお, Length: 4, dtype: object")
18541857
self.assertEqual(_rep(s), expected)
18551858

18561859
# ambiguous unicode
@@ -2018,7 +2021,8 @@ def test_max_multi_index_display(self):
20182021
# Make sure #8532 is fixed
20192022
def test_consistent_format(self):
20202023
s = pd.Series([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.9999, 1, 1] * 10)
2021-
with option_context("display.max_rows", 10):
2024+
with option_context("display.max_rows", 10,
2025+
"display.show_dimensions", False):
20222026
res = repr(s)
20232027
exp = ('0 1.0000\n1 1.0000\n2 1.0000\n3 '
20242028
'1.0000\n4 1.0000\n ... \n125 '
@@ -2037,7 +2041,8 @@ def chck_ncols(self, s):
20372041

20382042
def test_format_explicit(self):
20392043
test_sers = gen_series_formatting()
2040-
with option_context("display.max_rows", 4):
2044+
with option_context("display.max_rows", 4,
2045+
"display.show_dimensions", False):
20412046
res = repr(test_sers['onel'])
20422047
exp = '0 a\n1 a\n ..\n98 a\n99 a\ndtype: object'
20432048
self.assertEqual(exp, res)
@@ -2084,6 +2089,21 @@ def getndots(s):
20842089
strrepr = repr(s).replace('\n', '')
20852090
self.assertEqual(getndots(strrepr), 3)
20862091

2092+
def test_show_dimensions(self):
2093+
s = Series(range(5))
2094+
2095+
assert 'Length' not in repr(s)
2096+
2097+
with option_context("display.max_rows", 4):
2098+
assert 'Length' in repr(s)
2099+
2100+
with option_context("display.show_dimensions", True):
2101+
assert 'Length' in repr(s)
2102+
2103+
with option_context("display.max_rows", 4,
2104+
"display.show_dimensions", False):
2105+
assert 'Length' not in repr(s)
2106+
20872107
def test_to_string_name(self):
20882108
s = Series(range(100), dtype='int64')
20892109
s.name = 'myser'

0 commit comments

Comments
 (0)