From 43628249990a2be1d7ab86b5d599d3bb2aed463f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Sok=C3=B3=C5=82?= Date: Fri, 4 Aug 2023 13:36:32 +0200 Subject: [PATCH 1/2] ENH: Update numpy exceptions imports --- pandas/core/common.py | 5 ++++- pandas/tests/indexes/test_common.py | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pandas/core/common.py b/pandas/core/common.py index 11c6d8ea1a821..1683d11fd5853 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -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, @@ -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"): + 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 diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index e29fc12f118f4..3eac6eeb37b10 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -27,6 +27,7 @@ RangeIndex, ) import pandas._testing as tm +from pandas.util.version import Version class TestCommon: @@ -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: From 3894c6e8626b761ea9e8c2be7fcf493467fb6597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Sok=C3=B3=C5=82?= Date: Mon, 7 Aug 2023 09:59:44 +0200 Subject: [PATCH 2/2] ENH: Use version checks from dedicated utils --- pandas/core/common.py | 5 ++--- pandas/tests/indexes/test_common.py | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pandas/core/common.py b/pandas/core/common.py index 1683d11fd5853..6d419098bf279 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -32,6 +32,7 @@ import numpy as np from pandas._libs import lib +from pandas.compat.numpy import np_version_gte1p24 from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike from pandas.core.dtypes.common import ( @@ -45,8 +46,6 @@ ) from pandas.core.dtypes.inference import iterable_not_string -from pandas.util.version import Version - if TYPE_CHECKING: from pandas._typing import ( AnyArrayLike, @@ -238,7 +237,7 @@ 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 - if Version(np.__version__) < Version("1.24.0"): + if not np_version_gte1p24: warnings.simplefilter("ignore", np.VisibleDeprecationWarning) result = np.asarray(values, dtype=dtype) except ValueError: diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index 3eac6eeb37b10..05834b02a5ff3 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -13,6 +13,7 @@ import pytest from pandas.compat import IS64 +from pandas.compat.numpy import np_version_gte1p25 from pandas.core.dtypes.common import ( is_integer_dtype, @@ -27,7 +28,6 @@ RangeIndex, ) import pandas._testing as tm -from pandas.util.version import Version class TestCommon: @@ -390,7 +390,7 @@ def test_astype_preserves_name(self, index, dtype): warn = None if index.dtype.kind == "c" and dtype in ["float64", "int64", "uint64"]: # imaginary components discarded - if Version(np.__version__) >= Version("1.25.0"): + if np_version_gte1p25: warn = np.exceptions.ComplexWarning else: warn = np.ComplexWarning