23
23
type_t ,
24
24
)
25
25
from pandas .compat import (
26
+ pa_version_under1p0 ,
26
27
pa_version_under2p0 ,
27
28
pa_version_under3p0 ,
28
29
pa_version_under4p0 ,
29
30
)
30
- from pandas .compat .pyarrow import pa_version_under1p0
31
31
from pandas .util ._decorators import doc
32
32
from pandas .util ._validators import validate_fillna_kwargs
33
33
55
55
)
56
56
from pandas .core .strings .object_array import ObjectStringArrayMixin
57
57
58
- try :
58
+ # PyArrow backed StringArrays are available starting at 1.0.0, but this
59
+ # file is imported from even if pyarrow is < 1.0.0, before pyarrow.compute
60
+ # and its compute functions existed. GH38801
61
+ if not pa_version_under1p0 :
59
62
import pyarrow as pa
60
- except ImportError :
61
- pa = None
62
- else :
63
- # PyArrow backed StringArrays are available starting at 1.0.0, but this
64
- # file is imported from even if pyarrow is < 1.0.0, before pyarrow.compute
65
- # and its compute functions existed. GH38801
66
- if not pa_version_under1p0 :
67
- import pyarrow .compute as pc
68
-
69
- ARROW_CMP_FUNCS = {
70
- "eq" : pc .equal ,
71
- "ne" : pc .not_equal ,
72
- "lt" : pc .less ,
73
- "gt" : pc .greater ,
74
- "le" : pc .less_equal ,
75
- "ge" : pc .greater_equal ,
76
- }
63
+ import pyarrow .compute as pc
64
+
65
+ ARROW_CMP_FUNCS = {
66
+ "eq" : pc .equal ,
67
+ "ne" : pc .not_equal ,
68
+ "lt" : pc .less ,
69
+ "gt" : pc .greater ,
70
+ "le" : pc .less_equal ,
71
+ "ge" : pc .greater_equal ,
72
+ }
77
73
78
74
79
75
if TYPE_CHECKING :
80
76
from pandas import Series
81
77
82
78
79
+ def _chk_pyarrow_available () -> None :
80
+ if pa_version_under1p0 :
81
+ msg = "pyarrow>=1.0.0 is required for PyArrow backed StringArray."
82
+ raise ImportError (msg )
83
+
84
+
83
85
@register_extension_dtype
84
86
class ArrowStringDtype (StringDtype ):
85
87
"""
@@ -112,6 +114,9 @@ class ArrowStringDtype(StringDtype):
112
114
#: StringDtype.na_value uses pandas.NA
113
115
na_value = libmissing .NA
114
116
117
+ def __init__ (self ):
118
+ _chk_pyarrow_available ()
119
+
115
120
@property
116
121
def type (self ) -> type [str ]:
117
122
return str
@@ -213,10 +218,8 @@ class ArrowStringArray(OpsMixin, ExtensionArray, ObjectStringArrayMixin):
213
218
Length: 4, dtype: arrow_string
214
219
"""
215
220
216
- _dtype = ArrowStringDtype ()
217
-
218
221
def __init__ (self , values ):
219
- self ._chk_pyarrow_available ()
222
+ self ._dtype = ArrowStringDtype ()
220
223
if isinstance (values , pa .Array ):
221
224
self ._data = pa .chunked_array ([values ])
222
225
elif isinstance (values , pa .ChunkedArray ):
@@ -229,19 +232,11 @@ def __init__(self, values):
229
232
"ArrowStringArray requires a PyArrow (chunked) array of string type"
230
233
)
231
234
232
- @classmethod
233
- def _chk_pyarrow_available (cls ) -> None :
234
- # TODO: maybe update import_optional_dependency to allow a minimum
235
- # version to be specified rather than use the global minimum
236
- if pa is None or pa_version_under1p0 :
237
- msg = "pyarrow>=1.0.0 is required for PyArrow backed StringArray."
238
- raise ImportError (msg )
239
-
240
235
@classmethod
241
236
def _from_sequence (cls , scalars , dtype : Dtype | None = None , copy : bool = False ):
242
237
from pandas .core .arrays .masked import BaseMaskedArray
243
238
244
- cls . _chk_pyarrow_available ()
239
+ _chk_pyarrow_available ()
245
240
246
241
if isinstance (scalars , BaseMaskedArray ):
247
242
# avoid costly conversion to object dtype in ensure_string_array and
0 commit comments