Skip to content

Commit 1d809c3

Browse files
yuanx749mroeschke
andauthored
BUG: fix NameError raised when specifying dtype with string having "[pyarrow]" while PyArrow is not installed (#60413)
* Add test * Fix * Add note * Update pandas/tests/dtypes/test_common.py Co-authored-by: Matthew Roeschke <[email protected]> * update * Fix doc warning --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 106f33c commit 1d809c3

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,7 @@ ExtensionArray
761761
- Bug in :meth:`.arrays.ArrowExtensionArray.__setitem__` which caused wrong behavior when using an integer array with repeated values as a key (:issue:`58530`)
762762
- Bug in :meth:`api.types.is_datetime64_any_dtype` where a custom :class:`ExtensionDtype` would return ``False`` for array-likes (:issue:`57055`)
763763
- 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`)
764+
- 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`)
764765
- Bug in various :class:`DataFrame` reductions for pyarrow temporal dtypes returning incorrect dtype when result was null (:issue:`59234`)
765766

766767
Styler

pandas/core/dtypes/dtypes.py

+2
Original file line numberDiff line numberDiff line change
@@ -2344,6 +2344,8 @@ def construct_from_string(cls, string: str) -> ArrowDtype:
23442344
if string == "string[pyarrow]":
23452345
# Ensure Registry.find skips ArrowDtype to use StringDtype instead
23462346
raise TypeError("string[pyarrow] should be constructed by StringDtype")
2347+
if pa_version_under10p1:
2348+
raise ImportError("pyarrow>=10.0.1 is required for ArrowDtype")
23472349

23482350
base_type = string[:-9] # get rid of "[pyarrow]"
23492351
try:

pandas/tests/dtypes/test_common.py

+7
Original file line numberDiff line numberDiff line change
@@ -835,3 +835,10 @@ def test_pandas_dtype_string_dtypes(string_storage):
835835
with pd.option_context("string_storage", string_storage):
836836
result = pandas_dtype("string")
837837
assert result == pd.StringDtype(string_storage, na_value=pd.NA)
838+
839+
840+
@td.skip_if_installed("pyarrow")
841+
def test_construct_from_string_without_pyarrow_installed():
842+
# GH 57928
843+
with pytest.raises(ImportError, match="pyarrow>=10.0.1 is required"):
844+
pd.Series([-1.5, 0.2, None], dtype="float32[pyarrow]")

0 commit comments

Comments
 (0)