1
1
import numpy as np
2
2
import pytest
3
3
4
- import pandas .util . _test_decorators as td
4
+ from pandas .compat . _optional import import_optional_dependency
5
5
6
6
import pandas as pd
7
7
import pandas ._testing as tm
8
8
9
+ try :
10
+ pa = import_optional_dependency ("pyarrow" , min_version = "0.16.0" )
11
+ except ImportError :
12
+ pytestmark = pytest .mark .skip (reason = "Pyarrow not available" )
13
+
9
14
arrays = [pd .array ([1 , 2 , 3 , None ], dtype = dtype ) for dtype in tm .ALL_EA_INT_DTYPES ]
10
15
arrays += [pd .array ([0.1 , 0.2 , 0.3 , None ], dtype = dtype ) for dtype in tm .FLOAT_EA_DTYPES ]
11
16
arrays += [pd .array ([True , False , True , None ], dtype = "boolean" )]
@@ -16,10 +21,8 @@ def data(request):
16
21
return request .param
17
22
18
23
19
- @td .skip_if_no ("pyarrow" , min_version = "0.15.0" )
20
24
def test_arrow_array (data ):
21
25
# protocol added in 0.15.0
22
- import pyarrow as pa
23
26
24
27
arr = pa .array (data )
25
28
expected = pa .array (
@@ -29,10 +32,8 @@ def test_arrow_array(data):
29
32
assert arr .equals (expected )
30
33
31
34
32
- @td .skip_if_no ("pyarrow" , min_version = "0.16.0" )
33
35
def test_arrow_roundtrip (data ):
34
36
# roundtrip possible from arrow 0.16.0
35
- import pyarrow as pa
36
37
37
38
df = pd .DataFrame ({"a" : data })
38
39
table = pa .table (df )
@@ -42,11 +43,9 @@ def test_arrow_roundtrip(data):
42
43
tm .assert_frame_equal (result , df )
43
44
44
45
45
- @td .skip_if_no ("pyarrow" , min_version = "0.16.0" )
46
46
def test_arrow_from_arrow_uint ():
47
47
# https://github.com/pandas-dev/pandas/issues/31896
48
48
# possible mismatch in types
49
- import pyarrow as pa
50
49
51
50
dtype = pd .UInt32Dtype ()
52
51
result = dtype .__from_arrow__ (pa .array ([1 , 2 , 3 , 4 , None ], type = "int64" ))
@@ -55,10 +54,8 @@ def test_arrow_from_arrow_uint():
55
54
tm .assert_extension_array_equal (result , expected )
56
55
57
56
58
- @td .skip_if_no ("pyarrow" , min_version = "0.16.0" )
59
57
def test_arrow_sliced ():
60
58
# https://github.com/pandas-dev/pandas/issues/38525
61
- import pyarrow as pa
62
59
63
60
df = pd .DataFrame ({"a" : pd .array ([0 , None , 2 , 3 , None ], dtype = "Int64" )})
64
61
table = pa .table (df )
@@ -68,10 +65,8 @@ def test_arrow_sliced():
68
65
69
66
70
67
@pytest .fixture
71
- def np_dtype_to_arrays (request ):
72
- import pyarrow as pa
73
-
74
- np_dtype = request .param
68
+ def np_dtype_to_arrays (any_real_dtype ):
69
+ np_dtype = np .dtype (any_real_dtype )
75
70
pa_type = pa .from_numpy_dtype (np_dtype )
76
71
77
72
pa_array = pa .array ([0 , 1 , 2 ], type = pa_type )
@@ -80,25 +75,6 @@ def np_dtype_to_arrays(request):
80
75
return np_dtype , pa_array , np_expected , mask_expected
81
76
82
77
83
- @td .skip_if_no ("pyarrow" )
84
- @pytest .mark .parametrize (
85
- "np_dtype_to_arrays" ,
86
- (
87
- [
88
- np .int8 (),
89
- np .int16 (),
90
- np .int32 (),
91
- np .int64 (),
92
- np .uint8 (),
93
- np .uint16 (),
94
- np .uint32 (),
95
- np .uint64 (),
96
- np .float32 (),
97
- np .float64 (),
98
- ]
99
- ),
100
- indirect = True ,
101
- )
102
78
def test_pyarrow_array_to_numpy_and_mask (np_dtype_to_arrays ):
103
79
"""
104
80
Test conversion from pyarrow array to numpy array.
@@ -109,8 +85,6 @@ def test_pyarrow_array_to_numpy_and_mask(np_dtype_to_arrays):
109
85
Also tests empty pyarrow arrays with non empty buffers.
110
86
See https://github.com/pandas-dev/pandas/issues/40896
111
87
"""
112
- import pyarrow as pa
113
-
114
88
from pandas .core .arrays ._arrow_utils import pyarrow_array_to_numpy_and_mask
115
89
116
90
np_dtype , pa_array , np_expected , mask_expected = np_dtype_to_arrays
0 commit comments