Skip to content

Commit 3f2daf5

Browse files
COMPAT: Use actual isinstance check for pyarrow.Array instead of hasattr(.. 'type') duck typing (#52830)
* Use actual isinstance check for pyarrow.Array instead of hasattr(.. 'type') duck typing * fix import * move to cython * add docstring * try TypeGuard * remove ArrowArrayTypeGuard
1 parent a1757c4 commit 3f2daf5

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

pandas/_libs/lib.pyi

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def infer_dtype(value: object, skipna: bool = ...) -> str: ...
4242
def is_iterator(obj: object) -> bool: ...
4343
def is_scalar(val: object) -> bool: ...
4444
def is_list_like(obj: object, allow_sets: bool = ...) -> bool: ...
45+
def is_pyarrow_array(obj: object) -> bool: ...
4546
def is_period(val: object) -> TypeGuard[Period]: ...
4647
def is_interval(val: object) -> TypeGuard[Interval]: ...
4748
def is_decimal(val: object) -> TypeGuard[Decimal]: ...

pandas/_libs/lib.pyx

+23
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ i8max = <int64_t>INT64_MAX
149149
u8max = <uint64_t>UINT64_MAX
150150

151151

152+
cdef bint PYARROW_INSTALLED = False
153+
154+
try:
155+
import pyarrow as pa
156+
157+
PYARROW_INSTALLED = True
158+
except ImportError:
159+
pa = None
160+
161+
152162
@cython.wraparound(False)
153163
@cython.boundscheck(False)
154164
def memory_usage_of_objects(arr: object[:]) -> int64_t:
@@ -1177,6 +1187,19 @@ cdef bint c_is_list_like(object obj, bint allow_sets) except -1:
11771187
)
11781188

11791189

1190+
def is_pyarrow_array(obj):
1191+
"""
1192+
Return True if given object is a pyarrow Array or ChunkedArray.
1193+
1194+
Returns
1195+
-------
1196+
bool
1197+
"""
1198+
if PYARROW_INSTALLED:
1199+
return isinstance(obj, (pa.Array, pa.ChunkedArray))
1200+
return False
1201+
1202+
11801203
_TYPE_MAP = {
11811204
"categorical": "categorical",
11821205
"category": "categorical",

pandas/core/arrays/string_.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy: bool = Fal
355355
result[na_values] = libmissing.NA
356356

357357
else:
358-
if hasattr(scalars, "type"):
358+
if lib.is_pyarrow_array(scalars):
359359
# pyarrow array; we cannot rely on the "to_numpy" check in
360360
# ensure_string_array because calling scalars.to_numpy would set
361361
# zero_copy_only to True which caused problems see GH#52076

0 commit comments

Comments
 (0)