diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index aaac70755cbac..5180f513dfed0 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -1285,124 +1285,6 @@ def _is_unorderable_exception(e: TypeError) -> bool: return "unorderable" in str(e) -def is_numeric_v_string_like(a, b): - """ - Check if we are comparing a string-like object to a numeric ndarray. - - NumPy doesn't like to compare such objects, especially numeric arrays - and scalar string-likes. - - Parameters - ---------- - a : array-like, scalar - The first object to check. - b : array-like, scalar - The second object to check. - - Returns - ------- - boolean - Whether we return a comparing a string-like object to a numeric array. - - Examples - -------- - >>> is_numeric_v_string_like(1, 1) - False - >>> is_numeric_v_string_like("foo", "foo") - False - >>> is_numeric_v_string_like(1, "foo") # non-array numeric - False - >>> is_numeric_v_string_like(np.array([1]), "foo") - True - >>> is_numeric_v_string_like("foo", np.array([1])) # symmetric check - True - >>> is_numeric_v_string_like(np.array([1, 2]), np.array(["foo"])) - True - >>> is_numeric_v_string_like(np.array(["foo"]), np.array([1, 2])) - True - >>> is_numeric_v_string_like(np.array([1]), np.array([2])) - False - >>> is_numeric_v_string_like(np.array(["foo"]), np.array(["foo"])) - False - """ - - is_a_array = isinstance(a, np.ndarray) - is_b_array = isinstance(b, np.ndarray) - - is_a_numeric_array = is_a_array and is_numeric_dtype(a) - is_b_numeric_array = is_b_array and is_numeric_dtype(b) - is_a_string_array = is_a_array and is_string_like_dtype(a) - is_b_string_array = is_b_array and is_string_like_dtype(b) - - is_a_scalar_string_like = not is_a_array and isinstance(a, str) - is_b_scalar_string_like = not is_b_array and isinstance(b, str) - - return ( - (is_a_numeric_array and is_b_scalar_string_like) - or (is_b_numeric_array and is_a_scalar_string_like) - or (is_a_numeric_array and is_b_string_array) - or (is_b_numeric_array and is_a_string_array) - ) - - -def is_datetimelike_v_numeric(a, b): - """ - Check if we are comparing a datetime-like object to a numeric object. - - By "numeric," we mean an object that is either of an int or float dtype. - - Parameters - ---------- - a : array-like, scalar - The first object to check. - b : array-like, scalar - The second object to check. - - Returns - ------- - boolean - Whether we return a comparing a datetime-like to a numeric object. - - Examples - -------- - >>> dt = np.datetime64(pd.datetime(2017, 1, 1)) - >>> - >>> is_datetimelike_v_numeric(1, 1) - False - >>> is_datetimelike_v_numeric(dt, dt) - False - >>> is_datetimelike_v_numeric(1, dt) - True - >>> is_datetimelike_v_numeric(dt, 1) # symmetric check - True - >>> is_datetimelike_v_numeric(np.array([dt]), 1) - True - >>> is_datetimelike_v_numeric(np.array([1]), dt) - True - >>> is_datetimelike_v_numeric(np.array([dt]), np.array([1])) - True - >>> is_datetimelike_v_numeric(np.array([1]), np.array([2])) - False - >>> is_datetimelike_v_numeric(np.array([dt]), np.array([dt])) - False - """ - - if not hasattr(a, "dtype"): - a = np.asarray(a) - if not hasattr(b, "dtype"): - b = np.asarray(b) - - def is_numeric(x): - """ - Check if an object has a numeric dtype (i.e. integer or float). - """ - return is_integer_dtype(x) or is_float_dtype(x) - - return (needs_i8_conversion(a) and is_numeric(b)) or ( - needs_i8_conversion(b) and is_numeric(a) - ) - - def needs_i8_conversion(arr_or_dtype): """ Check whether the array or dtype should be converted to int64. diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index 22e38a805f996..0a8f636b4cb2a 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -17,7 +17,6 @@ is_complex_dtype, is_datetime64_dtype, is_datetime64tz_dtype, - is_datetimelike_v_numeric, is_dtype_equal, is_extension_array_dtype, is_float_dtype, @@ -463,12 +462,8 @@ def array_equivalent(left, right, strict_nan=False): return True return ((left == right) | (isna(left) & isna(right))).all() - # numpy will will not allow this type of datetimelike vs integer comparison - elif is_datetimelike_v_numeric(left, right): - return False - - # M8/m8 - elif needs_i8_conversion(left) and needs_i8_conversion(right): + elif needs_i8_conversion(left) or needs_i8_conversion(right): + # datetime64, timedelta64, Period if not is_dtype_equal(left.dtype, right.dtype): return False diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index f86a294475ee2..0e97e55acddad 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -18,10 +18,8 @@ ) from pandas.core.dtypes.common import ( _NS_DTYPE, - is_datetimelike_v_numeric, is_extension_array_dtype, is_list_like, - is_numeric_v_string_like, is_scalar, is_sparse, ) @@ -1926,15 +1924,7 @@ def _compare_or_regex_search(a, b, regex=False): is_a_array = isinstance(a, np.ndarray) is_b_array = isinstance(b, np.ndarray) - # numpy deprecation warning to have i8 vs integer comparisons - if is_datetimelike_v_numeric(a, b): - result = False - - # numpy deprecation warning if comparing numeric vs string-like - elif is_numeric_v_string_like(a, b): - result = False - else: - result = op(a) + result = op(a) if is_scalar(result) and (is_a_array or is_b_array): type_names = [type(a).__name__, type(b).__name__] diff --git a/pandas/core/missing.py b/pandas/core/missing.py index c1e63a49a0f0a..fb148b39c8a86 100644 --- a/pandas/core/missing.py +++ b/pandas/core/missing.py @@ -12,7 +12,6 @@ is_datetime64_dtype, is_datetime64tz_dtype, is_integer_dtype, - is_numeric_v_string_like, is_scalar, is_timedelta64_dtype, needs_i8_conversion, @@ -39,24 +38,14 @@ def mask_missing(arr, values_to_mask): mask = None for x in nonna: if mask is None: - - # numpy elementwise comparison warning - if is_numeric_v_string_like(arr, x): - mask = False - else: - mask = arr == x + mask = arr == x # if x is a string and arr is not, then we get False and we must # expand the mask to size arr.shape if is_scalar(mask): mask = np.zeros(arr.shape, dtype=bool) else: - - # numpy elementwise comparison warning - if is_numeric_v_string_like(arr, x): - mask |= False - else: - mask |= arr == x + mask |= arr == x if na_mask.any(): if mask is None: diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py index 5e409b85049ae..7abaa0651449e 100644 --- a/pandas/tests/dtypes/test_common.py +++ b/pandas/tests/dtypes/test_common.py @@ -493,34 +493,6 @@ def test_is_datetime_or_timedelta_dtype(): assert com.is_datetime_or_timedelta_dtype(np.array([], dtype=np.datetime64)) -def test_is_numeric_v_string_like(): - assert not com.is_numeric_v_string_like(1, 1) - assert not com.is_numeric_v_string_like(1, "foo") - assert not com.is_numeric_v_string_like("foo", "foo") - assert not com.is_numeric_v_string_like(np.array([1]), np.array([2])) - assert not com.is_numeric_v_string_like(np.array(["foo"]), np.array(["foo"])) - - assert com.is_numeric_v_string_like(np.array([1]), "foo") - assert com.is_numeric_v_string_like("foo", np.array([1])) - assert com.is_numeric_v_string_like(np.array([1, 2]), np.array(["foo"])) - assert com.is_numeric_v_string_like(np.array(["foo"]), np.array([1, 2])) - - -def test_is_datetimelike_v_numeric(): - dt = np.datetime64(pd.datetime(2017, 1, 1)) - - assert not com.is_datetimelike_v_numeric(1, 1) - assert not com.is_datetimelike_v_numeric(dt, dt) - assert not com.is_datetimelike_v_numeric(np.array([1]), np.array([2])) - assert not com.is_datetimelike_v_numeric(np.array([dt]), np.array([dt])) - - assert com.is_datetimelike_v_numeric(1, dt) - assert com.is_datetimelike_v_numeric(1, dt) - assert com.is_datetimelike_v_numeric(np.array([dt]), 1) - assert com.is_datetimelike_v_numeric(np.array([1]), dt) - assert com.is_datetimelike_v_numeric(np.array([dt]), np.array([1])) - - def test_needs_i8_conversion(): assert not com.needs_i8_conversion(str) assert not com.needs_i8_conversion(np.int64)