Skip to content

Commit 59103db

Browse files
committed
WARN: more uncomparables of numeric array vs object
1 parent 68083c8 commit 59103db

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

pandas/core/common.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ def mask_missing(arr, values_to_mask):
446446
if mask is None:
447447

448448
# numpy elementwise comparison warning
449-
if is_numeric_dtype(arr) and is_string_like(x):
449+
if is_numeric_v_string_like(arr, x):
450450
mask = False
451451
else:
452452
mask = arr == x
@@ -458,7 +458,7 @@ def mask_missing(arr, values_to_mask):
458458
else:
459459

460460
# numpy elementwise comparison warning
461-
if is_numeric_dtype(arr) and is_string_like(x):
461+
if is_numeric_v_string_like(arr, x):
462462
mask |= False
463463
else:
464464
mask |= arr == x
@@ -2538,6 +2538,27 @@ def is_datetime_or_timedelta_dtype(arr_or_dtype):
25382538
return issubclass(tipo, (np.datetime64, np.timedelta64))
25392539

25402540

2541+
def is_numeric_v_string_like(a, b):
2542+
"""
2543+
numpy doesn't like to compare numeric arrays vs scalar string-likes
2544+
2545+
return a boolean result if this is the case for a,b or b,a
2546+
2547+
"""
2548+
is_a_array = isinstance(a, np.ndarray)
2549+
is_b_array = isinstance(b, np.ndarray)
2550+
2551+
is_a_numeric_array = is_a_array and is_numeric_dtype(a)
2552+
is_b_numeric_array = is_b_array and is_numeric_dtype(b)
2553+
2554+
is_a_scalar_string_like = not is_a_array and is_string_like(a)
2555+
is_b_scalar_string_like = not is_b_array and is_string_like(b)
2556+
2557+
return (
2558+
is_a_numeric_array and is_b_scalar_string_like) or (
2559+
is_b_numeric_array and is_a_scalar_string_like
2560+
)
2561+
25412562
def is_datetimelike_v_numeric(a, b):
25422563
# return if we have an i8 convertible and numeric comparision
25432564
if not hasattr(a,'dtype'):

pandas/core/internals.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
is_datetime64tz_dtype, is_datetimetz, is_sparse,
1818
array_equivalent, _maybe_convert_string_to_object,
1919
is_categorical, needs_i8_conversion, is_datetimelike_v_numeric,
20-
is_string_like, is_internal_type)
20+
is_numeric_v_string_like, is_internal_type)
2121
from pandas.core.dtypes import DatetimeTZDtype
2222

2323
from pandas.core.index import Index, MultiIndex, _ensure_index
@@ -1087,7 +1087,7 @@ def get_result(other):
10871087
result = not func.__name__ == 'eq'
10881088

10891089
# avoid numpy warning of elementwise comparisons to object
1090-
elif self.is_numeric and is_string_like(other):
1090+
elif is_numeric_v_string_like(values, other):
10911091
result = False
10921092

10931093
else:
@@ -4259,11 +4259,16 @@ def _possibly_compare(a, b, op):
42594259

42604260
# numpy deprecation warning to have i8 vs integer comparisions
42614261
if is_datetimelike_v_numeric(a, b):
4262-
res = False
4262+
result = False
4263+
4264+
# numpy deprecation warning if comparing numeric vs string-like
4265+
elif is_numeric_v_string_like(a, b):
4266+
result = False
4267+
42634268
else:
4264-
res = op(a, b)
4269+
result = op(a, b)
42654270

4266-
if np.isscalar(res) and (is_a_array or is_b_array):
4271+
if lib.isscalar(result) and (is_a_array or is_b_array):
42674272
type_names = [type(a).__name__, type(b).__name__]
42684273

42694274
if is_a_array:
@@ -4273,7 +4278,7 @@ def _possibly_compare(a, b, op):
42734278
type_names[1] = 'ndarray(dtype=%s)' % b.dtype
42744279

42754280
raise TypeError("Cannot compare types %r and %r" % tuple(type_names))
4276-
return res
4281+
return result
42774282

42784283

42794284
def _concat_indexes(indexes):

0 commit comments

Comments
 (0)