Skip to content

Commit 8e04d00

Browse files
seismanweiji14
andauthored
Fix the conversion error for pandas.Series with missing values in pandas<=2.1 (#3505)
Co-authored-by: Wei Ji <[email protected]>
1 parent 1d7cf5c commit 8e04d00

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

pygmt/clib/conversion.py

Lines changed: 20 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")
@@ -205,8 +211,20 @@ def vectors_to_arrays(vectors):
205211
}
206212
arrays = []
207213
for vector in vectors:
208-
vec_dtype = str(getattr(vector, "dtype", ""))
209-
arrays.append(np.ascontiguousarray(vector, dtype=dtypes.get(vec_dtype)))
214+
if (
215+
hasattr(vector, "isna")
216+
and vector.isna().any()
217+
and Version(pd.__version__) < Version("2.2")
218+
):
219+
# Workaround for dealing with pd.NA with pandas < 2.2.
220+
# Bug report at: https://github.com/GenericMappingTools/pygmt/issues/2844
221+
# Following SPEC0, pandas 2.1 will be dropped in 2025 Q3, so it's likely
222+
# we can remove the workaround in PyGMT v0.17.0.
223+
array = np.ascontiguousarray(vector.astype(float))
224+
else:
225+
vec_dtype = str(getattr(vector, "dtype", ""))
226+
array = np.ascontiguousarray(vector, dtype=dtypes.get(vec_dtype))
227+
arrays.append(array)
210228
return arrays
211229

212230

0 commit comments

Comments
 (0)