Skip to content

Commit 28fa954

Browse files
committed
Separate variable 'dtype' and 'numpy_dtype' for the input and result array
1 parent b320d0b commit 28fa954

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

pygmt/clib/conversion.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,10 @@ def _to_numpy(data: Any) -> np.ndarray:
168168
"date64[ms][pyarrow]": "datetime64[ms]",
169169
}
170170

171-
# The expected numpy dtype for the result numpy array, but can be None.
172-
dtype = dtypes.get(str(getattr(data, "dtype", getattr(data, "type", ""))))
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))
173175

174176
# pandas numeric dtypes were converted to np.object_ dtype prior pandas 2.2, and are
175177
# converted to suitable NumPy dtypes since pandas 2.2. Refer to the following link
@@ -183,20 +185,20 @@ def _to_numpy(data: Any) -> np.ndarray:
183185
and hasattr(data.dtype, "numpy_dtype") # pandas dtypes only.
184186
and data.dtype.kind in "iuf" # Numeric dtypes only.
185187
): # pandas Series/Index with pandas nullable numeric dtypes.
186-
dtype = data.dtype.numpy_dtype # The expected numpy dtype.
188+
# The numpy dtype of the result numpy array.
189+
numpy_dtype = data.dtype.numpy_dtype
187190
if getattr(data, "hasnans", False):
188191
if data.dtype.kind in "iu":
189192
# Integers with missing values are converted to float64.
190-
dtype = np.float64
193+
numpy_dtype = np.float64
191194
data = data.to_numpy(na_value=np.nan)
192195

193-
array = np.ascontiguousarray(data, dtype=dtype)
196+
array = np.ascontiguousarray(data, dtype=numpy_dtype)
194197

195198
# Check if a np.object_ array can be converted to np.str_.
196199
if array.dtype == np.object_:
197200
with contextlib.suppress(TypeError, ValueError):
198201
return np.ascontiguousarray(array, dtype=np.str_)
199-
200202
return array
201203

202204

0 commit comments

Comments
 (0)