Skip to content

ENH: Update numpy exceptions imports #54405

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
Aug 7, 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
5 changes: 4 additions & 1 deletion pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
)
from pandas.core.dtypes.inference import iterable_not_string

from pandas.util.version import Version

if TYPE_CHECKING:
from pandas._typing import (
AnyArrayLike,
Expand Down Expand Up @@ -236,7 +238,8 @@ def asarray_tuplesafe(values: Iterable, dtype: NpDtype | None = None) -> ArrayLi
try:
with warnings.catch_warnings():
# Can remove warning filter once NumPy 1.24 is min version
warnings.simplefilter("ignore", np.VisibleDeprecationWarning)
if Version(np.__version__) < Version("1.24.0"):
Copy link
Member

Choose a reason for hiding this comment

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

you can use

np_version_gte1p24 = _nlv >= Version("1.24")

instead here and below.

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! I updated it to use these helpers.

warnings.simplefilter("ignore", np.VisibleDeprecationWarning)
result = np.asarray(values, dtype=dtype)
except ValueError:
# Using try/except since it's more performant than checking is_list_like
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/indexes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
RangeIndex,
)
import pandas._testing as tm
from pandas.util.version import Version


class TestCommon:
Expand Down Expand Up @@ -389,7 +390,10 @@ def test_astype_preserves_name(self, index, dtype):
warn = None
if index.dtype.kind == "c" and dtype in ["float64", "int64", "uint64"]:
# imaginary components discarded
warn = np.ComplexWarning
if Version(np.__version__) >= Version("1.25.0"):
warn = np.exceptions.ComplexWarning
else:
warn = np.ComplexWarning

is_pyarrow_str = str(index.dtype) == "string[pyarrow]" and dtype == "category"
try:
Expand Down