Skip to content

CLN: de-duplicate numeric type check in _libs/testing #36625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 6, 2020
30 changes: 7 additions & 23 deletions pandas/_libs/testing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,17 @@ from numpy cimport import_array

import_array()

from pandas._libs.util cimport is_array
from pandas._libs.util cimport (
is_array,
is_real_number_object,
)

from pandas.core.dtypes.common import is_dtype_equal
from pandas.core.dtypes.missing import array_equivalent, isna


cdef NUMERIC_TYPES = (
bool,
int,
float,
np.bool_,
np.int8,
np.int16,
np.int32,
np.int64,
np.uint8,
np.uint16,
np.uint32,
np.uint64,
np.float16,
np.float32,
np.float64,
)


cdef bint is_comparable_as_number(obj):
return isinstance(obj, NUMERIC_TYPES)
cdef bint is_comparable_as_real_number(obj):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is wouldn't even do this, just use the imported one directly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a huge deal, but unless we expect to use it elsewhere, id rather this new func live here rather than in util; otherwise it gets re-compiled into basically every cython module.

return is_real_number_object(obj)


cdef bint isiterable(obj):
Expand Down Expand Up @@ -198,7 +182,7 @@ cpdef assert_almost_equal(a, b,
# object comparison
return True

if is_comparable_as_number(a) and is_comparable_as_number(b):
if is_comparable_as_real_number(a) and is_comparable_as_real_number(b):
if array_equivalent(a, b, strict_nan=True):
# inf comparison
return True
Expand Down
4 changes: 4 additions & 0 deletions pandas/_libs/tslibs/util.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ cdef inline bint is_bool_object(object obj) nogil:
PyObject_TypeCheck(obj, &PyBoolArrType_Type))


cdef inline bint is_real_number_object(object obj) nogil:
return is_bool_object(obj) or is_integer_object(obj) or is_float_object(obj)


cdef inline bint is_timedelta64_object(object obj) nogil:
"""
Cython equivalent of `isinstance(val, np.timedelta64)`
Expand Down