Skip to content

Commit

Permalink
BUG: don't truncate small FP numbers to zero in DataFrame formatting c…
Browse files Browse the repository at this point in the history
…lose #1911
  • Loading branch information
wesm committed Sep 17, 2012
1 parent f1c51ce commit de31cbe
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ pandas 0.9.0
- Fix negative integer indexing regression in .ix from 0.7.x (#1888)
- Fix error while retrieving timezone and utc offset from subclasses of
datetime.tzinfo without .zone and ._utcoffset attributes (#1922)
- Fix DataFrame formatting of small, non-zero FP numbers (#1911)

pandas 0.8.1
============
Expand Down
9 changes: 8 additions & 1 deletion pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,12 +667,19 @@ def get_result(self):

too_long = maxlen > self.digits + 5

abs_vals = np.abs(self.values)

# this is pretty arbitrary for now
has_large_values = (np.abs(self.values) > 1e8).any()
has_large_values = (abs_vals > 1e8).any()
has_small_values = ((abs_vals < 10**(-self.digits)) &
(abs_vals > 0)).any()

if too_long and has_large_values:
fmt_str = '%% .%de' % (self.digits - 1)
fmt_values = self._format_with(fmt_str)
elif has_small_values:
fmt_str = '%% .%de' % (self.digits - 1)
fmt_values = self._format_with(fmt_str)

return _make_fixed_width(fmt_values, self.justify)

Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,24 @@ def test_to_string_float_formatting(self):
'1 2.512000e-01')
assert(df_s == expected)

def test_to_string_small_float_values(self):
df = DataFrame({'a': [1.5, 1e-17, -5.5e-7]})

result = df.to_string()
expected = (' a\n'
'0 1.500000e+00\n'
'1 1.000000e-17\n'
'2 -5.500000e-07')
self.assertEqual(result, expected)

# but not all exactly zero
df = df * 0
result = df.to_string()
expected = (' 0\n'
'0 0\n'
'1 0\n'
'2 -0')

def test_to_string_float_index(self):
index = Index([1.5, 2, 3, 4, 5])
df = DataFrame(range(5), index=index)
Expand Down

0 comments on commit de31cbe

Please sign in to comment.