Skip to content

Commit dccf5eb

Browse files
committed
DEPR: remove numpy deprecation warnings for i8 vs integer comparisions
1 parent 4fe7c68 commit dccf5eb

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

pandas/core/common.py

+11
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,10 @@ def array_equivalent(left, right, strict_nan=False):
462462
if issubclass(left.dtype.type, (np.floating, np.complexfloating)):
463463
return ((left == right) | (np.isnan(left) & np.isnan(right))).all()
464464

465+
# numpy will will not allow this type of datetimelike vs integer comparison
466+
elif is_datetimelike_v_integer(left, right):
467+
return False
468+
465469
# NaNs cannot occur otherwise.
466470
return np.array_equal(left, right)
467471

@@ -2539,6 +2543,13 @@ def is_datetime_or_timedelta_dtype(arr_or_dtype):
25392543
return issubclass(tipo, (np.datetime64, np.timedelta64))
25402544

25412545

2546+
def is_datetimelike_v_integer(a, b):
2547+
# return if we have an i8 convertible and and integer comparision
2548+
a = np.asarray(a)
2549+
b = np.asarray(b)
2550+
return (needs_i8_conversion(a) and is_integer_dtype(b)) or (
2551+
needs_i8_conversion(b) and is_integer_dtype(a))
2552+
25422553
needs_i8_conversion = is_datetime_or_timedelta_dtype
25432554

25442555
def i8_boxer(arr_or_dtype):

pandas/core/generic.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -3574,7 +3574,14 @@ def where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
35743574
except ValueError:
35753575
new_other = np.array(other)
35763576

3577-
matches = (new_other == np.array(other))
3577+
# we can end up comparing integers and m8[ns]
3578+
# which is a numpy no no
3579+
is_i8 = com.needs_i8_conversion(self.dtype)
3580+
if is_i8:
3581+
matches = False
3582+
else:
3583+
matches = (new_other == np.array(other))
3584+
35783585
if matches is False or not matches.all():
35793586

35803587
# coerce other to a common dtype if we can

pandas/core/internals.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
is_null_datelike_scalar, _maybe_promote,
1515
is_timedelta64_dtype, is_datetime64_dtype,
1616
array_equivalent, _maybe_convert_string_to_object,
17-
is_categorical)
17+
is_categorical, needs_i8_conversion, is_datetimelike_v_integer)
1818
from pandas.core.index import Index, MultiIndex, _ensure_index
1919
from pandas.core.indexing import maybe_convert_indices, length_of_indexer
2020
from pandas.core.categorical import Categorical, maybe_to_categorical
@@ -3885,9 +3885,16 @@ def _vstack(to_stack, dtype):
38853885

38863886

38873887
def _possibly_compare(a, b, op):
3888-
res = op(a, b)
3888+
38893889
is_a_array = isinstance(a, np.ndarray)
38903890
is_b_array = isinstance(b, np.ndarray)
3891+
3892+
# numpy deprecation warning to have i8 vs integer comparisions
3893+
if is_datetimelike_v_integer(a, b):
3894+
res = False
3895+
else:
3896+
res = op(a, b)
3897+
38913898
if np.isscalar(res) and (is_a_array or is_b_array):
38923899
type_names = [type(a).__name__, type(b).__name__]
38933900

pandas/core/ops.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from pandas.tslib import iNaT
1818
from pandas.core.common import(bind_method, is_list_like, notnull, isnull,
1919
_values_from_object, _maybe_match_name,
20-
needs_i8_conversion, is_integer_dtype)
20+
needs_i8_conversion, is_datetimelike_v_integer, is_integer_dtype)
2121

2222
# -----------------------------------------------------------------------------
2323
# Functions that add arithmetic methods to objects, given arithmetic factory
@@ -574,9 +574,7 @@ def na_op(x, y):
574574
# we are not NotImplemented, otherwise
575575
# we would allow datetime64 (but viewed as i8) against
576576
# integer comparisons
577-
if needs_i8_conversion(x) and (not isscalar(y) and is_integer_dtype(y)):
578-
raise TypeError("invalid type comparison")
579-
elif (not isscalar(y) and needs_i8_conversion(y)) and is_integer_dtype(x):
577+
if is_datetimelike_v_integer(x, y):
580578
raise TypeError("invalid type comparison")
581579

582580
# we have a datetime/timedelta and may need to convert
@@ -690,15 +688,15 @@ def na_op(x, y):
690688
return result
691689

692690
def wrapper(self, other):
693-
is_self_int_dtype = com.is_integer_dtype(self.dtype)
691+
is_self_int_dtype = is_integer_dtype(self.dtype)
694692

695693
fill_int = lambda x: x.fillna(0)
696694
fill_bool = lambda x: x.fillna(False).astype(bool)
697695

698696
if isinstance(other, pd.Series):
699697
name = _maybe_match_name(self, other)
700698
other = other.reindex_like(self)
701-
is_other_int_dtype = com.is_integer_dtype(other.dtype)
699+
is_other_int_dtype = is_integer_dtype(other.dtype)
702700
other = fill_int(other) if is_other_int_dtype else fill_bool(other)
703701

704702
filler = fill_int if is_self_int_dtype and is_other_int_dtype else fill_bool
@@ -711,7 +709,7 @@ def wrapper(self, other):
711709

712710
else:
713711
# scalars, list, tuple, np.array
714-
filler = fill_int if is_self_int_dtype and com.is_integer_dtype(np.asarray(other)) else fill_bool
712+
filler = fill_int if is_self_int_dtype and is_integer_dtype(np.asarray(other)) else fill_bool
715713
return filler(self._constructor(na_op(self.values, other),
716714
index=self.index)).__finalize__(self)
717715

0 commit comments

Comments
 (0)