Skip to content

Commit 2e38d4e

Browse files
TomAugspurgerjreback
authored andcommitted
Revert "CI: workaround numpydev bug (#29433)" (#29553)
1 parent d8c6610 commit 2e38d4e

File tree

6 files changed

+177
-13
lines changed

6 files changed

+177
-13
lines changed

ci/azure/posix.yml

+7-10
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,13 @@ jobs:
4444
PATTERN: "not slow and not network"
4545
LOCALE_OVERRIDE: "zh_CN.UTF-8"
4646

47-
# https://github.com/pandas-dev/pandas/issues/29432
48-
# py37_np_dev:
49-
# ENV_FILE: ci/deps/azure-37-numpydev.yaml
50-
# CONDA_PY: "37"
51-
# PATTERN: "not slow and not network"
52-
# TEST_ARGS: "-W error"
53-
# PANDAS_TESTING_MODE: "deprecate"
54-
# EXTRA_APT: "xsel"
55-
# # TODO:
56-
# continueOnError: true
47+
py37_np_dev:
48+
ENV_FILE: ci/deps/azure-37-numpydev.yaml
49+
CONDA_PY: "37"
50+
PATTERN: "not slow and not network"
51+
TEST_ARGS: "-W error"
52+
PANDAS_TESTING_MODE: "deprecate"
53+
EXTRA_APT: "xsel"
5754

5855
steps:
5956
- script: |

pandas/core/dtypes/common.py

+118
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,124 @@ def _is_unorderable_exception(e: TypeError) -> bool:
11911191
return "'>' not supported between instances of" in str(e)
11921192

11931193

1194+
# This exists to silence numpy deprecation warnings, see GH#29553
1195+
def is_numeric_v_string_like(a, b):
1196+
"""
1197+
Check if we are comparing a string-like object to a numeric ndarray.
1198+
NumPy doesn't like to compare such objects, especially numeric arrays
1199+
and scalar string-likes.
1200+
1201+
Parameters
1202+
----------
1203+
a : array-like, scalar
1204+
The first object to check.
1205+
b : array-like, scalar
1206+
The second object to check.
1207+
1208+
Returns
1209+
-------
1210+
boolean
1211+
Whether we return a comparing a string-like object to a numeric array.
1212+
1213+
Examples
1214+
--------
1215+
>>> is_numeric_v_string_like(1, 1)
1216+
False
1217+
>>> is_numeric_v_string_like("foo", "foo")
1218+
False
1219+
>>> is_numeric_v_string_like(1, "foo") # non-array numeric
1220+
False
1221+
>>> is_numeric_v_string_like(np.array([1]), "foo")
1222+
True
1223+
>>> is_numeric_v_string_like("foo", np.array([1])) # symmetric check
1224+
True
1225+
>>> is_numeric_v_string_like(np.array([1, 2]), np.array(["foo"]))
1226+
True
1227+
>>> is_numeric_v_string_like(np.array(["foo"]), np.array([1, 2]))
1228+
True
1229+
>>> is_numeric_v_string_like(np.array([1]), np.array([2]))
1230+
False
1231+
>>> is_numeric_v_string_like(np.array(["foo"]), np.array(["foo"]))
1232+
False
1233+
"""
1234+
1235+
is_a_array = isinstance(a, np.ndarray)
1236+
is_b_array = isinstance(b, np.ndarray)
1237+
1238+
is_a_numeric_array = is_a_array and is_numeric_dtype(a)
1239+
is_b_numeric_array = is_b_array and is_numeric_dtype(b)
1240+
is_a_string_array = is_a_array and is_string_like_dtype(a)
1241+
is_b_string_array = is_b_array and is_string_like_dtype(b)
1242+
1243+
is_a_scalar_string_like = not is_a_array and isinstance(a, str)
1244+
is_b_scalar_string_like = not is_b_array and isinstance(b, str)
1245+
1246+
return (
1247+
(is_a_numeric_array and is_b_scalar_string_like)
1248+
or (is_b_numeric_array and is_a_scalar_string_like)
1249+
or (is_a_numeric_array and is_b_string_array)
1250+
or (is_b_numeric_array and is_a_string_array)
1251+
)
1252+
1253+
1254+
# This exists to silence numpy deprecation warnings, see GH#29553
1255+
def is_datetimelike_v_numeric(a, b):
1256+
"""
1257+
Check if we are comparing a datetime-like object to a numeric object.
1258+
By "numeric," we mean an object that is either of an int or float dtype.
1259+
1260+
Parameters
1261+
----------
1262+
a : array-like, scalar
1263+
The first object to check.
1264+
b : array-like, scalar
1265+
The second object to check.
1266+
1267+
Returns
1268+
-------
1269+
boolean
1270+
Whether we return a comparing a datetime-like to a numeric object.
1271+
1272+
Examples
1273+
--------
1274+
>>> dt = np.datetime64(pd.datetime(2017, 1, 1))
1275+
>>>
1276+
>>> is_datetimelike_v_numeric(1, 1)
1277+
False
1278+
>>> is_datetimelike_v_numeric(dt, dt)
1279+
False
1280+
>>> is_datetimelike_v_numeric(1, dt)
1281+
True
1282+
>>> is_datetimelike_v_numeric(dt, 1) # symmetric check
1283+
True
1284+
>>> is_datetimelike_v_numeric(np.array([dt]), 1)
1285+
True
1286+
>>> is_datetimelike_v_numeric(np.array([1]), dt)
1287+
True
1288+
>>> is_datetimelike_v_numeric(np.array([dt]), np.array([1]))
1289+
True
1290+
>>> is_datetimelike_v_numeric(np.array([1]), np.array([2]))
1291+
False
1292+
>>> is_datetimelike_v_numeric(np.array([dt]), np.array([dt]))
1293+
False
1294+
"""
1295+
1296+
if not hasattr(a, "dtype"):
1297+
a = np.asarray(a)
1298+
if not hasattr(b, "dtype"):
1299+
b = np.asarray(b)
1300+
1301+
def is_numeric(x):
1302+
"""
1303+
Check if an object has a numeric dtype (i.e. integer or float).
1304+
"""
1305+
return is_integer_dtype(x) or is_float_dtype(x)
1306+
1307+
return (needs_i8_conversion(a) and is_numeric(b)) or (
1308+
needs_i8_conversion(b) and is_numeric(a)
1309+
)
1310+
1311+
11941312
def needs_i8_conversion(arr_or_dtype) -> bool:
11951313
"""
11961314
Check whether the array or dtype should be converted to int64.

pandas/core/dtypes/missing.py

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
is_complex_dtype,
1818
is_datetime64_dtype,
1919
is_datetime64tz_dtype,
20+
is_datetimelike_v_numeric,
2021
is_dtype_equal,
2122
is_extension_array_dtype,
2223
is_float_dtype,
@@ -465,6 +466,10 @@ def array_equivalent(left, right, strict_nan: bool = False) -> bool:
465466
return True
466467
return ((left == right) | (isna(left) & isna(right))).all()
467468

469+
elif is_datetimelike_v_numeric(left, right):
470+
# GH#29553 avoid numpy deprecation warning
471+
return False
472+
468473
elif needs_i8_conversion(left) or needs_i8_conversion(right):
469474
# datetime64, timedelta64, Period
470475
if not is_dtype_equal(left.dtype, right.dtype):

pandas/core/internals/managers.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
)
1919
from pandas.core.dtypes.common import (
2020
_NS_DTYPE,
21+
is_datetimelike_v_numeric,
2122
is_extension_array_dtype,
2223
is_list_like,
24+
is_numeric_v_string_like,
2325
is_scalar,
2426
is_sparse,
2527
)
@@ -1917,7 +1919,11 @@ def _compare_or_regex_search(a, b, regex=False):
19171919
is_a_array = isinstance(a, np.ndarray)
19181920
is_b_array = isinstance(b, np.ndarray)
19191921

1920-
result = op(a)
1922+
if is_datetimelike_v_numeric(a, b) or is_numeric_v_string_like(a, b):
1923+
# GH#29553 avoid deprecation warnings from numpy
1924+
result = False
1925+
else:
1926+
result = op(a)
19211927

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

pandas/core/missing.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Routines for filling missing data.
33
"""
4+
45
import numpy as np
56

67
from pandas._libs import algos, lib
@@ -12,6 +13,7 @@
1213
is_datetime64_dtype,
1314
is_datetime64tz_dtype,
1415
is_integer_dtype,
16+
is_numeric_v_string_like,
1517
is_scalar,
1618
is_timedelta64_dtype,
1719
needs_i8_conversion,
@@ -38,14 +40,22 @@ def mask_missing(arr, values_to_mask):
3840
mask = None
3941
for x in nonna:
4042
if mask is None:
41-
mask = arr == x
43+
if is_numeric_v_string_like(arr, x):
44+
# GH#29553 prevent numpy deprecation warnings
45+
mask = False
46+
else:
47+
mask = arr == x
4248

4349
# if x is a string and arr is not, then we get False and we must
4450
# expand the mask to size arr.shape
4551
if is_scalar(mask):
4652
mask = np.zeros(arr.shape, dtype=bool)
4753
else:
48-
mask |= arr == x
54+
if is_numeric_v_string_like(arr, x):
55+
# GH#29553 prevent numpy deprecation warnings
56+
mask |= False
57+
else:
58+
mask |= arr == x
4959

5060
if na_mask.any():
5161
if mask is None:

pandas/tests/dtypes/test_common.py

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

476476

477+
def test_is_numeric_v_string_like():
478+
assert not com.is_numeric_v_string_like(1, 1)
479+
assert not com.is_numeric_v_string_like(1, "foo")
480+
assert not com.is_numeric_v_string_like("foo", "foo")
481+
assert not com.is_numeric_v_string_like(np.array([1]), np.array([2]))
482+
assert not com.is_numeric_v_string_like(np.array(["foo"]), np.array(["foo"]))
483+
484+
assert com.is_numeric_v_string_like(np.array([1]), "foo")
485+
assert com.is_numeric_v_string_like("foo", np.array([1]))
486+
assert com.is_numeric_v_string_like(np.array([1, 2]), np.array(["foo"]))
487+
assert com.is_numeric_v_string_like(np.array(["foo"]), np.array([1, 2]))
488+
489+
490+
def test_is_datetimelike_v_numeric():
491+
dt = np.datetime64(pd.datetime(2017, 1, 1))
492+
493+
assert not com.is_datetimelike_v_numeric(1, 1)
494+
assert not com.is_datetimelike_v_numeric(dt, dt)
495+
assert not com.is_datetimelike_v_numeric(np.array([1]), np.array([2]))
496+
assert not com.is_datetimelike_v_numeric(np.array([dt]), np.array([dt]))
497+
498+
assert com.is_datetimelike_v_numeric(1, dt)
499+
assert com.is_datetimelike_v_numeric(1, dt)
500+
assert com.is_datetimelike_v_numeric(np.array([dt]), 1)
501+
assert com.is_datetimelike_v_numeric(np.array([1]), dt)
502+
assert com.is_datetimelike_v_numeric(np.array([dt]), np.array([1]))
503+
504+
477505
def test_needs_i8_conversion():
478506
assert not com.needs_i8_conversion(str)
479507
assert not com.needs_i8_conversion(np.int64)

0 commit comments

Comments
 (0)