Skip to content

Commit bcbc468

Browse files
jbrockmendeljreback
authored andcommitted
CLN: remove unnecessary type checks (#29517)
1 parent e8e22d4 commit bcbc468

File tree

5 files changed

+5
-177
lines changed

5 files changed

+5
-177
lines changed

pandas/core/dtypes/common.py

-118
Original file line numberDiff line numberDiff line change
@@ -1285,124 +1285,6 @@ def _is_unorderable_exception(e: TypeError) -> bool:
12851285
return "unorderable" in str(e)
12861286

12871287

1288-
def is_numeric_v_string_like(a, b):
1289-
"""
1290-
Check if we are comparing a string-like object to a numeric ndarray.
1291-
1292-
NumPy doesn't like to compare such objects, especially numeric arrays
1293-
and scalar string-likes.
1294-
1295-
Parameters
1296-
----------
1297-
a : array-like, scalar
1298-
The first object to check.
1299-
b : array-like, scalar
1300-
The second object to check.
1301-
1302-
Returns
1303-
-------
1304-
boolean
1305-
Whether we return a comparing a string-like object to a numeric array.
1306-
1307-
Examples
1308-
--------
1309-
>>> is_numeric_v_string_like(1, 1)
1310-
False
1311-
>>> is_numeric_v_string_like("foo", "foo")
1312-
False
1313-
>>> is_numeric_v_string_like(1, "foo") # non-array numeric
1314-
False
1315-
>>> is_numeric_v_string_like(np.array([1]), "foo")
1316-
True
1317-
>>> is_numeric_v_string_like("foo", np.array([1])) # symmetric check
1318-
True
1319-
>>> is_numeric_v_string_like(np.array([1, 2]), np.array(["foo"]))
1320-
True
1321-
>>> is_numeric_v_string_like(np.array(["foo"]), np.array([1, 2]))
1322-
True
1323-
>>> is_numeric_v_string_like(np.array([1]), np.array([2]))
1324-
False
1325-
>>> is_numeric_v_string_like(np.array(["foo"]), np.array(["foo"]))
1326-
False
1327-
"""
1328-
1329-
is_a_array = isinstance(a, np.ndarray)
1330-
is_b_array = isinstance(b, np.ndarray)
1331-
1332-
is_a_numeric_array = is_a_array and is_numeric_dtype(a)
1333-
is_b_numeric_array = is_b_array and is_numeric_dtype(b)
1334-
is_a_string_array = is_a_array and is_string_like_dtype(a)
1335-
is_b_string_array = is_b_array and is_string_like_dtype(b)
1336-
1337-
is_a_scalar_string_like = not is_a_array and isinstance(a, str)
1338-
is_b_scalar_string_like = not is_b_array and isinstance(b, str)
1339-
1340-
return (
1341-
(is_a_numeric_array and is_b_scalar_string_like)
1342-
or (is_b_numeric_array and is_a_scalar_string_like)
1343-
or (is_a_numeric_array and is_b_string_array)
1344-
or (is_b_numeric_array and is_a_string_array)
1345-
)
1346-
1347-
1348-
def is_datetimelike_v_numeric(a, b):
1349-
"""
1350-
Check if we are comparing a datetime-like object to a numeric object.
1351-
1352-
By "numeric," we mean an object that is either of an int or float dtype.
1353-
1354-
Parameters
1355-
----------
1356-
a : array-like, scalar
1357-
The first object to check.
1358-
b : array-like, scalar
1359-
The second object to check.
1360-
1361-
Returns
1362-
-------
1363-
boolean
1364-
Whether we return a comparing a datetime-like to a numeric object.
1365-
1366-
Examples
1367-
--------
1368-
>>> dt = np.datetime64(pd.datetime(2017, 1, 1))
1369-
>>>
1370-
>>> is_datetimelike_v_numeric(1, 1)
1371-
False
1372-
>>> is_datetimelike_v_numeric(dt, dt)
1373-
False
1374-
>>> is_datetimelike_v_numeric(1, dt)
1375-
True
1376-
>>> is_datetimelike_v_numeric(dt, 1) # symmetric check
1377-
True
1378-
>>> is_datetimelike_v_numeric(np.array([dt]), 1)
1379-
True
1380-
>>> is_datetimelike_v_numeric(np.array([1]), dt)
1381-
True
1382-
>>> is_datetimelike_v_numeric(np.array([dt]), np.array([1]))
1383-
True
1384-
>>> is_datetimelike_v_numeric(np.array([1]), np.array([2]))
1385-
False
1386-
>>> is_datetimelike_v_numeric(np.array([dt]), np.array([dt]))
1387-
False
1388-
"""
1389-
1390-
if not hasattr(a, "dtype"):
1391-
a = np.asarray(a)
1392-
if not hasattr(b, "dtype"):
1393-
b = np.asarray(b)
1394-
1395-
def is_numeric(x):
1396-
"""
1397-
Check if an object has a numeric dtype (i.e. integer or float).
1398-
"""
1399-
return is_integer_dtype(x) or is_float_dtype(x)
1400-
1401-
return (needs_i8_conversion(a) and is_numeric(b)) or (
1402-
needs_i8_conversion(b) and is_numeric(a)
1403-
)
1404-
1405-
14061288
def needs_i8_conversion(arr_or_dtype):
14071289
"""
14081290
Check whether the array or dtype should be converted to int64.

pandas/core/dtypes/missing.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
is_complex_dtype,
1818
is_datetime64_dtype,
1919
is_datetime64tz_dtype,
20-
is_datetimelike_v_numeric,
2120
is_dtype_equal,
2221
is_extension_array_dtype,
2322
is_float_dtype,
@@ -463,12 +462,8 @@ def array_equivalent(left, right, strict_nan=False):
463462
return True
464463
return ((left == right) | (isna(left) & isna(right))).all()
465464

466-
# numpy will will not allow this type of datetimelike vs integer comparison
467-
elif is_datetimelike_v_numeric(left, right):
468-
return False
469-
470-
# M8/m8
471-
elif needs_i8_conversion(left) and needs_i8_conversion(right):
465+
elif needs_i8_conversion(left) or needs_i8_conversion(right):
466+
# datetime64, timedelta64, Period
472467
if not is_dtype_equal(left.dtype, right.dtype):
473468
return False
474469

pandas/core/internals/managers.py

+1-11
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
)
1919
from pandas.core.dtypes.common import (
2020
_NS_DTYPE,
21-
is_datetimelike_v_numeric,
2221
is_extension_array_dtype,
2322
is_list_like,
24-
is_numeric_v_string_like,
2523
is_scalar,
2624
is_sparse,
2725
)
@@ -1926,15 +1924,7 @@ def _compare_or_regex_search(a, b, regex=False):
19261924
is_a_array = isinstance(a, np.ndarray)
19271925
is_b_array = isinstance(b, np.ndarray)
19281926

1929-
# numpy deprecation warning to have i8 vs integer comparisons
1930-
if is_datetimelike_v_numeric(a, b):
1931-
result = False
1932-
1933-
# numpy deprecation warning if comparing numeric vs string-like
1934-
elif is_numeric_v_string_like(a, b):
1935-
result = False
1936-
else:
1937-
result = op(a)
1927+
result = op(a)
19381928

19391929
if is_scalar(result) and (is_a_array or is_b_array):
19401930
type_names = [type(a).__name__, type(b).__name__]

pandas/core/missing.py

+2-13
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
is_datetime64_dtype,
1313
is_datetime64tz_dtype,
1414
is_integer_dtype,
15-
is_numeric_v_string_like,
1615
is_scalar,
1716
is_timedelta64_dtype,
1817
needs_i8_conversion,
@@ -39,24 +38,14 @@ def mask_missing(arr, values_to_mask):
3938
mask = None
4039
for x in nonna:
4140
if mask is None:
42-
43-
# numpy elementwise comparison warning
44-
if is_numeric_v_string_like(arr, x):
45-
mask = False
46-
else:
47-
mask = arr == x
41+
mask = arr == x
4842

4943
# if x is a string and arr is not, then we get False and we must
5044
# expand the mask to size arr.shape
5145
if is_scalar(mask):
5246
mask = np.zeros(arr.shape, dtype=bool)
5347
else:
54-
55-
# numpy elementwise comparison warning
56-
if is_numeric_v_string_like(arr, x):
57-
mask |= False
58-
else:
59-
mask |= arr == x
48+
mask |= arr == x
6049

6150
if na_mask.any():
6251
if mask is None:

pandas/tests/dtypes/test_common.py

-28
Original file line numberDiff line numberDiff line change
@@ -493,34 +493,6 @@ def test_is_datetime_or_timedelta_dtype():
493493
assert com.is_datetime_or_timedelta_dtype(np.array([], dtype=np.datetime64))
494494

495495

496-
def test_is_numeric_v_string_like():
497-
assert not com.is_numeric_v_string_like(1, 1)
498-
assert not com.is_numeric_v_string_like(1, "foo")
499-
assert not com.is_numeric_v_string_like("foo", "foo")
500-
assert not com.is_numeric_v_string_like(np.array([1]), np.array([2]))
501-
assert not com.is_numeric_v_string_like(np.array(["foo"]), np.array(["foo"]))
502-
503-
assert com.is_numeric_v_string_like(np.array([1]), "foo")
504-
assert com.is_numeric_v_string_like("foo", np.array([1]))
505-
assert com.is_numeric_v_string_like(np.array([1, 2]), np.array(["foo"]))
506-
assert com.is_numeric_v_string_like(np.array(["foo"]), np.array([1, 2]))
507-
508-
509-
def test_is_datetimelike_v_numeric():
510-
dt = np.datetime64(pd.datetime(2017, 1, 1))
511-
512-
assert not com.is_datetimelike_v_numeric(1, 1)
513-
assert not com.is_datetimelike_v_numeric(dt, dt)
514-
assert not com.is_datetimelike_v_numeric(np.array([1]), np.array([2]))
515-
assert not com.is_datetimelike_v_numeric(np.array([dt]), np.array([dt]))
516-
517-
assert com.is_datetimelike_v_numeric(1, dt)
518-
assert com.is_datetimelike_v_numeric(1, dt)
519-
assert com.is_datetimelike_v_numeric(np.array([dt]), 1)
520-
assert com.is_datetimelike_v_numeric(np.array([1]), dt)
521-
assert com.is_datetimelike_v_numeric(np.array([dt]), np.array([1]))
522-
523-
524496
def test_needs_i8_conversion():
525497
assert not com.needs_i8_conversion(str)
526498
assert not com.needs_i8_conversion(np.int64)

0 commit comments

Comments
 (0)