Skip to content

Commit c809ab4

Browse files
CI/TST: fix tests for compat with numpy 2+ when using legacy promotion
1 parent 7bd594c commit c809ab4

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

pandas/compat/numpy/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""support numpy compatibility across versions"""
22

3+
import os
34
import warnings
45

56
import numpy as np
@@ -13,6 +14,10 @@
1314
np_version_gte1p24p3 = _nlv >= Version("1.24.3")
1415
np_version_gte1p25 = _nlv >= Version("1.25")
1516
np_version_gt2 = _nlv >= Version("2.0.0")
17+
np_version_gt2_not_legacy = (
18+
_nlv >= Version("2.0.0")
19+
and os.environ.get("NPY_PROMOTION_STATE", "weak") != "legacy"
20+
)
1621
is_numpy_dev = _nlv.dev is not None
1722
_min_numpy_ver = "1.23.5"
1823

pandas/tests/dtypes/test_inference.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
missing as libmissing,
3535
ops as libops,
3636
)
37-
from pandas.compat.numpy import np_version_gt2
37+
from pandas.compat.numpy import np_version_gt2_not_legacy
3838

3939
from pandas.core.dtypes import inference
4040
from pandas.core.dtypes.cast import find_result_type
@@ -1989,7 +1989,7 @@ def test_ensure_int32():
19891989
# find a smaller floating dtype
19901990
(300.0, np.uint16), # for integer floats, we convert them to ints
19911991
(300.1, np.float64),
1992-
(np.int16(300), np.int16 if np_version_gt2 else np.uint16),
1992+
(np.int16(300), np.int16 if np_version_gt2_not_legacy else np.uint16),
19931993
],
19941994
)
19951995
def test_find_result_type_uint_int(right, result):

pandas/tests/indexing/test_coercion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
IS64,
1414
is_platform_windows,
1515
)
16-
from pandas.compat.numpy import np_version_gt2
16+
from pandas.compat.numpy import np_version_gt2_not_legacy
1717

1818
import pandas as pd
1919
import pandas._testing as tm
@@ -231,7 +231,7 @@ def test_insert_float_index(
231231
obj = pd.Index([1.0, 2.0, 3.0, 4.0], dtype=dtype)
232232
coerced_dtype = coerced_dtype if coerced_dtype is not None else dtype
233233

234-
if np_version_gt2 and dtype == "float32" and coerced_val == 1.1:
234+
if np_version_gt2_not_legacy and dtype == "float32" and coerced_val == 1.1:
235235
# Hack, in the 2nd test case, since 1.1 can be losslessly cast to float32
236236
# the expected dtype will be float32 if the original dtype was float32
237237
coerced_dtype = np.float32

pandas/tests/series/test_constructors.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
lib,
1616
)
1717
from pandas.compat import HAS_PYARROW
18-
from pandas.compat.numpy import np_version_gt2
18+
from pandas.compat.numpy import (
19+
np_version_gt2,
20+
np_version_gt2_not_legacy,
21+
)
1922
from pandas.errors import IntCastingNaNError
2023

2124
from pandas.core.dtypes.dtypes import CategoricalDtype
@@ -770,7 +773,7 @@ def test_constructor_cast(self):
770773

771774
def test_constructor_signed_int_overflow_raises(self):
772775
# GH#41734 disallow silent overflow, enforced in 2.0
773-
if np_version_gt2:
776+
if np_version_gt2_not_legacy:
774777
msg = "The elements provided in the data cannot all be casted to the dtype"
775778
err = OverflowError
776779
else:
@@ -803,7 +806,7 @@ def test_constructor_numpy_uints(self, values):
803806

804807
def test_constructor_unsigned_dtype_overflow(self, any_unsigned_int_numpy_dtype):
805808
# see gh-15832
806-
if np_version_gt2:
809+
if np_version_gt2_not_legacy:
807810
msg = (
808811
f"The elements provided in the data cannot "
809812
f"all be casted to the dtype {any_unsigned_int_numpy_dtype}"
@@ -1939,7 +1942,7 @@ def test_constructor_int64_dtype(self, any_int_dtype):
19391942

19401943
def test_constructor_raise_on_lossy_conversion_of_strings(self):
19411944
# GH#44923
1942-
if not np_version_gt2:
1945+
if not np_version_gt2_not_legacy:
19431946
raises = pytest.raises(
19441947
ValueError, match="string values cannot be losslessly cast to int8"
19451948
)

0 commit comments

Comments
 (0)