@@ -132,6 +132,52 @@ def dataarray_to_matrix(
132132 return matrix , region , inc
133133
134134
135+ def _to_numpy (data : Any ) -> np .ndarray :
136+ """
137+ Convert an array-like object to a C contiguous numpy array.
138+
139+ The function aims to convert any array-like objects (e.g., Python lists or tuples,
140+ NumPy arrays with various dtypes, pandas.Series with NumPy/Pandas/PyArrow dtypes,
141+ PyArrow arrays with various dtypes) to a numpy array.
142+
143+ The function is internally used in the ``vectors_to_arrays`` function, which is
144+ responsible for converting a sequence of vectors to a list of C contiguous numpy
145+ arrays. Thus, the function uses the :numpy:func:`numpy.ascontiguousarray` function
146+ rather than the :numpy:func:`numpy.asarray`/:func:`numpy.asanyarray` functions, to
147+ ensure the returned numpy array is C contiguous.
148+
149+ Parameters
150+ ----------
151+ data
152+ The array-like object to convert.
153+
154+ Returns
155+ -------
156+ array
157+ The C contiguous numpy array.
158+ """
159+ # Mapping of unsupported dtypes to the expected numpy dtype.
160+ dtypes : dict [str , type ] = {
161+ "date32[day][pyarrow]" : np .datetime64 ,
162+ "date64[ms][pyarrow]" : np .datetime64 ,
163+ }
164+
165+ if (
166+ hasattr (data , "isna" )
167+ and data .isna ().any ()
168+ and Version (pd .__version__ ) < Version ("2.2" )
169+ ):
170+ # Workaround for dealing with pd.NA with pandas < 2.2.
171+ # Bug report at: https://github.com/GenericMappingTools/pygmt/issues/2844
172+ # Following SPEC0, pandas 2.1 will be dropped in 2025 Q3, so it's likely
173+ # we can remove the workaround in PyGMT v0.17.0.
174+ array = np .ascontiguousarray (data .astype (float ))
175+ else :
176+ vec_dtype = str (getattr (data , "dtype" , "" ))
177+ array = np .ascontiguousarray (data , dtype = dtypes .get (vec_dtype ))
178+ return array
179+
180+
135181def vectors_to_arrays (vectors : Sequence [Any ]) -> list [np .ndarray ]:
136182 """
137183 Convert 1-D vectors (scalars, lists, or array-like) to C contiguous 1-D arrays.
@@ -171,27 +217,7 @@ def vectors_to_arrays(vectors: Sequence[Any]) -> list[np.ndarray]:
171217 >>> all(i.ndim == 1 for i in arrays)
172218 True
173219 """
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
220+ return [_to_numpy (vector ) for vector in vectors ]
195221
196222
197223def sequence_to_ctypes_array (
0 commit comments