Skip to content

Commit d73b033

Browse files
committed
ENH: make None/NaN different from INF/-INF (GH pandas-dev#1919)
1 parent 2827ac3 commit d73b033

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

pandas/src/tseries.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ cdef double NEGINF = -INF
179179

180180
cpdef checknull(object val):
181181
if util.is_float_object(val) or util.is_complex_object(val):
182-
return val != val or val == INF or val == NEGINF
182+
return val != val and val != INF and val != NEGINF
183183
elif util.is_datetime64_object(val):
184184
return get_datetime64_value(val) == NPY_NAT
185185
elif isinstance(val, _NaT):
@@ -189,6 +189,7 @@ cpdef checknull(object val):
189189
else:
190190
return util._checknull(val)
191191

192+
192193
def isscalar(object val):
193194
return np.isscalar(val) or val is None or isinstance(val, _Timestamp)
194195

pandas/src/util.pxd

+4-1
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,11 @@ cdef inline is_array(object o):
6060

6161

6262
cdef inline bint _checknull(object val):
63+
import numpy as np
64+
cdef double INF = <double> np.inf
65+
cdef double NEGINF = -INF
6366
try:
64-
return bool(val is None or val != val)
67+
return bool(val is None or (val != val and val != INF and val != NEGINF))
6568
except ValueError:
6669
return False
6770

pandas/tests/test_common.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ def test_notnull():
1818
assert notnull(1.)
1919
assert not notnull(None)
2020
assert not notnull(np.NaN)
21-
assert not notnull(np.inf)
22-
assert not notnull(-np.inf)
21+
assert notnull(np.inf)
22+
assert notnull(-np.inf)
2323

2424
float_series = Series(np.random.randn(5))
2525
obj_series = Series(np.random.randn(5), dtype=object)
@@ -30,8 +30,8 @@ def test_isnull():
3030
assert not isnull(1.)
3131
assert isnull(None)
3232
assert isnull(np.NaN)
33-
assert isnull(np.inf)
34-
assert isnull(-np.inf)
33+
assert not isnull(np.inf)
34+
assert not isnull(-np.inf)
3535

3636
float_series = Series(np.random.randn(5))
3737
obj_series = Series(np.random.randn(5), dtype=object)

pandas/util/testing.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ def assert_almost_equal(a, b):
103103
return
104104

105105
if isinstance(a, (bool, float, int)):
106+
if np.isinf(a):
107+
assert np.isinf(b), err_msg(a,b)
106108
# case for zero
107-
if abs(a) < 1e-5:
109+
elif abs(a) < 1e-5:
108110
np.testing.assert_almost_equal(
109111
a, b, decimal=5, err_msg=err_msg(a, b), verbose=False)
110112
else:

0 commit comments

Comments
 (0)