From 87159dd5d09ab1d2b2776c00c81f5a94b6702686 Mon Sep 17 00:00:00 2001 From: Daniel Isaac Date: Tue, 14 Mar 2023 07:53:07 +0530 Subject: [PATCH] Backport PR #51917: Series.astype(np.integer) doesn't show numpy warning --- pandas/core/dtypes/common.py | 8 +++++++- pandas/tests/dtypes/test_common.py | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index 3be290ca2dfac..4b33fd8ed412c 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -7,6 +7,7 @@ Any, Callable, ) +import warnings import numpy as np @@ -1678,7 +1679,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)