Skip to content

Commit 5d4bff5

Browse files
committed
Add workaround for pandas.NA support with pandas<=2.1
1 parent 7a51128 commit 5d4bff5

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

pygmt/clib/conversion.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from collections.abc import Sequence
88

99
import numpy as np
10+
import pandas as pd
11+
from packaging.version import Version
1012
from pygmt.exceptions import GMTInvalidInput
1113

1214

@@ -178,6 +180,10 @@ def vectors_to_arrays(vectors):
178180
>>> [i.ndim for i in data] # Check that they are 1-D arrays
179181
[1, 1, 1]
180182
183+
>>> series = pd.Series(data=[0, 4, pd.NA, 8, 6], dtype=pd.Int32Dtype())
184+
>>> vectors_to_arrays([series])
185+
[array([ 0., 4., nan, 8., 6.])]
186+
181187
>>> import datetime
182188
>>> import pytest
183189
>>> pa = pytest.importorskip("pyarrow")
@@ -198,15 +204,28 @@ def vectors_to_arrays(vectors):
198204
True
199205
>>> all(isinstance(a.dtype, np.dtypes.DateTime64DType) for a in arrays)
200206
True
207+
201208
"""
202209
dtypes = {
203210
"date32[day][pyarrow]": np.datetime64,
204211
"date64[ms][pyarrow]": np.datetime64,
205212
}
206213
arrays = []
207214
for vector in vectors:
208-
vec_dtype = str(getattr(vector, "dtype", ""))
209-
arrays.append(np.ascontiguousarray(vector, dtype=dtypes.get(vec_dtype)))
215+
if (
216+
hasattr(vector, "isna")
217+
and vector.isna().any()
218+
and Version(pd.__version__) < Version("2.2")
219+
):
220+
# Workaround for dealing with pd.NA with pandas < 2.2.
221+
# Bug report at: https://github.com/GenericMappingTools/pygmt/issues/2844
222+
# Following SPEC0, pandas 2.1 will be dropped in 2025 Q3, so it's likey
223+
# we can remove the workaround in PyGMT v0.17.0.
224+
array = np.ascontiguousarray(vector.astype(float))
225+
else:
226+
vec_dtype = str(getattr(vector, "dtype", ""))
227+
array = np.ascontiguousarray(vector, dtype=dtypes.get(vec_dtype))
228+
arrays.append(array)
210229
return arrays
211230

212231

0 commit comments

Comments
 (0)