Skip to content

Commit d0734ba

Browse files
yaduartjreback
authored andcommitted
BUG: Added checks for NaN in __call__ of EngFormatter
closes #11981 Author: Yadunandan <[email protected]> Closes #13124 from yaduart/bugfix-11981 and squashes the following commits: 8de1f64 [Yadunandan] BUG: Added checks for Nan in __call__ of EngFormatter
1 parent f0e47a9 commit d0734ba

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
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 ``pd.set_eng_float_format()`` 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

+17-2
Original file line numberDiff line numberDiff line change
@@ -3087,11 +3087,11 @@ def test_to_csv_doublequote(self):
30873087

30883088
def test_to_csv_escapechar(self):
30893089
df = DataFrame({'col': ['a"a', '"bb"']})
3090-
expected = """\
3090+
expected = '''\
30913091
"","col"
30923092
"0","a\\"a"
30933093
"1","\\"bb\\""
3094-
"""
3094+
'''
30953095

30963096
with tm.ensure_clean('test.csv') as path: # QUOTE_ALL
30973097
df.to_csv(path, quoting=1, doublequote=False, escapechar='\\')
@@ -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+
self.assertTrue('NaN' in result)
3942+
self.reset_display_options()
39283943

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

0 commit comments

Comments
 (0)