Skip to content

Commit 6da8258

Browse files
Backport PR #51917 on branch 2.0.x (Series.astype(np.integer) doesn't show numpy warning) (#51958)
Backport PR #51917: Series.astype(np.integer) doesn't show numpy warning Co-authored-by: Daniel Isaac <[email protected]>
1 parent 5cf184e commit 6da8258

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

pandas/core/dtypes/common.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Any,
88
Callable,
99
)
10+
import warnings
1011

1112
import numpy as np
1213

@@ -1678,7 +1679,12 @@ def pandas_dtype(dtype) -> DtypeObj:
16781679
# try a numpy dtype
16791680
# raise a consistent TypeError if failed
16801681
try:
1681-
npdtype = np.dtype(dtype)
1682+
with warnings.catch_warnings():
1683+
# GH#51523 - Series.astype(np.integer) doesn't show
1684+
# numpy deprication warning of np.integer
1685+
# Hence enabling DeprecationWarning
1686+
warnings.simplefilter("always", DeprecationWarning)
1687+
npdtype = np.dtype(dtype)
16821688
except SyntaxError as err:
16831689
# np.dtype uses `eval` which can raise SyntaxError
16841690
raise TypeError(f"data type '{dtype}' not understood") from err

pandas/tests/dtypes/test_common.py

+10
Original file line numberDiff line numberDiff line change
@@ -754,3 +754,13 @@ def test_validate_allhashable():
754754

755755
with pytest.raises(TypeError, match="list must be a hashable type"):
756756
com.validate_all_hashable([], error_name="list")
757+
758+
759+
def test_pandas_dtype_numpy_warning():
760+
# GH#51523
761+
with tm.assert_produces_warning(
762+
DeprecationWarning,
763+
check_stacklevel=False,
764+
match="Converting `np.integer` or `np.signedinteger` to a dtype is deprecated",
765+
):
766+
pandas_dtype(np.integer)

0 commit comments

Comments
 (0)