Skip to content

Commit 48ad2f4

Browse files
swfiuajreback
authored andcommitted
BUG: add check for infinity in __call__ of EngFormatter
EngFormatter throws an exception if passed 'inf' as below. This patch checks for infinity and just returns 'inf' in that case. xref #11981 Author: Johnny Gill <[email protected]> Closes #14214 from swfiua/master and squashes the following commits: f2348e7 [Johnny Gill] test EngFormatter with np.inf 83f4091 [Johnny Gill] add check for infinity in __call__ of EngFormatter
1 parent f363236 commit 48ad2f4

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v0.19.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,7 @@ Bug Fixes
15071507
- Bug in ``Series`` and ``Index`` comparison may output incorrect result if it contains ``NaT`` with ``object`` dtype (:issue:`13592`)
15081508
- Bug in ``Period`` addition raises ``TypeError`` if ``Period`` is on right hand side (:issue:`13069`)
15091509
- Bug in ``Peirod`` and ``Series`` or ``Index`` comparison raises ``TypeError`` (:issue:`13200`)
1510-
- Bug in ``pd.set_eng_float_format()`` that would prevent NaN's from formatting (:issue:`11981`)
1510+
- Bug in ``pd.set_eng_float_format()`` that would prevent NaN and Inf from formatting (:issue:`11981`)
15111511
- Bug in ``.unstack`` with ``Categorical`` dtype resets ``.ordered`` to ``True`` (:issue:`13249`)
15121512
- Clean some compile time warnings in datetime parsing (:issue:`13607`)
15131513
- Bug in ``factorize`` raises ``AmbiguousTimeError`` if data contains datetime near DST boundary (:issue:`13750`)

pandas/formats/format.py

+3
Original file line numberDiff line numberDiff line change
@@ -2586,6 +2586,9 @@ def __call__(self, num):
25862586
if decimal.Decimal.is_nan(dnum):
25872587
return 'NaN'
25882588

2589+
if decimal.Decimal.is_infinite(dnum):
2590+
return 'inf'
2591+
25892592
sign = 1
25902593

25912594
if dnum < 0: # pragma: no cover

pandas/tests/formats/test_format.py

+8
Original file line numberDiff line numberDiff line change
@@ -4047,6 +4047,14 @@ def test_nan(self):
40474047
self.assertTrue('NaN' in result)
40484048
self.reset_display_options()
40494049

4050+
def test_inf(self):
4051+
# Issue #11981
4052+
4053+
formatter = fmt.EngFormatter(accuracy=1, use_eng_prefix=True)
4054+
result = formatter(np.inf)
4055+
self.assertEqual(result, u('inf'))
4056+
4057+
40504058
def _three_digit_exp():
40514059
return '%.4g' % 1.7e8 == '1.7e+008'
40524060

0 commit comments

Comments
 (0)