Skip to content

Commit 3cc596a

Browse files
committed
Further improve the workaround for pandas dtypes
1 parent c5ed329 commit 3cc596a

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

pygmt/clib/conversion.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,14 @@ def _to_numpy(data: Any) -> np.ndarray:
162162
"date64[ms][pyarrow]": np.datetime64,
163163
}
164164

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
165+
# pandas numeric dtypes were converted to np.object_ dtype prior pandas 2.2, and are
166+
# converted to suitable numpy dtypes since pandas 2.2. Refer to the following link
167+
# 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
169168
# Here are the workarounds for pandas < 2.2.
170169
# Following SPEC 0, pandas 2.1 should be dropped in 2025 Q3, so it's likely we can
171170
# remove the workaround in PyGMT v0.17.0.
172171
if Version(pd.__version__) < Version("2.2"):
172+
# Specify mapping from pandas nullable dtypes to suitable numpy dtypes
173173
dtypes.update(
174174
{
175175
"Int8": np.int8,
@@ -184,9 +184,10 @@ def _to_numpy(data: Any) -> np.ndarray:
184184
"Float64": np.float64,
185185
}
186186
)
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.
187+
# For pandas.Index/pandas.Series, pandas/pyarrow integer dtypes with missing
188+
# values should be cast to NumPy float dtypes and NaN is used as missing value
189+
# indicator.
190+
if getattr(data, "hasnans", False): # pandas.Index/pandas.Series has 'hasnans'
190191
dtype = np.float64 if data.dtype.kind in "iu" else data.dtype.numpy_dtype
191192
data = data.to_numpy(na_value=np.nan).astype(dtype=dtype)
192193

0 commit comments

Comments
 (0)