From 06ae7f2a641228ba352507010683b6b582598469 Mon Sep 17 00:00:00 2001 From: Daniel Isaac Date: Sun, 12 Mar 2023 22:03:24 +0530 Subject: [PATCH 1/2] Series.astype(np.integer) doesn't show numpy warning --- pandas/core/dtypes/common.py | 8 +++++++- pandas/tests/dtypes/test_common.py | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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..b26cb0503f308 100644 --- a/pandas/tests/dtypes/test_common.py +++ b/pandas/tests/dtypes/test_common.py @@ -1,5 +1,7 @@ from __future__ import annotations +import warnings + import numpy as np import pytest @@ -754,3 +756,12 @@ 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 warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + pandas_dtype(np.integer) + assert len(w) == 1 # should have raised one warning + assert "DeprecationWarning" in str(w[-1]) # we get the default message From c73e65ea2c7f3ecc08ce19aec95c1df63e43a199 Mon Sep 17 00:00:00 2001 From: Daniel Isaac Date: Mon, 13 Mar 2023 23:59:18 +0530 Subject: [PATCH 2/2] updated testcases using tm.assert_produces_warning --- pandas/tests/dtypes/test_common.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py index b26cb0503f308..9c11bff8862c1 100644 --- a/pandas/tests/dtypes/test_common.py +++ b/pandas/tests/dtypes/test_common.py @@ -1,7 +1,5 @@ from __future__ import annotations -import warnings - import numpy as np import pytest @@ -760,8 +758,9 @@ def test_validate_allhashable(): def test_pandas_dtype_numpy_warning(): # GH#51523 - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always") + 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) - assert len(w) == 1 # should have raised one warning - assert "DeprecationWarning" in str(w[-1]) # we get the default message