Skip to content

BUG: formatting for Series was printing multiple Dtype lines for long display #2930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@

class SeriesFormatter(object):

def __init__(self, series, buf=None, header=True, length=True,
na_rep='NaN', name=False, float_format=None, dtype=True):
def __init__(self, series, buf=None, header=True, length=True, dtype=True,
na_rep='NaN', name=False, float_format=None):
self.series = series
self.buf = buf if buf is not None else StringIO(u"")
self.name = name
self.dtype = dtype
self.na_rep = na_rep
self.length = length
self.dtype = dtype
self.header = header

if float_format is None:
Expand Down
21 changes: 13 additions & 8 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,9 +1047,10 @@ def __unicode__(self):
elif len(self.index) > 0:
result = self._get_repr(print_header=True,
length=len(self) > 50,
name=True)
name=True,
dtype=True)
else:
result = u'Series([], dtype=%s)' % self.dtype
result = u'Series([], Dtype: %s)' % self.dtype

assert type(result) == unicode
return result
Expand All @@ -1069,9 +1070,10 @@ def _tidy_repr(self, max_vals=20):
"""
num = max_vals // 2
head = self[:num]._get_repr(print_header=True, length=False,
name=False)
dtype=False, name=False)
tail = self[-(max_vals - num):]._get_repr(print_header=False,
length=False,
dtype=False,
name=False)
result = head + '\n...\n' + tail
result = '%s\n%s' % (result, self._repr_footer())
Expand All @@ -1085,7 +1087,7 @@ def _repr_footer(self):
com.pprint_thing(self.dtype.name))

def to_string(self, buf=None, na_rep='NaN', float_format=None,
nanRep=None, length=False, name=False):
nanRep=None, length=False, dtype=False, name=False):
"""
Render a string representation of the Series

Expand All @@ -1100,6 +1102,8 @@ def to_string(self, buf=None, na_rep='NaN', float_format=None,
default None
length : boolean, default False
Add the Series length
dtype : boolean, default False
Add the Series dtype
name : boolean, default False
Add the Series name (which may be None)

Expand All @@ -1114,7 +1118,7 @@ def to_string(self, buf=None, na_rep='NaN', float_format=None,
na_rep = nanRep

the_repr = self._get_repr(float_format=float_format, na_rep=na_rep,
length=length, name=name)
length=length, dtype=dtype, name=name)

assert type(the_repr) == unicode

Expand All @@ -1123,15 +1127,15 @@ def to_string(self, buf=None, na_rep='NaN', float_format=None,
else:
print >> buf, the_repr

def _get_repr(self, name=False, print_header=False, length=True,
def _get_repr(self, name=False, print_header=False, length=True, dtype=True,
na_rep='NaN', float_format=None):
"""

Internal function, should always return unicode string
"""

formatter = fmt.SeriesFormatter(self, name=name, header=print_header,
length=length, na_rep=na_rep,
length=length, dtype=dtype, na_rep=na_rep,
float_format=float_format)
result = formatter.to_string()
assert type(result) == unicode
Expand Down Expand Up @@ -3287,7 +3291,8 @@ def _repr_footer(self):

namestr = "Name: %s, " % str(
self.name) if self.name is not None else ""
return '%s%sLength: %d' % (freqstr, namestr, len(self))
return '%s%sLength: %d, Dtype: %s' % (freqstr, namestr, len(self),
com.pprint_thing(self.dtype.name))

def to_timestamp(self, freq=None, how='start', copy=True):
"""
Expand Down
27 changes: 15 additions & 12 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,15 @@ def test_wide_repr_wide_long_columns(self):
self.assertTrue('ccccc' in result)
self.assertTrue('ddddd' in result)

def test_long_series(self):
n = 1000
s = Series(np.random.randint(-50,50,n),index=['s%04d' % x for x in xrange(n)], dtype='int64')

import re
str_rep = str(s)
nmatches = len(re.findall('Dtype',str_rep))
self.assert_(nmatches == 1)

def test_to_string(self):
from pandas import read_table
import re
Expand Down Expand Up @@ -1119,7 +1128,7 @@ def test_to_string(self):
format = '%.4f'.__mod__
result = self.ts.to_string(float_format=format)
result = [x.split()[1] for x in result.split('\n')]
expected = [format(x) for x in self.ts] + [u'float64']
expected = [format(x) for x in self.ts]
self.assertEqual(result, expected)

# empty string
Expand All @@ -1132,7 +1141,7 @@ def test_to_string(self):
# name and length
cp = self.ts.copy()
cp.name = 'foo'
result = cp.to_string(length=True, name=True)
result = cp.to_string(length=True, name=True, dtype=True)
last_line = result.split('\n')[-1].strip()
self.assertEqual(last_line, "Freq: B, Name: foo, Length: %d, Dtype: float64" % len(cp))

Expand All @@ -1149,8 +1158,7 @@ def test_to_string_mixed(self):
expected = (u'0 foo\n'
u'1 NaN\n'
u'2 -1.23\n'
u'3 4.56\n'
u'Dtype: object')
u'3 4.56')
self.assertEqual(result, expected)

# but don't count NAs as floats
Expand All @@ -1159,17 +1167,15 @@ def test_to_string_mixed(self):
expected = (u'0 foo\n'
'1 NaN\n'
'2 bar\n'
'3 baz\n'
u'Dtype: object')
'3 baz')
self.assertEqual(result, expected)

s = Series(['foo', 5, 'bar', 'baz'])
result = s.to_string()
expected = (u'0 foo\n'
'1 5\n'
'2 bar\n'
'3 baz\n'
u'Dtype: object')
'3 baz')
self.assertEqual(result, expected)

def test_to_string_float_na_spacing(self):
Expand All @@ -1181,8 +1187,7 @@ def test_to_string_float_na_spacing(self):
'1 1.5678\n'
'2 NaN\n'
'3 -3.0000\n'
'4 NaN\n'
u'Dtype: float64')
'4 NaN')
self.assertEqual(result, expected)

def test_unicode_name_in_footer(self):
Expand Down Expand Up @@ -1216,14 +1221,12 @@ def test_timedelta64(self):
result = y.to_string()
self.assertTrue('1 days, 00:00:00' in result)
self.assertTrue('NaT' in result)
self.assertTrue('timedelta64[ns]' in result)

# with frac seconds
s = Series(date_range('2012-1-1', periods=3, freq='D'))
y = s-datetime(2012,1,1,microsecond=150)
result = y.to_string()
self.assertTrue('00:00:00.000150' in result)
self.assertTrue('timedelta64[ns]' in result)

def test_mixed_datetime64(self):
df = DataFrame({'A': [1, 2],
Expand Down