Skip to content

Commit db8e3e8

Browse files
Backport PR #55369 on branch 2.1.x (MAINT: Remove np.int_ and np.uint) (#55387)
Backport PR #55369: MAINT: Remove `np.int_` and `np.uint` Co-authored-by: Mateusz Sokół <[email protected]>
1 parent f4359ec commit db8e3e8

31 files changed

+81
-49
lines changed

doc/source/user_guide/enhancingperf.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ can be improved by passing an ``np.ndarray``.
184184
...: cpdef np.ndarray[double] apply_integrate_f(np.ndarray col_a, np.ndarray col_b,
185185
...: np.ndarray col_N):
186186
...: assert (col_a.dtype == np.float64
187-
...: and col_b.dtype == np.float64 and col_N.dtype == np.int_)
187+
...: and col_b.dtype == np.float64 and col_N.dtype == np.dtype(int))
188188
...: cdef Py_ssize_t i, n = len(col_N)
189189
...: assert (len(col_a) == len(col_b) == n)
190190
...: cdef np.ndarray[double] res = np.empty(n)

pandas/compat/numpy/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@
2121
)
2222

2323

24+
np_long: type
25+
np_ulong: type
26+
27+
if _nlv >= Version("2.0.0.dev0"):
28+
try:
29+
np_long = np.long # type: ignore[attr-defined]
30+
np_ulong = np.ulong # type: ignore[attr-defined]
31+
except AttributeError:
32+
np_long = np.int_
33+
np_ulong = np.uint
34+
else:
35+
np_long = np.int_
36+
np_ulong = np.uint
37+
38+
2439
__all__ = [
2540
"np",
2641
"_np_version",

pandas/core/algorithms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,7 @@ def safe_sort(
16411641
else:
16421642
mask = None
16431643
else:
1644-
reverse_indexer = np.empty(len(sorter), dtype=np.int_)
1644+
reverse_indexer = np.empty(len(sorter), dtype=int)
16451645
reverse_indexer.put(sorter, np.arange(len(sorter)))
16461646
# Out of bound indices will be masked with `-1` next, so we
16471647
# may deal with them here without performance loss using `mode='wrap'`

pandas/tests/arrays/boolean/test_reduction.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import numpy as np
22
import pytest
33

4+
from pandas.compat.numpy import np_long
5+
46
import pandas as pd
57

68

@@ -51,7 +53,7 @@ def test_reductions_return_types(dropna, data, all_numeric_reductions):
5153
s = s.dropna()
5254

5355
if op in ("sum", "prod"):
54-
assert isinstance(getattr(s, op)(), np.int_)
56+
assert isinstance(getattr(s, op)(), np_long)
5557
elif op == "count":
5658
# Oddly on the 32 bit build (but not Windows), this is intc (!= intp)
5759
assert isinstance(getattr(s, op)(), np.integer)

pandas/tests/dtypes/cast/test_infer_dtype.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def test_infer_dtype_from_scalar(value, expected):
170170
@pytest.mark.parametrize(
171171
"arr, expected",
172172
[
173-
([1], np.int_),
173+
([1], np.dtype(int)),
174174
(np.array([1], dtype=np.int64), np.int64),
175175
([np.nan, 1, ""], np.object_),
176176
(np.array([[1.0, 2.0]]), np.float64),

pandas/tests/extension/base/dim2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def get_reduction_result_dtype(dtype):
237237
return NUMPY_INT_TO_DTYPE[np.dtype(int)]
238238
else:
239239
# i.e. dtype.kind == "u"
240-
return NUMPY_INT_TO_DTYPE[np.dtype(np.uint)]
240+
return NUMPY_INT_TO_DTYPE[np.dtype("uint")]
241241

242242
if method in ["sum", "prod"]:
243243
# std and var are not dtype-preserving

pandas/tests/frame/indexing/test_indexing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ def test_setitem_list(self, float_frame):
108108
data["A"] = newcolumndata
109109

110110
def test_setitem_list2(self):
111-
df = DataFrame(0, index=range(3), columns=["tt1", "tt2"], dtype=np.int_)
111+
df = DataFrame(0, index=range(3), columns=["tt1", "tt2"], dtype=int)
112112
df.loc[1, ["tt1", "tt2"]] = [1, 2]
113113

114114
result = df.loc[df.index[1], ["tt1", "tt2"]]
115-
expected = Series([1, 2], df.columns, dtype=np.int_, name=1)
115+
expected = Series([1, 2], df.columns, dtype=int, name=1)
116116
tm.assert_series_equal(result, expected)
117117

118118
df["tt1"] = df["tt2"] = "0"

pandas/tests/frame/methods/test_shift.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
import pytest
33

4+
from pandas.compat.numpy import np_long
45
import pandas.util._test_decorators as td
56

67
import pandas as pd
@@ -471,22 +472,22 @@ def test_shift_axis1_multiple_blocks_with_int_fill(self):
471472
df1 = DataFrame(rng.integers(1000, size=(5, 3), dtype=int))
472473
df2 = DataFrame(rng.integers(1000, size=(5, 2), dtype=int))
473474
df3 = pd.concat([df1.iloc[:4, 1:3], df2.iloc[:4, :]], axis=1)
474-
result = df3.shift(2, axis=1, fill_value=np.int_(0))
475+
result = df3.shift(2, axis=1, fill_value=np_long(0))
475476
assert len(df3._mgr.blocks) == 2
476477

477478
expected = df3.take([-1, -1, 0, 1], axis=1)
478-
expected.iloc[:, :2] = np.int_(0)
479+
expected.iloc[:, :2] = np_long(0)
479480
expected.columns = df3.columns
480481

481482
tm.assert_frame_equal(result, expected)
482483

483484
# Case with periods < 0
484485
df3 = pd.concat([df1.iloc[:4, 1:3], df2.iloc[:4, :]], axis=1)
485-
result = df3.shift(-2, axis=1, fill_value=np.int_(0))
486+
result = df3.shift(-2, axis=1, fill_value=np_long(0))
486487
assert len(df3._mgr.blocks) == 2
487488

488489
expected = df3.take([2, 3, -1, -1], axis=1)
489-
expected.iloc[:, -2:] = np.int_(0)
490+
expected.iloc[:, -2:] = np_long(0)
490491
expected.columns = df3.columns
491492

492493
tm.assert_frame_equal(result, expected)

pandas/tests/frame/test_constructors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1823,7 +1823,7 @@ def test_constructor_single_value(self):
18231823
DataFrame("a", [1, 2], ["a", "c"], float)
18241824

18251825
def test_constructor_with_datetimes(self):
1826-
intname = np.dtype(np.int_).name
1826+
intname = np.dtype(int).name
18271827
floatname = np.dtype(np.float64).name
18281828
objectname = np.dtype(np.object_).name
18291829

pandas/tests/frame/test_reductions.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
IS64,
1111
is_platform_windows,
1212
)
13+
from pandas.compat.numpy import (
14+
np_long,
15+
np_ulong,
16+
)
1317
import pandas.util._test_decorators as td
1418

1519
import pandas as pd
@@ -1712,11 +1716,11 @@ class TestEmptyDataFrameReductions:
17121716
"opname, dtype, exp_value, exp_dtype",
17131717
[
17141718
("sum", np.int8, 0, np.int64),
1715-
("prod", np.int8, 1, np.int_),
1719+
("prod", np.int8, 1, np_long),
17161720
("sum", np.int64, 0, np.int64),
17171721
("prod", np.int64, 1, np.int64),
17181722
("sum", np.uint8, 0, np.uint64),
1719-
("prod", np.uint8, 1, np.uint),
1723+
("prod", np.uint8, 1, np_ulong),
17201724
("sum", np.uint64, 0, np.uint64),
17211725
("prod", np.uint64, 1, np.uint64),
17221726
("sum", np.float32, 0, np.float32),

0 commit comments

Comments
 (0)