From d73b03324cc51b8ed18408271cebef19a263d162 Mon Sep 17 00:00:00 2001 From: Abraham Flaxman Date: Mon, 8 Oct 2012 14:01:27 -0700 Subject: [PATCH] ENH: make None/NaN different from INF/-INF (GH #1919) --- pandas/src/tseries.pyx | 3 ++- pandas/src/util.pxd | 5 ++++- pandas/tests/test_common.py | 8 ++++---- pandas/util/testing.py | 4 +++- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/pandas/src/tseries.pyx b/pandas/src/tseries.pyx index 54641a78a08d9..237b0220aa34d 100644 --- a/pandas/src/tseries.pyx +++ b/pandas/src/tseries.pyx @@ -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): @@ -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) diff --git a/pandas/src/util.pxd b/pandas/src/util.pxd index fe6e4391c59e5..62bf9dcaa8250 100644 --- a/pandas/src/util.pxd +++ b/pandas/src/util.pxd @@ -60,8 +60,11 @@ cdef inline is_array(object o): cdef inline bint _checknull(object val): + import numpy as np + cdef double INF = 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 diff --git a/pandas/tests/test_common.py b/pandas/tests/test_common.py index e2b0b918f0142..865a9de941c2f 100644 --- a/pandas/tests/test_common.py +++ b/pandas/tests/test_common.py @@ -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) @@ -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) diff --git a/pandas/util/testing.py b/pandas/util/testing.py index 904426731738a..a15be23c1c4c6 100644 --- a/pandas/util/testing.py +++ b/pandas/util/testing.py @@ -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: