Skip to content

Commit 8de1f64

Browse files
committed
BUG: Added checks for Nan in __call__ of EngFormatter
Closes #11981 Updated the v0.18.2 document Updated the document v0.18.2.txt and the test test_nan Updated test_nan for smoke test
1 parent af7bdd3 commit 8de1f64

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

doc/source/whatsnew/v0.18.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,4 @@ Bug Fixes
132132

133133
- Bug in ``NaT`` - ``Period`` raises ``AttributeError`` (:issue:`13071`)
134134
- Bug in ``Period`` addition raises ``TypeError`` if ``Period`` is on right hand side (:issue:`13069`)
135+
- Bug in ``__call__`` of ``EngFormatter`` that would prevent NaN's from formatting (:issue:`11981`)

pandas/formats/format.py

+3
Original file line numberDiff line numberDiff line change
@@ -2590,6 +2590,9 @@ def __call__(self, num):
25902590
import math
25912591
dnum = decimal.Decimal(str(num))
25922592

2593+
if decimal.Decimal.is_nan(dnum):
2594+
return 'NaN'
2595+
25932596
sign = 1
25942597

25952598
if dnum < 0: # pragma: no cover

pandas/tests/formats/test_format.py

+15
Original file line numberDiff line numberDiff line change
@@ -3925,6 +3925,21 @@ def test_rounding(self):
39253925
result = formatter(0)
39263926
self.assertEqual(result, u(' 0.000'))
39273927

3928+
def test_nan(self):
3929+
# Issue #11981
3930+
3931+
formatter = fmt.EngFormatter(accuracy=1, use_eng_prefix=True)
3932+
result = formatter(np.nan)
3933+
self.assertEqual(result, u('NaN'))
3934+
3935+
df = pd.DataFrame({'a':[1.5, 10.3, 20.5],
3936+
'b':[50.3, 60.67, 70.12],
3937+
'c':[100.2, 101.33, 120.33]})
3938+
pt = df.pivot_table(values='a', index='b', columns='c')
3939+
fmt.set_eng_float_format(accuracy=1)
3940+
result = pt.to_string()
3941+
# Smoke test for Invalid Operation Error, no comparision required
3942+
self.reset_display_options()
39283943

39293944
def _three_digit_exp():
39303945
return '%.4g' % 1.7e8 == '1.7e+008'

0 commit comments

Comments
 (0)