Skip to content
Merged
2 changes: 2 additions & 0 deletions pandas/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import pandas.compat.compressors
from pandas.compat.numpy import is_numpy_dev
from pandas.compat.pyarrow import (
is_pyarrow_array,
pa_version_under7p0,
pa_version_under8p0,
pa_version_under9p0,
Expand Down Expand Up @@ -156,6 +157,7 @@ def get_lzma_file() -> type[pandas.compat.compressors.LZMAFile]:

__all__ = [
"is_numpy_dev",
"is_pyarrow_array",
"pa_version_under7p0",
"pa_version_under8p0",
"pa_version_under9p0",
Expand Down
14 changes: 14 additions & 0 deletions pandas/compat/pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from pandas.util.version import Version

PYARROW_INSTALLED = None

try:
import pyarrow as pa

Expand All @@ -14,9 +16,21 @@
pa_version_under9p0 = _palv < Version("9.0.0")
pa_version_under10p0 = _palv < Version("10.0.0")
pa_version_under11p0 = _palv < Version("11.0.0")

PYARROW_INSTALLED = True
except ImportError:
pa = None

pa_version_under7p0 = True
pa_version_under8p0 = True
pa_version_under9p0 = True
pa_version_under10p0 = True
pa_version_under11p0 = True

PYARROW_INSTALLED = False


def is_pyarrow_array(obj) -> bool:
if PYARROW_INSTALLED:
return isinstance(obj, (pa.Array, pa.ChunkedArray))
return False
7 changes: 5 additions & 2 deletions pandas/core/arrays/string_.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
missing as libmissing,
)
from pandas._libs.arrays import NDArrayBacked
from pandas.compat import pa_version_under7p0
from pandas.compat import (
is_pyarrow_array,
pa_version_under7p0,
)
from pandas.compat.numpy import function as nv
from pandas.util._decorators import doc

Expand Down Expand Up @@ -356,7 +359,7 @@ def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy: bool = Fal
result[na_values] = libmissing.NA

else:
if hasattr(scalars, "type"):
if is_pyarrow_array(scalars):
# pyarrow array; we cannot rely on the "to_numpy" check in
# ensure_string_array because calling scalars.to_numpy would set
# zero_copy_only to True which caused problems see GH#52076
Expand Down