@@ -162,19 +162,36 @@ def _to_numpy(data: Any) -> np.ndarray:
162162 "date64[ms][pyarrow]" : np .datetime64 ,
163163 }
164164
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 ))
165+ # pandas nullable dtypes and pyarrow types were converted to np.object_ dtype
166+ # before, and are converted to suitable numpy dtypes since pandas 2.2.
167+ # Refer to the following link for details:
168+ # https://pandas.pydata.org/docs/whatsnew/v2.2.0.html#to-numpy-for-numpy-nullable-and-arrow-types-converts-to-suitable-numpy-dtype
169+ # Here are the workarounds for pandas < 2.2.
170+ # Following SPEC 0, pandas 2.1 should be dropped in 2025 Q3, so it's likely we can
171+ # remove the workaround in PyGMT v0.17.0.
172+ if Version (pd .__version__ ) < Version ("2.2" ):
173+ dtypes .update (
174+ {
175+ "Int8" : np .int8 ,
176+ "Int16" : np .int16 ,
177+ "Int32" : np .int32 ,
178+ "Int64" : np .int64 ,
179+ "UInt8" : np .uint8 ,
180+ "UInt16" : np .uint16 ,
181+ "UInt32" : np .uint32 ,
182+ "UInt64" : np .uint64 ,
183+ "Float32" : np .float32 ,
184+ "Float64" : np .float64 ,
185+ }
186+ )
187+ if hasattr (data , "isna" ) and data .isna ().any ():
188+ # Integer dtypes with missing values are cast to NumPy float dtypes and NaN
189+ # is used as missing value indicator.
190+ dtype = np .float64 if data .dtype .kind in "iu" else data .dtype .numpy_dtype
191+ data = data .to_numpy (dtype = dtype , na_value = np .nan )
192+
193+ vec_dtype = str (getattr (data , "dtype" , "" ))
194+ array = np .ascontiguousarray (data , dtype = dtypes .get (vec_dtype ))
178195 return array
179196
180197
0 commit comments