Skip to content

Series.astype(np.integer) doesn't show numpy warning #51917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Any,
Callable,
)
import warnings

import numpy as np

Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)