Skip to content

Commit

Permalink
CLN: Partially revert #29553 (#51137)
Browse files Browse the repository at this point in the history
* CLN: Partially revert #29553

* fix tests

* Update missing.py
  • Loading branch information
lithomas1 authored Feb 6, 2023
1 parent 9bd15db commit 1efc2d7
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 100 deletions.
13 changes: 1 addition & 12 deletions pandas/core/array_algos/replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
)

from pandas.core.dtypes.common import (
is_datetimelike_v_numeric,
is_numeric_v_string_like,
is_re,
is_re_compilable,
is_scalar,
Expand All @@ -44,7 +42,7 @@ def should_use_regex(regex: bool, to_replace: Any) -> bool:

def compare_or_regex_search(
a: ArrayLike, b: Scalar | Pattern, regex: bool, mask: npt.NDArray[np.bool_]
) -> ArrayLike | bool:
) -> ArrayLike:
"""
Compare two array-like inputs of the same shape or two scalar values
Expand Down Expand Up @@ -95,15 +93,6 @@ def _check_comparison_types(
if isinstance(a, np.ndarray):
a = a[mask]

if is_numeric_v_string_like(a, b):
# GH#29553 avoid deprecation warnings from numpy
return np.zeros(a.shape, dtype=bool)

elif is_datetimelike_v_numeric(a, b):
# GH#29553 avoid deprecation warnings from numpy
_check_comparison_types(False, a, b)
return False

result = op(a)

if isinstance(result, np.ndarray) and mask is not None:
Expand Down
60 changes: 0 additions & 60 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,64 +1067,6 @@ def is_numeric_v_string_like(a: ArrayLike, b) -> bool:
)


# This exists to silence numpy deprecation warnings, see GH#29553
def is_datetimelike_v_numeric(a, b) -> bool:
"""
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
--------
>>> from datetime import datetime
>>> dt = np.datetime64(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) -> bool:
"""
Check whether the array or dtype should be converted to int64.
Expand Down Expand Up @@ -1790,7 +1732,6 @@ def is_all_strings(value: ArrayLike) -> bool:
"is_datetime64_dtype",
"is_datetime64_ns_dtype",
"is_datetime64tz_dtype",
"is_datetimelike_v_numeric",
"is_datetime_or_timedelta_dtype",
"is_decimal",
"is_dict_like",
Expand All @@ -1809,7 +1750,6 @@ def is_all_strings(value: ArrayLike) -> bool:
"is_number",
"is_numeric_dtype",
"is_any_numeric_dtype",
"is_numeric_v_string_like",
"is_object_dtype",
"is_period_dtype",
"is_re",
Expand Down
7 changes: 0 additions & 7 deletions pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
is_bool_dtype,
is_categorical_dtype,
is_complex_dtype,
is_datetimelike_v_numeric,
is_dtype_equal,
is_extension_array_dtype,
is_float_dtype,
Expand Down Expand Up @@ -505,8 +504,6 @@ def array_equivalent(
# fastpath when we require that the dtypes match (Block.equals)
if left.dtype.kind in ["f", "c"]:
return _array_equivalent_float(left, right)
elif is_datetimelike_v_numeric(left.dtype, right.dtype):
return False
elif needs_i8_conversion(left.dtype):
return _array_equivalent_datetimelike(left, right)
elif is_string_or_object_np_dtype(left.dtype):
Expand All @@ -529,10 +526,6 @@ def array_equivalent(
return True
return ((left == right) | (isna(left) & isna(right))).all()

elif is_datetimelike_v_numeric(left, right):
# GH#29553 avoid numpy deprecation warning
return False

elif needs_i8_conversion(left.dtype) or needs_i8_conversion(right.dtype):
# datetime64, timedelta64, Period
if not is_dtype_equal(left.dtype, right.dtype):
Expand Down
5 changes: 1 addition & 4 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,10 +677,7 @@ def replace_list(
# GH#38086 faster if we know we dont need to check for regex
masks = [missing.mask_missing(values, s[0]) for s in pairs]

# error: Argument 1 to "extract_bool_array" has incompatible type
# "Union[ExtensionArray, ndarray, bool]"; expected "Union[ExtensionArray,
# ndarray]"
masks = [extract_bool_array(x) for x in masks] # type: ignore[arg-type]
masks = [extract_bool_array(x) for x in masks]

rb = [self if inplace else self.copy()]
for i, (src, dest) in enumerate(pairs):
Expand Down
17 changes: 0 additions & 17 deletions pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

from datetime import datetime

import numpy as np
import pytest

Expand Down Expand Up @@ -517,21 +515,6 @@ def test_is_numeric_v_string_like():
assert com.is_numeric_v_string_like(np.array(["foo"]), np.array([1, 2]))


def test_is_datetimelike_v_numeric():
dt = np.datetime64(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)
Expand Down

0 comments on commit 1efc2d7

Please sign in to comment.