@@ -76,8 +76,13 @@ def string(
76
76
if mode not in valid_modes :
77
77
raise ValueError (f"mode must be one of { valid_modes } , got { mode } " )
78
78
if backend == "pyarrow" :
79
- import pyarrow as pa
80
-
79
+ try :
80
+ import pyarrow as pa
81
+ except ImportError as err :
82
+ raise ImportError (
83
+ "pyarrow is required for the 'pyarrow' backend. "
84
+ "Please install pyarrow to use this feature."
85
+ ) from err
81
86
if mode == "string" :
82
87
pa_type = pa .large_string () if large else pa .string ()
83
88
else : # mode == "binary"
@@ -129,8 +134,13 @@ def datetime(
129
134
return DatetimeTZDtype (unit = unit , tz = tz )
130
135
return np .dtype (f"datetime64[{ unit } ]" )
131
136
else : # pyarrow
132
- import pyarrow as pa
133
-
137
+ try :
138
+ import pyarrow as pa
139
+ except ImportError as err :
140
+ raise ImportError (
141
+ "pyarrow is required for the 'pyarrow' backend. "
142
+ "Please install pyarrow to use this feature."
143
+ ) from err
134
144
return ArrowDtype (pa .timestamp (unit , tz = tz ))
135
145
136
146
@@ -180,7 +190,13 @@ def integer(
180
190
else : # bits == 64
181
191
return Int64Dtype ()
182
192
elif backend == "pyarrow" :
183
- import pyarrow as pa
193
+ try :
194
+ import pyarrow as pa
195
+ except ImportError as err :
196
+ raise ImportError (
197
+ "pyarrow is required for the 'pyarrow' backend. "
198
+ "Please install pyarrow to use this feature."
199
+ ) from err
184
200
185
201
if bits == 8 :
186
202
return ArrowDtype (pa .int8 ())
@@ -234,7 +250,13 @@ def floating(
234
250
else : # bits == 64
235
251
return Float64Dtype ()
236
252
elif backend == "pyarrow" :
237
- import pyarrow as pa
253
+ try :
254
+ import pyarrow as pa
255
+ except ImportError as err :
256
+ raise ImportError (
257
+ "pyarrow is required for the 'pyarrow' backend. "
258
+ "Please install pyarrow to use this feature."
259
+ ) from err
238
260
239
261
if bits == 32 :
240
262
return ArrowDtype (pa .float32 ())
@@ -275,8 +297,13 @@ def decimal(
275
297
decimal256[40, 5][pyarrow]
276
298
"""
277
299
if backend == "pyarrow" :
278
- import pyarrow as pa
279
-
300
+ try :
301
+ import pyarrow as pa
302
+ except ImportError as err :
303
+ raise ImportError (
304
+ "pyarrow is required for the 'pyarrow' backend. "
305
+ "Please install pyarrow to use this feature."
306
+ ) from err
280
307
if precision <= 38 :
281
308
return ArrowDtype (pa .decimal128 (precision , scale ))
282
309
return ArrowDtype (pa .decimal256 (precision , scale ))
@@ -309,8 +336,13 @@ def boolean(
309
336
if backend == "numpy" :
310
337
return BooleanDtype ()
311
338
else : # pyarrow
312
- import pyarrow as pa
313
-
339
+ try :
340
+ import pyarrow as pa
341
+ except ImportError as err :
342
+ raise ImportError (
343
+ "pyarrow is required for the 'pyarrow' backend. "
344
+ "Please install pyarrow to use this feature."
345
+ ) from err
314
346
return ArrowDtype (pa .bool_ ())
315
347
316
348
@@ -353,7 +385,13 @@ def list(
353
385
if backend == "numpy" :
354
386
return np .dtype ("object" )
355
387
else : # pyarrow
356
- import pyarrow as pa
388
+ try :
389
+ import pyarrow as pa
390
+ except ImportError as err :
391
+ raise ImportError (
392
+ "pyarrow is required for the 'pyarrow' backend. "
393
+ "Please install pyarrow to use this feature."
394
+ ) from err
357
395
358
396
if value_type is None :
359
397
value_type = pa .int64 ()
@@ -407,7 +445,13 @@ def categorical(
407
445
if backend == "numpy" :
408
446
return CategoricalDtype (categories = categories , ordered = ordered )
409
447
else : # pyarrow
410
- import pyarrow as pa
448
+ try :
449
+ import pyarrow as pa
450
+ except ImportError as err :
451
+ raise ImportError (
452
+ "pyarrow is required for the 'pyarrow' backend. "
453
+ "Please install pyarrow to use this feature."
454
+ ) from err
411
455
412
456
index_type = pa .int32 () if index_type is None else index_type
413
457
value_type = pa .string () if value_type is None else value_type
@@ -450,7 +494,13 @@ def interval(
450
494
if backend == "numpy" :
451
495
return IntervalDtype (subtype = subtype , closed = closed )
452
496
else : # pyarrow
453
- import pyarrow as pa
497
+ try :
498
+ import pyarrow as pa
499
+ except ImportError as err :
500
+ raise ImportError (
501
+ "pyarrow is required for the 'pyarrow' backend. "
502
+ "Please install pyarrow to use this feature."
503
+ ) from err
454
504
455
505
if subtype is not None :
456
506
return ArrowDtype (
@@ -506,8 +556,13 @@ def period(
506
556
if backend == "numpy" :
507
557
return PeriodDtype (freq = freq )
508
558
else : # pyarrow
509
- import pyarrow as pa
510
-
559
+ try :
560
+ import pyarrow as pa
561
+ except ImportError as err :
562
+ raise ImportError (
563
+ "pyarrow is required for the 'pyarrow' backend. "
564
+ "Please install pyarrow to use this feature."
565
+ ) from err
511
566
return ArrowDtype (pa .month_day_nano_interval ())
512
567
513
568
@@ -607,7 +662,13 @@ def date(
607
662
608
663
if backend != "pyarrow" :
609
664
raise ValueError ("Date types are only supported with PyArrow backend." )
610
- import pyarrow as pa
665
+ try :
666
+ import pyarrow as pa
667
+ except ImportError as err :
668
+ raise ImportError (
669
+ "pyarrow is required for the 'pyarrow' backend. "
670
+ "Please install pyarrow to use this feature."
671
+ ) from err
611
672
612
673
return ArrowDtype (pa .date32 () if unit == "day" else pa .date64 ())
613
674
@@ -648,8 +709,13 @@ def duration(
648
709
if backend == "numpy" :
649
710
return np .dtype (f"timedelta64[{ unit } ]" )
650
711
else : # pyarrow
651
- import pyarrow as pa
652
-
712
+ try :
713
+ import pyarrow as pa
714
+ except ImportError as err :
715
+ raise ImportError (
716
+ "pyarrow is required for the 'pyarrow' backend. "
717
+ "Please install pyarrow to use this feature."
718
+ ) from err
653
719
return ArrowDtype (pa .duration (unit ))
654
720
655
721
@@ -698,7 +764,13 @@ def map(
698
764
"""
699
765
if backend != "pyarrow" :
700
766
raise ValueError ("Map types are only supported with PyArrow backend." )
701
- import pyarrow as pa
767
+ try :
768
+ import pyarrow as pa
769
+ except ImportError as err :
770
+ raise ImportError (
771
+ "pyarrow is required for the 'pyarrow' backend. "
772
+ "Please install pyarrow to use this feature."
773
+ ) from err
702
774
703
775
return ArrowDtype (pa .map_ (index_type , value_type ))
704
776
@@ -748,7 +820,13 @@ def struct(
748
820
dtype: struct<id: int32, name: string>[pyarrow]
749
821
"""
750
822
if backend == "pyarrow" :
751
- import pyarrow as pa
823
+ try :
824
+ import pyarrow as pa
825
+ except ImportError as err :
826
+ raise ImportError (
827
+ "pyarrow is required for the 'pyarrow' backend. "
828
+ "Please install pyarrow to use this feature."
829
+ ) from err
752
830
753
831
pa_fields = [(name , getattr (typ , "pyarrow_dtype" , typ )) for name , typ in fields ]
754
832
return ArrowDtype (pa .struct (pa_fields ))
0 commit comments