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 1 commit
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
11 changes: 11 additions & 0 deletions pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import warnings

import numpy as np
import pytest

Expand Down Expand Up @@ -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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you do tm.assert_produces_warning() instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure thing, I've updated the testcase.

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