From 9ae80f5208cfe3459a913f4305322940a0a19f8b Mon Sep 17 00:00:00 2001 From: yuanx749 Date: Mon, 25 Nov 2024 15:17:54 +0800 Subject: [PATCH 1/6] Add test --- pandas/tests/dtypes/test_common.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py index e338fb1331734..ecdc56dce6cbe 100644 --- a/pandas/tests/dtypes/test_common.py +++ b/pandas/tests/dtypes/test_common.py @@ -3,7 +3,10 @@ import numpy as np import pytest -from pandas.compat import HAS_PYARROW +from pandas.compat import ( + HAS_PYARROW, + pa_version_under10p1, +) import pandas.util._test_decorators as td from pandas.core.dtypes.astype import astype_array @@ -835,3 +838,10 @@ def test_pandas_dtype_string_dtypes(string_storage): with pd.option_context("string_storage", string_storage): result = pandas_dtype("string") assert result == pd.StringDtype(string_storage, na_value=pd.NA) + + +@pytest.mark.skipif(not pa_version_under10p1, reason="pyarrow>=10.0.1 installed") +def test_construct_from_string_without_pyarrow_installed(): + # GH 57928 + with pytest.raises(ImportError, match="pyarrow>=10.0.1 is required"): + pd.Series([-1.5, 0.2, None], dtype="float32[pyarrow]") From 31801961ba6b9a6c7e6e211699dad5d6075c02a7 Mon Sep 17 00:00:00 2001 From: yuanx749 Date: Mon, 25 Nov 2024 15:18:16 +0800 Subject: [PATCH 2/6] Fix --- pandas/core/dtypes/dtypes.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 96b0aa16940a6..e5d1033de4457 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -2344,6 +2344,8 @@ def construct_from_string(cls, string: str) -> ArrowDtype: if string == "string[pyarrow]": # Ensure Registry.find skips ArrowDtype to use StringDtype instead raise TypeError("string[pyarrow] should be constructed by StringDtype") + if pa_version_under10p1: + raise ImportError("pyarrow>=10.0.1 is required for ArrowDtype") base_type = string[:-9] # get rid of "[pyarrow]" try: From 87d1e6f9d73ad93e62206efaa77fc85d742cc151 Mon Sep 17 00:00:00 2001 From: yuanx749 Date: Mon, 25 Nov 2024 15:18:28 +0800 Subject: [PATCH 3/6] Add note --- doc/source/whatsnew/v3.0.0.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 1d55fc3ed7b84..5dfa3482bb0ea 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -761,6 +761,8 @@ ExtensionArray - Bug in :meth:`.arrays.ArrowExtensionArray.__setitem__` which caused wrong behavior when using an integer array with repeated values as a key (:issue:`58530`) - Bug in :meth:`api.types.is_datetime64_any_dtype` where a custom :class:`ExtensionDtype` would return ``False`` for array-likes (:issue:`57055`) - Bug in comparison between object with :class:`ArrowDtype` and incompatible-dtyped (e.g. string vs bool) incorrectly raising instead of returning all-``False`` (for ``==``) or all-``True`` (for ``!=``) (:issue:`59505`) +- Bug in constructing pandas data structures when passing into ``dtype`` a string of the type followed by +``[pyarrow]`` while PyArrow is not installed would raise ``NameError`` rather than ``ImportError`` (:issue:`57928`) - Bug in various :class:`DataFrame` reductions for pyarrow temporal dtypes returning incorrect dtype when result was null (:issue:`59234`) Styler From a6dfb6d83f71599f923d25a46b8a1253594e9213 Mon Sep 17 00:00:00 2001 From: Xiao Yuan Date: Tue, 26 Nov 2024 09:25:46 +0800 Subject: [PATCH 4/6] Update pandas/tests/dtypes/test_common.py Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- pandas/tests/dtypes/test_common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py index ecdc56dce6cbe..7fb88092512bd 100644 --- a/pandas/tests/dtypes/test_common.py +++ b/pandas/tests/dtypes/test_common.py @@ -840,7 +840,7 @@ def test_pandas_dtype_string_dtypes(string_storage): assert result == pd.StringDtype(string_storage, na_value=pd.NA) -@pytest.mark.skipif(not pa_version_under10p1, reason="pyarrow>=10.0.1 installed") +@td.skip_if_installed("pyarrow") def test_construct_from_string_without_pyarrow_installed(): # GH 57928 with pytest.raises(ImportError, match="pyarrow>=10.0.1 is required"): From dfdabc15526e476ee094d569265adc0a696dbee9 Mon Sep 17 00:00:00 2001 From: yuanx749 Date: Tue, 26 Nov 2024 09:39:32 +0800 Subject: [PATCH 5/6] update --- pandas/tests/dtypes/test_common.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py index 7fb88092512bd..5a59617ce5bd3 100644 --- a/pandas/tests/dtypes/test_common.py +++ b/pandas/tests/dtypes/test_common.py @@ -3,10 +3,7 @@ import numpy as np import pytest -from pandas.compat import ( - HAS_PYARROW, - pa_version_under10p1, -) +from pandas.compat import HAS_PYARROW import pandas.util._test_decorators as td from pandas.core.dtypes.astype import astype_array From 10ce01b05e3e3360e1c1b2be83809dd865ebba17 Mon Sep 17 00:00:00 2001 From: yuanx749 Date: Wed, 27 Nov 2024 10:04:10 +0800 Subject: [PATCH 6/6] Fix doc warning --- doc/source/whatsnew/v3.0.0.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 3255eda8e5086..4bd31de185bb4 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -761,8 +761,7 @@ ExtensionArray - Bug in :meth:`.arrays.ArrowExtensionArray.__setitem__` which caused wrong behavior when using an integer array with repeated values as a key (:issue:`58530`) - Bug in :meth:`api.types.is_datetime64_any_dtype` where a custom :class:`ExtensionDtype` would return ``False`` for array-likes (:issue:`57055`) - Bug in comparison between object with :class:`ArrowDtype` and incompatible-dtyped (e.g. string vs bool) incorrectly raising instead of returning all-``False`` (for ``==``) or all-``True`` (for ``!=``) (:issue:`59505`) -- Bug in constructing pandas data structures when passing into ``dtype`` a string of the type followed by -``[pyarrow]`` while PyArrow is not installed would raise ``NameError`` rather than ``ImportError`` (:issue:`57928`) +- Bug in constructing pandas data structures when passing into ``dtype`` a string of the type followed by ``[pyarrow]`` while PyArrow is not installed would raise ``NameError`` rather than ``ImportError`` (:issue:`57928`) - Bug in various :class:`DataFrame` reductions for pyarrow temporal dtypes returning incorrect dtype when result was null (:issue:`59234`) Styler