diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 4f7eb7c87b260..1765b22dda05f 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -655,7 +655,7 @@ Conversion Strings ^^^^^^^ -- +- Fixed bug in checking for ``string[pyarrow]`` dtype incorrectly raising an ImportError when pyarrow is not installed (:issue:`44327`) - Interval diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index 72cc28c1dd66d..75689b8dba8f5 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -613,7 +613,7 @@ def is_dtype_equal(source, target) -> bool: src = get_dtype(source) if isinstance(src, ExtensionDtype): return src == target - except (TypeError, AttributeError): + except (TypeError, AttributeError, ImportError): return False elif isinstance(source, str): return is_dtype_equal(target, source) @@ -622,7 +622,7 @@ def is_dtype_equal(source, target) -> bool: source = get_dtype(source) target = get_dtype(target) return source == target - except (TypeError, AttributeError): + except (TypeError, AttributeError, ImportError): # invalid comparison # object == category will hit this diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py index be5cb81506efd..7f1bcecc31d26 100644 --- a/pandas/tests/dtypes/test_common.py +++ b/pandas/tests/dtypes/test_common.py @@ -115,6 +115,7 @@ def test_period_dtype(self, dtype): "float": np.dtype(np.float64), "object": np.dtype(object), "category": com.pandas_dtype("category"), + "string": pd.StringDtype(), } @@ -128,6 +129,12 @@ def test_dtype_equal(name1, dtype1, name2, dtype2): assert not com.is_dtype_equal(dtype1, dtype2) +@pytest.mark.parametrize("name,dtype", list(dtypes.items()), ids=lambda x: str(x)) +def test_pyarrow_string_import_error(name, dtype): + # GH-44276 + assert not com.is_dtype_equal(dtype, "string[pyarrow]") + + @pytest.mark.parametrize( "dtype1,dtype2", [