Skip to content

Commit d7dc5ff

Browse files
committed
Backward compatibility with pandas 2.0/2.1
1 parent 44bb82c commit d7dc5ff

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

pygmt/clib/conversion.py

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,20 +161,42 @@ def _to_ndarray(array: Any) -> np.ndarray:
161161
"date32[day][pyarrow]": np.datetime64,
162162
"date64[ms][pyarrow]": np.datetime64,
163163
}
164+
# pandas nullable types and pyarrow types were converted to object dtype prior to
165+
# pandas 2.2, and these dtypes are now converted to suitable numpy dtypes.
166+
# https://pandas.pydata.org/docs/whatsnew/v2.2.0.html#to-numpy-for-numpy-nullable-and-arrow-types-converts-to-suitable-numpy-dtype
167+
# Following SPEC 0, pandas 2.1 will be dropped in 2025 Q3, so it's likely we can
168+
# remove the workaround in PyGMT v0.17.0.
169+
if Version(pd.__version__) < Version("2.2"):
170+
dtypes.update(
171+
{
172+
"Int8": np.int8,
173+
"Int16": np.int16,
174+
"Int32": np.int32,
175+
"Int64": np.int64,
176+
"UInt8": np.uint8,
177+
"UInt16": np.uint16,
178+
"UInt32": np.uint32,
179+
"UInt64": np.uint64,
180+
"Float32": np.float32,
181+
"Float64": np.float64,
182+
}
183+
)
184+
if hasattr(array, "isna") and array.isna().any():
185+
dtypes.update(
186+
{
187+
"Int8": np.float64,
188+
"Int16": np.float64,
189+
"Int32": np.float64,
190+
"Int64": np.float64,
191+
"UInt8": np.float64,
192+
"UInt16": np.float64,
193+
"UInt32": np.float64,
194+
"UInt64": np.float64,
195+
}
196+
)
164197

165-
if (
166-
hasattr(array, "isna")
167-
and array.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(array.astype(float))
175-
else:
176-
vec_dtype = str(getattr(array, "dtype", ""))
177-
array = np.ascontiguousarray(array, dtype=dtypes.get(vec_dtype))
198+
vec_dtype = str(getattr(array, "dtype", ""))
199+
array = np.ascontiguousarray(array, dtype=dtypes.get(vec_dtype))
178200
return array
179201

180202

0 commit comments

Comments
 (0)