Use math library instead of numpy for nan/inf checks
Math is much faster than NumPy for scalar checks
@@ -11,6 +11,7 @@ performance and efficiency. The list of supported dtypes is:
from __future__ import absolute_import
import base64
+import math
from six import iterkeys
@@ -219,11 +220,12 @@ def traverse_data(obj, is_numpy=is_numpy, use_numpy=True):
# Check the base/common case first for performance reasons
# Also use type(x) is float because it's faster than isinstance
if type(item) is float:
- if np.isnan(item):
+ if math.isnan(item):
item = 'NaN'
- elif np.isposinf(item):
+ elif math.isinf(item):
+ if item > 0:
item = 'Infinity'
- elif np.isneginf(item):
+ else:
item = '-Infinity'
obj_copy.append(item)
elif isinstance(item, (list, tuple)): # check less common type second
