Skip to content

Commit 4976b69

Browse files
authored
MAINT: Remove np.int_ and np.uint (pandas-dev#55369)
MAINT: Remove np.int_ and np.uint
1 parent 2245217 commit 4976b69

31 files changed

+81
-49
lines changed

doc/source/user_guide/enhancingperf.rst

+1-1
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

+15
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1636,7 +1636,7 @@ def safe_sort(
16361636
else:
16371637
mask = None
16381638
else:
1639-
reverse_indexer = np.empty(len(sorter), dtype=np.int_)
1639+
reverse_indexer = np.empty(len(sorter), dtype=int)
16401640
reverse_indexer.put(sorter, np.arange(len(sorter)))
16411641
# Out of bound indices will be masked with `-1` next, so we
16421642
# may deal with them here without performance loss using `mode='wrap'`

pandas/tests/arrays/boolean/test_reduction.py

+3-1
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

+1-1
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def get_reduction_result_dtype(dtype):
248248
return NUMPY_INT_TO_DTYPE[np.dtype(int)]
249249
else:
250250
# i.e. dtype.kind == "u"
251-
return NUMPY_INT_TO_DTYPE[np.dtype(np.uint)]
251+
return NUMPY_INT_TO_DTYPE[np.dtype("uint")]
252252

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

pandas/tests/frame/indexing/test_indexing.py

+2-2
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

+5-4
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

+1-1
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

+6-2
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
@@ -1713,11 +1717,11 @@ class TestEmptyDataFrameReductions:
17131717
"opname, dtype, exp_value, exp_dtype",
17141718
[
17151719
("sum", np.int8, 0, np.int64),
1716-
("prod", np.int8, 1, np.int_),
1720+
("prod", np.int8, 1, np_long),
17171721
("sum", np.int64, 0, np.int64),
17181722
("prod", np.int64, 1, np.int64),
17191723
("sum", np.uint8, 0, np.uint64),
1720-
("prod", np.uint8, 1, np.uint),
1724+
("prod", np.uint8, 1, np_ulong),
17211725
("sum", np.uint64, 0, np.uint64),
17221726
("prod", np.uint64, 1, np.uint64),
17231727
("sum", np.float32, 0, np.float32),

pandas/tests/groupby/aggregate/test_cython.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def test_cython_agg_empty_buckets_nanops(observed):
229229
# GH-18869 can't call nanops on empty groups, so hardcode expected
230230
# for these
231231
df = DataFrame([11, 12, 13], columns=["a"])
232-
grps = np.arange(0, 25, 5, dtype=np.int_)
232+
grps = np.arange(0, 25, 5, dtype=int)
233233
# add / sum
234234
result = df.groupby(pd.cut(df["a"], grps), observed=observed)._cython_agg_general(
235235
"sum", alt=None, numeric_only=True

pandas/tests/groupby/test_function.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ def test_nlargest_and_smallest_noop(data, groups, dtype, method):
824824
data = list(reversed(data))
825825
ser = Series(data, name="a")
826826
result = getattr(ser.groupby(groups), method)(n=2)
827-
expidx = np.array(groups, dtype=np.int_) if isinstance(groups, list) else groups
827+
expidx = np.array(groups, dtype=int) if isinstance(groups, list) else groups
828828
expected = Series(data, index=MultiIndex.from_arrays([expidx, ser.index]), name="a")
829829
tm.assert_series_equal(result, expected)
830830

pandas/tests/groupby/test_groupby.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3004,12 +3004,12 @@ def test_groupby_reduce_period():
30043004

30053005
res = gb.max()
30063006
expected = ser[-10:]
3007-
expected.index = Index(range(10), dtype=np.int_)
3007+
expected.index = Index(range(10), dtype=int)
30083008
tm.assert_series_equal(res, expected)
30093009

30103010
res = gb.min()
30113011
expected = ser[:10]
3012-
expected.index = Index(range(10), dtype=np.int_)
3012+
expected.index = Index(range(10), dtype=int)
30133013
tm.assert_series_equal(res, expected)
30143014

30153015

pandas/tests/groupby/test_min_max.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def test_max_inat_not_all_na():
108108

109109
# Note: in converting to float64, the iNaT + 1 maps to iNaT, i.e. is lossy
110110
expected = Series({1: np.nan, 2: np.nan, 3: iNaT + 1})
111-
expected.index = expected.index.astype(np.int_)
111+
expected.index = expected.index.astype(int)
112112
tm.assert_series_equal(result, expected, check_exact=True)
113113

114114

pandas/tests/groupby/test_quantile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ def test_groupby_quantile_dt64tz_period():
468468
# Check that we match the group-by-group result
469469
exp = {i: df.iloc[i::5].quantile(0.5) for i in range(5)}
470470
expected = DataFrame(exp).T.infer_objects()
471-
expected.index = expected.index.astype(np.int_)
471+
expected.index = expected.index.astype(int)
472472

473473
tm.assert_frame_equal(result, expected)
474474

pandas/tests/groupby/test_size.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_size_axis_1(df, axis_1, by, sort, dropna):
3939
if sort:
4040
expected = expected.sort_index()
4141
if is_integer_dtype(expected.index.dtype) and not any(x is None for x in by):
42-
expected.index = expected.index.astype(np.int_)
42+
expected.index = expected.index.astype(int)
4343

4444
msg = "DataFrame.groupby with axis=1 is deprecated"
4545
with tm.assert_produces_warning(FutureWarning, match=msg):

pandas/tests/groupby/test_value_counts.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ def test_mixed_groupings(normalize, expected_label, expected_values):
996996
result = gp.value_counts(sort=True, normalize=normalize)
997997
expected = DataFrame(
998998
{
999-
"level_0": np.array([4, 4, 5], dtype=np.int_),
999+
"level_0": np.array([4, 4, 5], dtype=int),
10001000
"A": [1, 1, 2],
10011001
"level_2": [8, 8, 7],
10021002
"B": [1, 3, 2],

pandas/tests/indexes/datetimes/test_datetime.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import numpy as np
55
import pytest
66

7+
from pandas.compat.numpy import np_long
8+
79
import pandas as pd
810
from pandas import (
911
DataFrame,
@@ -54,7 +56,7 @@ def test_time_overflow_for_32bit_machines(self):
5456
# (which has value 1e9) and since the max value for np.int32 is ~2e9,
5557
# and since those machines won't promote np.int32 to np.int64, we get
5658
# overflow.
57-
periods = np.int_(1000)
59+
periods = np_long(1000)
5860

5961
idx1 = date_range(start="2000", periods=periods, freq="s")
6062
assert len(idx1) == periods

pandas/tests/indexes/datetimes/test_indexing.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import numpy as np
99
import pytest
1010

11+
from pandas.compat.numpy import np_long
12+
1113
import pandas as pd
1214
from pandas import (
1315
DatetimeIndex,
@@ -91,7 +93,7 @@ def test_dti_business_getitem(self, freq):
9193
assert fancy_indexed.freq is None
9294

9395
# 32-bit vs. 64-bit platforms
94-
assert rng[4] == rng[np.int_(4)]
96+
assert rng[4] == rng[np_long(4)]
9597

9698
@pytest.mark.parametrize("freq", ["B", "C"])
9799
def test_dti_business_getitem_matplotlib_hackaround(self, freq):

pandas/tests/indexes/test_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ def test_fancy(self, simple_index):
457457
["string", "int64", "int32", "uint64", "uint32", "float64", "float32"],
458458
indirect=True,
459459
)
460-
@pytest.mark.parametrize("dtype", [np.int_, np.bool_])
460+
@pytest.mark.parametrize("dtype", [int, np.bool_])
461461
def test_empty_fancy(self, index, dtype):
462462
empty_arr = np.array([], dtype=dtype)
463463
empty_index = type(index)([], dtype=index.dtype)

pandas/tests/indexing/multiindex/test_partial.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def test_getitem_intkey_leading_level(
166166
mi = ser.index
167167
assert isinstance(mi, MultiIndex)
168168
if dtype is int:
169-
assert mi.levels[0].dtype == np.int_
169+
assert mi.levels[0].dtype == np.dtype(int)
170170
else:
171171
assert mi.levels[0].dtype == np.float64
172172

pandas/tests/indexing/test_loc.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ def test_loc_to_fail(self):
442442
)
443443

444444
msg = (
445-
rf"\"None of \[Index\(\[1, 2\], dtype='{np.int_().dtype}'\)\] are "
445+
rf"\"None of \[Index\(\[1, 2\], dtype='{np.dtype(int)}'\)\] are "
446446
r"in the \[index\]\""
447447
)
448448
with pytest.raises(KeyError, match=msg):
@@ -460,7 +460,7 @@ def test_loc_to_fail2(self):
460460
s.loc[-1]
461461

462462
msg = (
463-
rf"\"None of \[Index\(\[-1, -2\], dtype='{np.int_().dtype}'\)\] are "
463+
rf"\"None of \[Index\(\[-1, -2\], dtype='{np.dtype(int)}'\)\] are "
464464
r"in the \[index\]\""
465465
)
466466
with pytest.raises(KeyError, match=msg):
@@ -476,7 +476,7 @@ def test_loc_to_fail2(self):
476476

477477
s["a"] = 2
478478
msg = (
479-
rf"\"None of \[Index\(\[-2\], dtype='{np.int_().dtype}'\)\] are "
479+
rf"\"None of \[Index\(\[-2\], dtype='{np.dtype(int)}'\)\] are "
480480
r"in the \[index\]\""
481481
)
482482
with pytest.raises(KeyError, match=msg):
@@ -493,7 +493,7 @@ def test_loc_to_fail3(self):
493493
df = DataFrame([["a"], ["b"]], index=[1, 2], columns=["value"])
494494

495495
msg = (
496-
rf"\"None of \[Index\(\[3\], dtype='{np.int_().dtype}'\)\] are "
496+
rf"\"None of \[Index\(\[3\], dtype='{np.dtype(int)}'\)\] are "
497497
r"in the \[index\]\""
498498
)
499499
with pytest.raises(KeyError, match=msg):
@@ -510,7 +510,7 @@ def test_loc_getitem_list_with_fail(self):
510510

511511
s.loc[[2]]
512512

513-
msg = f"\"None of [Index([3], dtype='{np.int_().dtype}')] are in the [index]"
513+
msg = f"\"None of [Index([3], dtype='{np.dtype(int)}')] are in the [index]"
514514
with pytest.raises(KeyError, match=re.escape(msg)):
515515
s.loc[[3]]
516516

@@ -1209,7 +1209,7 @@ def test_loc_setitem_empty_append_raises(self):
12091209
df = DataFrame(columns=["x", "y"])
12101210
df.index = df.index.astype(np.int64)
12111211
msg = (
1212-
rf"None of \[Index\(\[0, 1\], dtype='{np.int_().dtype}'\)\] "
1212+
rf"None of \[Index\(\[0, 1\], dtype='{np.dtype(int)}'\)\] "
12131213
r"are in the \[index\]"
12141214
)
12151215
with pytest.raises(KeyError, match=msg):

pandas/tests/indexing/test_partial.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ def test_series_partial_set(self):
402402

403403
# raises as nothing is in the index
404404
msg = (
405-
rf"\"None of \[Index\(\[3, 3, 3\], dtype='{np.int_().dtype}'\)\] "
405+
rf"\"None of \[Index\(\[3, 3, 3\], dtype='{np.dtype(int)}'\)\] "
406406
r"are in the \[index\]\""
407407
)
408408
with pytest.raises(KeyError, match=msg):
@@ -483,7 +483,7 @@ def test_series_partial_set_with_name(self):
483483

484484
# raises as nothing is in the index
485485
msg = (
486-
rf"\"None of \[Index\(\[3, 3, 3\], dtype='{np.int_().dtype}', "
486+
rf"\"None of \[Index\(\[3, 3, 3\], dtype='{np.dtype(int)}', "
487487
r"name='idx'\)\] are in the \[index\]\""
488488
)
489489
with pytest.raises(KeyError, match=msg):

pandas/tests/internals/test_internals.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ def test_set_change_dtype(self, mgr):
468468
np.random.default_rng(2).standard_normal(N).astype(int),
469469
)
470470
idx = mgr2.items.get_loc("quux")
471-
assert mgr2.iget(idx).dtype == np.int_
471+
assert mgr2.iget(idx).dtype == np.dtype(int)
472472

473473
mgr2.iset(
474474
mgr2.items.get_loc("quux"), np.random.default_rng(2).standard_normal(N)

pandas/tests/io/parser/test_parse_dates.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_read_csv_with_custom_date_parser(all_parsers):
4747
# GH36111
4848
def __custom_date_parser(time):
4949
time = time.astype(np.float64)
50-
time = time.astype(np.int_) # convert float seconds to int type
50+
time = time.astype(int) # convert float seconds to int type
5151
return pd.to_timedelta(time, unit="s")
5252

5353
testdata = StringIO(
@@ -87,7 +87,7 @@ def test_read_csv_with_custom_date_parser_parse_dates_false(all_parsers):
8787
# GH44366
8888
def __custom_date_parser(time):
8989
time = time.astype(np.float64)
90-
time = time.astype(np.int_) # convert float seconds to int type
90+
time = time.astype(int) # convert float seconds to int type
9191
return pd.to_timedelta(time, unit="s")
9292

9393
testdata = StringIO(

0 commit comments

Comments
 (0)