@@ -168,19 +168,32 @@ def _to_numpy(data: Any) -> np.ndarray:
168
168
"date64[ms][pyarrow]" : "datetime64[ms]" ,
169
169
}
170
170
171
+ # The dtype for the input object.
172
+ dtype = getattr (data , "dtype" , getattr (data , "type" , "" ))
173
+ # The numpy dtype for the result numpy array, but can be None.
174
+ numpy_dtype = dtypes .get (str (dtype ))
175
+
176
+ # pandas numeric dtypes were converted to np.object_ dtype prior pandas 2.2, and are
177
+ # converted to suitable NumPy dtypes since pandas 2.2. Refer to the following link
178
+ # for details: https://pandas.pydata.org/docs/whatsnew/v2.2.0.html#to-numpy-for-numpy-nullable-and-arrow-types-converts-to-suitable-numpy-dtype
179
+ #
180
+ # Workarounds for pandas < 2.2. Following SPEC 0, pandas 2.1 should be dropped in
181
+ # 2025 Q3, so it's likely we can remove the workaround in PyGMT v0.17.0.
171
182
if (
172
- hasattr (data , "isna" )
173
- and data .isna ().any ()
174
- and Version (pd .__version__ ) < Version ("2.2" )
175
- ):
176
- # Workaround for dealing with pd.NA with pandas < 2.2.
177
- # Bug report at: https://github.com/GenericMappingTools/pygmt/issues/2844
178
- # Following SPEC0, pandas 2.1 will be dropped in 2025 Q3, so it's likely
179
- # we can remove the workaround in PyGMT v0.17.0.
180
- array = np .ascontiguousarray (data .astype (float ))
181
- else :
182
- vec_dtype = str (getattr (data , "dtype" , getattr (data , "type" , "" )))
183
- array = np .ascontiguousarray (data , dtype = dtypes .get (vec_dtype ))
183
+ Version (pd .__version__ ) < Version ("2.2" ) # pandas < 2.2 only.
184
+ and hasattr (data , "dtype" ) # NumPy array or pandas objects only.
185
+ and hasattr (data .dtype , "numpy_dtype" ) # pandas dtypes only.
186
+ and data .dtype .kind in "iuf" # Numeric dtypes only.
187
+ ): # pandas Series/Index with pandas nullable numeric dtypes.
188
+ # The numpy dtype of the result numpy array.
189
+ numpy_dtype = data .dtype .numpy_dtype
190
+ if getattr (data , "hasnans" , False ):
191
+ if data .dtype .kind in "iu" :
192
+ # Integers with missing values are converted to float64.
193
+ numpy_dtype = np .float64
194
+ data = data .to_numpy (na_value = np .nan )
195
+
196
+ array = np .ascontiguousarray (data , dtype = numpy_dtype )
184
197
185
198
# Check if a np.object_ array can be converted to np.str_.
186
199
if array .dtype == np .object_ :
0 commit comments