Skip to content

Commit 5652869

Browse files
BUG: Slice Arrow buffer before passing it to numpy (pandas-dev#40896)
Modify test_arrow_compat.py: - Use import_optional_dependency to skip the whole module if pyarrow is not available. - Use any_real_dtype fixture.
1 parent 1586d50 commit 5652869

File tree

1 file changed

+8
-34
lines changed

1 file changed

+8
-34
lines changed

pandas/tests/arrays/masked/test_arrow_compat.py

+8-34
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import numpy as np
22
import pytest
33

4-
import pandas.util._test_decorators as td
4+
from pandas.compat._optional import import_optional_dependency
55

66
import pandas as pd
77
import pandas._testing as tm
88

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+
914
arrays = [pd.array([1, 2, 3, None], dtype=dtype) for dtype in tm.ALL_EA_INT_DTYPES]
1015
arrays += [pd.array([0.1, 0.2, 0.3, None], dtype=dtype) for dtype in tm.FLOAT_EA_DTYPES]
1116
arrays += [pd.array([True, False, True, None], dtype="boolean")]
@@ -16,10 +21,8 @@ def data(request):
1621
return request.param
1722

1823

19-
@td.skip_if_no("pyarrow", min_version="0.15.0")
2024
def test_arrow_array(data):
2125
# protocol added in 0.15.0
22-
import pyarrow as pa
2326

2427
arr = pa.array(data)
2528
expected = pa.array(
@@ -29,10 +32,8 @@ def test_arrow_array(data):
2932
assert arr.equals(expected)
3033

3134

32-
@td.skip_if_no("pyarrow", min_version="0.16.0")
3335
def test_arrow_roundtrip(data):
3436
# roundtrip possible from arrow 0.16.0
35-
import pyarrow as pa
3637

3738
df = pd.DataFrame({"a": data})
3839
table = pa.table(df)
@@ -42,11 +43,9 @@ def test_arrow_roundtrip(data):
4243
tm.assert_frame_equal(result, df)
4344

4445

45-
@td.skip_if_no("pyarrow", min_version="0.16.0")
4646
def test_arrow_from_arrow_uint():
4747
# https://github.com/pandas-dev/pandas/issues/31896
4848
# possible mismatch in types
49-
import pyarrow as pa
5049

5150
dtype = pd.UInt32Dtype()
5251
result = dtype.__from_arrow__(pa.array([1, 2, 3, 4, None], type="int64"))
@@ -55,10 +54,8 @@ def test_arrow_from_arrow_uint():
5554
tm.assert_extension_array_equal(result, expected)
5655

5756

58-
@td.skip_if_no("pyarrow", min_version="0.16.0")
5957
def test_arrow_sliced():
6058
# https://github.com/pandas-dev/pandas/issues/38525
61-
import pyarrow as pa
6259

6360
df = pd.DataFrame({"a": pd.array([0, None, 2, 3, None], dtype="Int64")})
6461
table = pa.table(df)
@@ -68,10 +65,8 @@ def test_arrow_sliced():
6865

6966

7067
@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)
7570
pa_type = pa.from_numpy_dtype(np_dtype)
7671

7772
pa_array = pa.array([0, 1, 2], type=pa_type)
@@ -80,25 +75,6 @@ def np_dtype_to_arrays(request):
8075
return np_dtype, pa_array, np_expected, mask_expected
8176

8277

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-
)
10278
def test_pyarrow_array_to_numpy_and_mask(np_dtype_to_arrays):
10379
"""
10480
Test conversion from pyarrow array to numpy array.
@@ -109,8 +85,6 @@ def test_pyarrow_array_to_numpy_and_mask(np_dtype_to_arrays):
10985
Also tests empty pyarrow arrays with non empty buffers.
11086
See https://github.com/pandas-dev/pandas/issues/40896
11187
"""
112-
import pyarrow as pa
113-
11488
from pandas.core.arrays._arrow_utils import pyarrow_array_to_numpy_and_mask
11589

11690
np_dtype, pa_array, np_expected, mask_expected = np_dtype_to_arrays

0 commit comments

Comments
 (0)