diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index 6802cf096e868..23bc0e6280e27 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -8,6 +8,7 @@ Any, Callable, ) +import warnings import numpy as np @@ -1694,7 +1695,12 @@ def pandas_dtype(dtype) -> DtypeObj: # try a numpy dtype # raise a consistent TypeError if failed try: - npdtype = np.dtype(dtype) + with warnings.catch_warnings(): + # GH#51523 - Series.astype(np.integer) doesn't show + # numpy deprication warning of np.integer + # Hence enabling DeprecationWarning + warnings.simplefilter("always", DeprecationWarning) + npdtype = np.dtype(dtype) except SyntaxError as err: # np.dtype uses `eval` which can raise SyntaxError raise TypeError(f"data type '{dtype}' not understood") from err diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py index 638cfa9d82bc2..9c11bff8862c1 100644 --- a/pandas/tests/dtypes/test_common.py +++ b/pandas/tests/dtypes/test_common.py @@ -754,3 +754,13 @@ def test_validate_allhashable(): with pytest.raises(TypeError, match="list must be a hashable type"): com.validate_all_hashable([], error_name="list") + + +def test_pandas_dtype_numpy_warning(): + # GH#51523 + with tm.assert_produces_warning( + DeprecationWarning, + check_stacklevel=False, + match="Converting `np.integer` or `np.signedinteger` to a dtype is deprecated", + ): + pandas_dtype(np.integer)