From 8de1f64ea1218f6a03e179b8ff56959e816a6353 Mon Sep 17 00:00:00 2001 From: Yadunandan Date: Mon, 9 May 2016 22:50:46 +1000 Subject: [PATCH] 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 --- doc/source/whatsnew/v0.18.2.txt | 1 + pandas/formats/format.py | 3 +++ pandas/tests/formats/test_format.py | 15 +++++++++++++++ 3 files changed, 19 insertions(+) diff --git a/doc/source/whatsnew/v0.18.2.txt b/doc/source/whatsnew/v0.18.2.txt index fa426aa30bc65..9ffe068fac166 100644 --- a/doc/source/whatsnew/v0.18.2.txt +++ b/doc/source/whatsnew/v0.18.2.txt @@ -132,3 +132,4 @@ Bug Fixes - Bug in ``NaT`` - ``Period`` raises ``AttributeError`` (:issue:`13071`) - Bug in ``Period`` addition raises ``TypeError`` if ``Period`` is on right hand side (:issue:`13069`) +- Bug in ``__call__`` of ``EngFormatter`` that would prevent NaN's from formatting (:issue:`11981`) diff --git a/pandas/formats/format.py b/pandas/formats/format.py index c3ffc018d1031..70b506a1415c1 100644 --- a/pandas/formats/format.py +++ b/pandas/formats/format.py @@ -2590,6 +2590,9 @@ def __call__(self, num): import math dnum = decimal.Decimal(str(num)) + if decimal.Decimal.is_nan(dnum): + return 'NaN' + sign = 1 if dnum < 0: # pragma: no cover diff --git a/pandas/tests/formats/test_format.py b/pandas/tests/formats/test_format.py index 4fcee32c46067..d5b90c39225d1 100644 --- a/pandas/tests/formats/test_format.py +++ b/pandas/tests/formats/test_format.py @@ -3925,6 +3925,21 @@ def test_rounding(self): result = formatter(0) self.assertEqual(result, u(' 0.000')) + def test_nan(self): + # Issue #11981 + + formatter = fmt.EngFormatter(accuracy=1, use_eng_prefix=True) + result = formatter(np.nan) + self.assertEqual(result, u('NaN')) + + df = pd.DataFrame({'a':[1.5, 10.3, 20.5], + 'b':[50.3, 60.67, 70.12], + 'c':[100.2, 101.33, 120.33]}) + pt = df.pivot_table(values='a', index='b', columns='c') + fmt.set_eng_float_format(accuracy=1) + result = pt.to_string() + # Smoke test for Invalid Operation Error, no comparision required + self.reset_display_options() def _three_digit_exp(): return '%.4g' % 1.7e8 == '1.7e+008'