Skip to content

COMPAT: Use actual isinstance check for pyarrow.Array instead of hasattr(.. 'type') duck typing #52830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 2, 2023
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