Skip to content

Commit 1e8353e

Browse files
committed
fixed import pyarrow
1 parent 5f4170d commit 1e8353e

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

pandas/core/dtypes/factory.py

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
)
1111

1212
import numpy as np
13-
import pyarrow as pa
1413

1514
from pandas._libs import missing as libmissing
1615

@@ -77,6 +76,8 @@ def string(
7776
if mode not in valid_modes:
7877
raise ValueError(f"mode must be one of {valid_modes}, got {mode}")
7978
if backend == "pyarrow":
79+
import pyarrow as pa
80+
8081
if mode == "string":
8182
pa_type = pa.large_string() if large else pa.string()
8283
else: # mode == "binary"
@@ -128,6 +129,8 @@ def datetime(
128129
return DatetimeTZDtype(unit=unit, tz=tz)
129130
return np.dtype(f"datetime64[{unit}]")
130131
else: # pyarrow
132+
import pyarrow as pa
133+
131134
return ArrowDtype(pa.timestamp(unit, tz=tz))
132135

133136

@@ -167,24 +170,25 @@ def integer(
167170

168171
if backend == "numpy":
169172
return np.dtype(f"int{bits}")
170-
171-
if backend == "pandas":
173+
elif backend == "pandas":
172174
if bits == 8:
173175
return Int8Dtype()
174176
elif bits == 16:
175177
return Int16Dtype()
176178
elif bits == 32:
177179
return Int32Dtype()
178-
elif bits == 64:
180+
else: # bits == 64
179181
return Int64Dtype()
180182
elif backend == "pyarrow":
183+
import pyarrow as pa
184+
181185
if bits == 8:
182186
return ArrowDtype(pa.int8())
183187
elif bits == 16:
184188
return ArrowDtype(pa.int16())
185189
elif bits == 32:
186190
return ArrowDtype(pa.int32())
187-
elif bits == 64:
191+
else: # bits == 64
188192
return ArrowDtype(pa.int64())
189193
else:
190194
raise ValueError(f"Unsupported backend: {backend!r}")
@@ -224,16 +228,17 @@ def floating(
224228

225229
if backend == "numpy":
226230
return np.dtype(f"float{bits}")
227-
228-
if backend == "pandas":
231+
elif backend == "pandas":
229232
if bits == 32:
230233
return Float32Dtype()
231-
elif bits == 64:
234+
else: # bits == 64
232235
return Float64Dtype()
233236
elif backend == "pyarrow":
237+
import pyarrow as pa
238+
234239
if bits == 32:
235240
return ArrowDtype(pa.float32())
236-
elif bits == 64:
241+
else: # bits == 64
237242
return ArrowDtype(pa.float64())
238243
else:
239244
raise ValueError(f"Unsupported backend: {backend!r}")
@@ -270,6 +275,8 @@ def decimal(
270275
decimal256[40, 5][pyarrow]
271276
"""
272277
if backend == "pyarrow":
278+
import pyarrow as pa
279+
273280
if precision <= 38:
274281
return ArrowDtype(pa.decimal128(precision, scale))
275282
return ArrowDtype(pa.decimal256(precision, scale))
@@ -302,6 +309,8 @@ def boolean(
302309
if backend == "numpy":
303310
return BooleanDtype()
304311
else: # pyarrow
312+
import pyarrow as pa
313+
305314
return ArrowDtype(pa.bool_())
306315

307316

@@ -344,6 +353,8 @@ def list(
344353
if backend == "numpy":
345354
return np.dtype("object")
346355
else: # pyarrow
356+
import pyarrow as pa
357+
347358
if value_type is None:
348359
value_type = pa.int64()
349360
pa_type = pa.large_list(value_type) if large else pa.list_(value_type)
@@ -396,6 +407,8 @@ def categorical(
396407
if backend == "numpy":
397408
return CategoricalDtype(categories=categories, ordered=ordered)
398409
else: # pyarrow
410+
import pyarrow as pa
411+
399412
index_type = pa.int32() if index_type is None else index_type
400413
value_type = pa.string() if value_type is None else value_type
401414
return ArrowDtype(pa.dictionary(index_type, value_type))
@@ -437,6 +450,8 @@ def interval(
437450
if backend == "numpy":
438451
return IntervalDtype(subtype=subtype, closed=closed)
439452
else: # pyarrow
453+
import pyarrow as pa
454+
440455
if subtype is not None:
441456
return ArrowDtype(
442457
pa.struct(
@@ -491,6 +506,8 @@ def period(
491506
if backend == "numpy":
492507
return PeriodDtype(freq=freq)
493508
else: # pyarrow
509+
import pyarrow as pa
510+
494511
return ArrowDtype(pa.month_day_nano_interval())
495512

496513

@@ -590,6 +607,8 @@ def date(
590607

591608
if backend != "pyarrow":
592609
raise ValueError("Date types are only supported with PyArrow backend.")
610+
import pyarrow as pa
611+
593612
return ArrowDtype(pa.date32() if unit == "day" else pa.date64())
594613

595614

@@ -629,6 +648,8 @@ def duration(
629648
if backend == "numpy":
630649
return np.dtype(f"timedelta64[{unit}]")
631650
else: # pyarrow
651+
import pyarrow as pa
652+
632653
return ArrowDtype(pa.duration(unit))
633654

634655

@@ -677,6 +698,8 @@ def map(
677698
"""
678699
if backend != "pyarrow":
679700
raise ValueError("Map types are only supported with PyArrow backend.")
701+
import pyarrow as pa
702+
680703
return ArrowDtype(pa.map_(index_type, value_type))
681704

682705

@@ -724,14 +747,10 @@ def struct(
724747
1 (2, Bob)
725748
dtype: struct<id: int32, name: string>[pyarrow]
726749
"""
727-
if backend != "pyarrow":
728-
raise ValueError("Struct types are only supported with PyArrow backend.")
729-
# Validate that fields is a list of (str, type) tuples
730-
for field in fields:
731-
if (
732-
not isinstance(field, tuple)
733-
or len(field) != 2
734-
or not isinstance(field[0], str)
735-
):
736-
raise ValueError("Each field must be a tuple of (str, type), got {field}")
737-
return ArrowDtype(pa.struct(fields))
750+
if backend == "pyarrow":
751+
import pyarrow as pa
752+
753+
pa_fields = [(name, getattr(typ, "pyarrow_dtype", typ)) for name, typ in fields]
754+
return ArrowDtype(pa.struct(pa_fields))
755+
else:
756+
raise ValueError(f"Unsupported backend: {backend!r}")

0 commit comments

Comments
 (0)