77import pandas as pd
88import pytest
99from pygmt .clib .conversion import _to_ndarray
10+ from pygmt .helpers .testing import skip_if_no
1011
1112try :
1213 import pyarrow as pa
1516except ImportError :
1617 _HAS_PYARROW = False
1718
18-
19- @pytest .fixture (scope = "module" , name = "dtypes_numpy_numeric" )
20- def fixture_dtypes_numpy_numeric ():
21- """
22- List of NumPy numeric dtypes.
23-
24- Reference: https://numpy.org/doc/stable/reference/arrays.scalars.html
25- """
26- return [
27- np .int8 ,
28- np .int16 ,
29- np .int32 ,
30- np .int64 ,
31- np .longlong ,
32- np .uint8 ,
33- np .uint16 ,
34- np .uint32 ,
35- np .uint64 ,
36- np .ulonglong ,
37- np .float16 ,
38- np .float32 ,
39- np .float64 ,
40- np .longdouble ,
41- np .complex64 ,
42- np .complex128 ,
43- np .clongdouble ,
44- ]
45-
46-
47- @pytest .fixture (scope = "module" , name = "dtypes_pandas_numeric" )
48- def fixture_dtypes_pandas_numeric ():
49- """
50- List of pandas numeric dtypes.
51-
52- Reference: https://pandas.pydata.org/docs/reference/arrays.html
53- """
54- return [
55- pd .Int8Dtype (),
56- pd .Int16Dtype (),
57- pd .Int32Dtype (),
58- pd .Int64Dtype (),
59- pd .UInt8Dtype (),
60- pd .UInt16Dtype (),
61- pd .UInt32Dtype (),
62- pd .UInt64Dtype (),
63- pd .Float32Dtype (),
64- pd .Float64Dtype (),
65- ]
66-
67-
68- @pytest .fixture (scope = "module" , name = "dtypes_pandas_numeric_pyarrow_backend" )
69- def fixture_dtypes_pandas_numeric_pyarrow_backend ():
70- """
71- List of pandas dtypes that use pyarrow as the backend.
72-
73- Reference: https://pandas.pydata.org/docs/user_guide/pyarrow.html
74- """
75- return [
76- "int8[pyarrow]" ,
77- "int16[pyarrow]" ,
78- "int32[pyarrow]" ,
79- "int64[pyarrow]" ,
80- "uint8[pyarrow]" ,
81- "uint16[pyarrow]" ,
82- "uint32[pyarrow]" ,
83- "uint64[pyarrow]" ,
84- "float32[pyarrow]" ,
85- "float64[pyarrow]" ,
86- ]
87-
88-
89- @pytest .fixture (scope = "module" , name = "dtypes_pyarrow_numeric" )
90- def fixture_dtypes_pyarrow_numeric ():
91- """
92- List of pyarrow numeric dtypes.
93-
94- Reference: https://arrow.apache.org/docs/python/api/datatypes.html
95- """
96- if not _HAS_PYARROW :
97- return []
98- return [
99- pa .int8 (),
100- pa .int16 (),
101- pa .int32 (),
102- pa .int64 (),
103- pa .uint8 (),
104- pa .uint16 (),
105- pa .uint32 (),
106- pa .uint64 (),
107- # pa.float16(), # Need special handling.
108- pa .float32 (),
109- pa .float64 (),
110- ]
19+ dtypes_numpy = [
20+ np .int8 ,
21+ np .int16 ,
22+ np .int32 ,
23+ np .int64 ,
24+ np .longlong ,
25+ np .uint8 ,
26+ np .uint16 ,
27+ np .uint32 ,
28+ np .uint64 ,
29+ np .ulonglong ,
30+ np .float16 ,
31+ np .float32 ,
32+ np .float64 ,
33+ np .longdouble ,
34+ np .complex64 ,
35+ np .complex128 ,
36+ np .clongdouble ,
37+ ]
11138
11239
11340def _check_result (result ):
@@ -125,60 +52,97 @@ def _check_result(result):
12552 assert result .dtype != np .object_
12653
12754
128- def test_to_ndarray_numpy_ndarray_numpy_numeric (dtypes_numpy_numeric ):
55+ @pytest .mark .parametrize ("dtype" , dtypes_numpy )
56+ def test_to_ndarray_numpy_ndarray_numpy_numeric (dtype ):
12957 """
13058 Test the _to_ndarray function with 1-D NumPy arrays.
13159 """
13260 # 1-D array
133- for dtype in dtypes_numpy_numeric :
134- array = np .array ([1 , 2 , 3 ], dtype = dtype )
135- assert array .dtype == dtype
136- result = _to_ndarray (array )
137- _check_result (result )
138- npt .assert_array_equal (result , array )
61+ array = np .array ([1 , 2 , 3 ], dtype = dtype )
62+ assert array .dtype == dtype
63+ result = _to_ndarray (array )
64+ _check_result (result )
65+ npt .assert_array_equal (result , array )
13966
14067 # 2-D array
141- for dtype in dtypes_numpy_numeric :
142- array = np .array ([[1 , 2 , 3 ], [4 , 5 , 6 ]], dtype = dtype )
143- assert array .dtype == dtype
144- result = _to_ndarray (array )
145- _check_result (result )
146- npt .assert_array_equal (result , array )
68+ array = np .array ([[1 , 2 , 3 ], [4 , 5 , 6 ]], dtype = dtype )
69+ assert array .dtype == dtype
70+ result = _to_ndarray (array )
71+ _check_result (result )
72+ npt .assert_array_equal (result , array )
14773
14874
149- def test_to_ndarray_pandas_series_numeric (
150- dtypes_numpy_numeric , dtypes_pandas_numeric , dtypes_pandas_numeric_pyarrow_backend
151- ):
75+ @pytest .mark .parametrize (
76+ "dtype" ,
77+ [
78+ * dtypes_numpy ,
79+ pytest .param (pd .Int8Dtype (), id = "Int8" ),
80+ pytest .param (pd .Int16Dtype (), id = "Int16" ),
81+ pytest .param (pd .Int32Dtype (), id = "Int32" ),
82+ pytest .param (pd .Int64Dtype (), id = "Int64" ),
83+ pytest .param (pd .UInt8Dtype (), id = "UInt8" ),
84+ pytest .param (pd .UInt16Dtype (), id = "UInt16" ),
85+ pytest .param (pd .UInt32Dtype (), id = "UInt32" ),
86+ pytest .param (pd .UInt64Dtype (), id = "UInt64" ),
87+ pytest .param (pd .Float32Dtype (), id = "Float32" ),
88+ pytest .param (pd .Float64Dtype (), id = "Float64" ),
89+ pytest .param ("int8[pyarrow]" , marks = skip_if_no (package = "pyarrow" )),
90+ pytest .param ("int16[pyarrow]" , marks = skip_if_no (package = "pyarrow" )),
91+ pytest .param ("int32[pyarrow]" , marks = skip_if_no (package = "pyarrow" )),
92+ pytest .param ("int64[pyarrow]" , marks = skip_if_no (package = "pyarrow" )),
93+ pytest .param ("uint8[pyarrow]" , marks = skip_if_no (package = "pyarrow" )),
94+ pytest .param ("uint16[pyarrow]" , marks = skip_if_no (package = "pyarrow" )),
95+ pytest .param ("uint32[pyarrow]" , marks = skip_if_no (package = "pyarrow" )),
96+ pytest .param ("uint64[pyarrow]" , marks = skip_if_no (package = "pyarrow" )),
97+ pytest .param ("float32[pyarrow]" , marks = skip_if_no (package = "pyarrow" )),
98+ pytest .param ("float64[pyarrow]" , marks = skip_if_no (package = "pyarrow" )),
99+ ],
100+ )
101+ def test_to_ndarray_pandas_series_numeric (dtype ):
152102 """
153103 Test the _to_ndarray function with pandas Series with NumPy dtypes, pandas dtypes,
154104 and pandas dtypes with pyarrow backend.
155105 """
156- for dtype in (
157- dtypes_numpy_numeric
158- + dtypes_pandas_numeric
159- + dtypes_pandas_numeric_pyarrow_backend
160- ):
161- series = pd .Series ([1 , 2 , 3 ], dtype = dtype )
162- assert series .dtype == dtype
163- result = _to_ndarray (series )
164- _check_result (result )
165- npt .assert_array_equal (result , series )
106+ series = pd .Series ([1 , 2 , 3 ], dtype = dtype )
107+ assert series .dtype == dtype
108+ result = _to_ndarray (series )
109+ _check_result (result )
110+ npt .assert_array_equal (result , series )
166111
167112
168113@pytest .mark .skipif (not _HAS_PYARROW , reason = "pyarrow is not installed" )
169- def test_to_ndarray_pandas_series_pyarrow_dtype (dtypes_pyarrow_numeric ):
114+ @pytest .mark .parametrize (
115+ "dtype" ,
116+ [
117+ "int8" ,
118+ "int16" ,
119+ "int32" ,
120+ "int64" ,
121+ "uint8" ,
122+ "uint16" ,
123+ "uint32" ,
124+ "uint64" ,
125+ "float32" ,
126+ "float64" ,
127+ ],
128+ )
129+ def test_to_ndarray_pyarrow_array (dtype ):
170130 """
171131 Test the _to_ndarray function with pandas Series with pyarrow dtypes.
172132 """
173- for dtype in dtypes_pyarrow_numeric :
174- array = pa .array ([1 , 2 , 3 ], type = dtype )
175- assert array .type == dtype
176- result = _to_ndarray (array )
177- _check_result (result )
178- npt .assert_array_equal (result , array )
179-
180- # Special handling for float16.
181- # Example from https://arrow.apache.org/docs/python/generated/pyarrow.float16.html
133+ array = pa .array ([1 , 2 , 3 ], type = dtype )
134+ assert array .type == dtype
135+ result = _to_ndarray (array )
136+ _check_result (result )
137+ npt .assert_array_equal (result , array )
138+
139+
140+ def test_to_ndarray_pyarrow_array_float16 ():
141+ """
142+ Test the _to_ndarray function with pyarrow float16 array.
143+
144+ Example from https://arrow.apache.org/docs/python/generated/pyarrow.float16.html
145+ """
182146 array = pa .array (np .array ([1.5 , 2.5 , 3.5 ], dtype = np .float16 ), type = pa .float16 ())
183147 result = _to_ndarray (array )
184148 _check_result (result )
0 commit comments