Skip to content

Commit 50dfc4f

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

File tree

3 files changed

+40
-35
lines changed

3 files changed

+40
-35
lines changed

pandas/core/series.py

+12-28
Original file line numberDiff line numberDiff line change
@@ -980,9 +980,10 @@ def __unicode__(self):
980980
width, height = get_terminal_size()
981981
max_rows = (height if get_option("display.max_rows") == 0 else
982982
get_option("display.max_rows"))
983+
show_dimensions = get_option("display.show_dimensions")
983984

984985
self.to_string(buf=buf, name=self.name, dtype=self.dtype,
985-
max_rows=max_rows)
986+
max_rows=max_rows, length=show_dimensions)
986987
result = buf.getvalue()
987988

988989
return result
@@ -1021,44 +1022,27 @@ def to_string(self, buf=None, na_rep='NaN', float_format=None, header=True,
10211022
formatted : string (if not buffer passed)
10221023
"""
10231024

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

1056-
# TODO: following check prob. not neces.
1032+
# catch contract violations
10571033
if not isinstance(result, compat.text_type):
10581034
raise AssertionError("result must be of type unicode, type"
10591035
" of result is {0!r}"
10601036
"".format(result.__class__.__name__))
1061-
return result
1037+
1038+
if buf is None:
1039+
return result
1040+
else:
1041+
try:
1042+
buf.write(result)
1043+
except AttributeError:
1044+
with open(buf, 'w') as f:
1045+
f.write(result)
10621046

10631047
def __iter__(self):
10641048
""" provide iteration over the values of the Series

pandas/io/formats/format.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ def _get_footer(self):
199199
escape_chars=('\t', '\r', '\n'))
200200
footer += ("Name: %s" % series_name) if name is not None else ""
201201

202-
if self.length:
202+
if (self.length is True or
203+
(self.length == 'truncate' and self.truncate_v)):
203204
if footer:
204205
footer += ', '
205206
footer += 'Length: %d' % len(self.series)

pandas/tests/io/formats/test_format.py

+26-6
Original file line numberDiff line numberDiff line change
@@ -1770,12 +1770,14 @@ def test_east_asian_unicode_series(self):
17701770
name=u'おおおおおおお')
17711771

17721772
expected = (u"0 あ\n ... \n"
1773-
u"3 ええええ\nName: おおおおおおお, dtype: object")
1773+
u"3 ええええ\n"
1774+
u"Name: おおおおおおお, Length: 4, dtype: object")
17741775
self.assertEqual(_rep(s), expected)
17751776

17761777
s.index = [u'ああ', u'いいいい', u'う', u'えええ']
17771778
expected = (u"ああ あ\n ... \n"
1778-
u"えええ ええええ\nName: おおおおおおお, dtype: object")
1779+
u"えええ ええええ\n"
1780+
u"Name: おおおおおおお, Length: 4, dtype: object")
17791781
self.assertEqual(_rep(s), expected)
17801782

17811783
# Emable Unicode option -----------------------------------------
@@ -1846,14 +1848,15 @@ def test_east_asian_unicode_series(self):
18461848
s = Series([u'あ', u'いい', u'ううう', u'ええええ'],
18471849
name=u'おおおおおおお')
18481850
expected = (u"0 あ\n ... \n"
1849-
u"3 ええええ\nName: おおおおおおお, dtype: object")
1851+
u"3 ええええ\n"
1852+
u"Name: おおおおおおお, Length: 4, dtype: object")
18501853
self.assertEqual(_rep(s), expected)
18511854

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

18591862
# ambiguous unicode
@@ -2021,7 +2024,8 @@ def test_max_multi_index_display(self):
20212024
# Make sure #8532 is fixed
20222025
def test_consistent_format(self):
20232026
s = pd.Series([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.9999, 1, 1] * 10)
2024-
with option_context("display.max_rows", 10):
2027+
with option_context("display.max_rows", 10,
2028+
"display.show_dimensions", False):
20252029
res = repr(s)
20262030
exp = ('0 1.0000\n1 1.0000\n2 1.0000\n3 '
20272031
'1.0000\n4 1.0000\n ... \n125 '
@@ -2040,7 +2044,8 @@ def chck_ncols(self, s):
20402044

20412045
def test_format_explicit(self):
20422046
test_sers = gen_series_formatting()
2043-
with option_context("display.max_rows", 4):
2047+
with option_context("display.max_rows", 4,
2048+
"display.show_dimensions", False):
20442049
res = repr(test_sers['onel'])
20452050
exp = '0 a\n1 a\n ..\n98 a\n99 a\ndtype: object'
20462051
self.assertEqual(exp, res)
@@ -2087,6 +2092,21 @@ def getndots(s):
20872092
strrepr = repr(s).replace('\n', '')
20882093
self.assertEqual(getndots(strrepr), 3)
20892094

2095+
def test_show_dimensions(self):
2096+
s = Series(range(5))
2097+
2098+
assert 'Length' not in repr(s)
2099+
2100+
with option_context("display.max_rows", 4):
2101+
assert 'Length' in repr(s)
2102+
2103+
with option_context("display.show_dimensions", True):
2104+
assert 'Length' in repr(s)
2105+
2106+
with option_context("display.max_rows", 4,
2107+
"display.show_dimensions", False):
2108+
assert 'Length' not in repr(s)
2109+
20902110
def test_to_string_name(self):
20912111
s = Series(range(100), dtype='int64')
20922112
s.name = 'myser'

0 commit comments

Comments
 (0)