Skip to content

Commit

Permalink
ENH: make None/NaN different from INF/-INF (GH pandas-dev#1919)
Browse files Browse the repository at this point in the history
  • Loading branch information
aflaxman committed Oct 8, 2012
1 parent 2827ac3 commit d73b033
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
3 changes: 2 additions & 1 deletion pandas/src/tseries.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ cdef double NEGINF = -INF

cpdef checknull(object val):
if util.is_float_object(val) or util.is_complex_object(val):
return val != val or val == INF or val == NEGINF
return val != val and val != INF and val != NEGINF
elif util.is_datetime64_object(val):
return get_datetime64_value(val) == NPY_NAT
elif isinstance(val, _NaT):
Expand All @@ -189,6 +189,7 @@ cpdef checknull(object val):
else:
return util._checknull(val)


def isscalar(object val):
return np.isscalar(val) or val is None or isinstance(val, _Timestamp)

Expand Down
5 changes: 4 additions & 1 deletion pandas/src/util.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ cdef inline is_array(object o):


cdef inline bint _checknull(object val):
import numpy as np
cdef double INF = <double> np.inf
cdef double NEGINF = -INF
try:
return bool(val is None or val != val)
return bool(val is None or (val != val and val != INF and val != NEGINF))
except ValueError:
return False

Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def test_notnull():
assert notnull(1.)
assert not notnull(None)
assert not notnull(np.NaN)
assert not notnull(np.inf)
assert not notnull(-np.inf)
assert notnull(np.inf)
assert notnull(-np.inf)

float_series = Series(np.random.randn(5))
obj_series = Series(np.random.randn(5), dtype=object)
Expand All @@ -30,8 +30,8 @@ def test_isnull():
assert not isnull(1.)
assert isnull(None)
assert isnull(np.NaN)
assert isnull(np.inf)
assert isnull(-np.inf)
assert not isnull(np.inf)
assert not isnull(-np.inf)

float_series = Series(np.random.randn(5))
obj_series = Series(np.random.randn(5), dtype=object)
Expand Down
4 changes: 3 additions & 1 deletion pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ def assert_almost_equal(a, b):
return

if isinstance(a, (bool, float, int)):
if np.isinf(a):
assert np.isinf(b), err_msg(a,b)
# case for zero
if abs(a) < 1e-5:
elif abs(a) < 1e-5:
np.testing.assert_almost_equal(
a, b, decimal=5, err_msg=err_msg(a, b), verbose=False)
else:
Expand Down

0 comments on commit d73b033

Please sign in to comment.