@@ -132,6 +132,45 @@ def dataarray_to_matrix(
132
132
return matrix , region , inc
133
133
134
134
135
+ def _to_ndarray (array : Any ) -> np .ndarray :
136
+ """
137
+ Convert an array-like object to a C contiguous numpy array.
138
+
139
+ The function can convert any array-like objects with various dtypes to a C
140
+ contiguous numpy array. It works for n-D arrays, e.g., 1-D and 2-D arrays.
141
+
142
+ Parameters
143
+ ----------
144
+ array
145
+ The array-like object to convert.
146
+
147
+ Returns
148
+ -------
149
+ array
150
+ The C contiguous numpy array.
151
+ """
152
+ # A dictionary mapping unsupported dtypes to the expected numpy dtype.
153
+ dtypes = {
154
+ "date32[day][pyarrow]" : np .datetime64 ,
155
+ "date64[ms][pyarrow]" : np .datetime64 ,
156
+ }
157
+
158
+ if (
159
+ hasattr (array , "isna" )
160
+ and array .isna ().any ()
161
+ and Version (pd .__version__ ) < Version ("2.2" )
162
+ ):
163
+ # Workaround for dealing with pd.NA with pandas < 2.2.
164
+ # Bug report at: https://github.com/GenericMappingTools/pygmt/issues/2844
165
+ # Following SPEC0, pandas 2.1 will be dropped in 2025 Q3, so it's likely
166
+ # we can remove the workaround in PyGMT v0.17.0.
167
+ array = np .ascontiguousarray (array .astype (float ))
168
+ else :
169
+ vec_dtype = str (getattr (array , "dtype" , "" ))
170
+ array = np .ascontiguousarray (array , dtype = dtypes .get (vec_dtype ))
171
+ return array
172
+
173
+
135
174
def vectors_to_arrays (vectors : Sequence [Any ]) -> list [np .ndarray ]:
136
175
"""
137
176
Convert 1-D vectors (scalars, lists, or array-like) to C contiguous 1-D arrays.
@@ -171,27 +210,7 @@ def vectors_to_arrays(vectors: Sequence[Any]) -> list[np.ndarray]:
171
210
>>> all(i.ndim == 1 for i in arrays)
172
211
True
173
212
"""
174
- dtypes = {
175
- "date32[day][pyarrow]" : np .datetime64 ,
176
- "date64[ms][pyarrow]" : np .datetime64 ,
177
- }
178
- arrays = []
179
- for vector in vectors :
180
- if (
181
- hasattr (vector , "isna" )
182
- and vector .isna ().any ()
183
- and Version (pd .__version__ ) < Version ("2.2" )
184
- ):
185
- # Workaround for dealing with pd.NA with pandas < 2.2.
186
- # Bug report at: https://github.com/GenericMappingTools/pygmt/issues/2844
187
- # Following SPEC0, pandas 2.1 will be dropped in 2025 Q3, so it's likely
188
- # we can remove the workaround in PyGMT v0.17.0.
189
- array = np .ascontiguousarray (vector .astype (float ))
190
- else :
191
- vec_dtype = str (getattr (vector , "dtype" , "" ))
192
- array = np .ascontiguousarray (vector , dtype = dtypes .get (vec_dtype ))
193
- arrays .append (array )
194
- return arrays
213
+ return [_to_ndarray (vector ) for vector in vectors ]
195
214
196
215
197
216
def sequence_to_ctypes_array (
0 commit comments