|
7 | 7 | from collections.abc import Sequence
|
8 | 8 |
|
9 | 9 | import numpy as np
|
| 10 | +import pandas as pd |
| 11 | +from packaging.version import Version |
10 | 12 | from pygmt.exceptions import GMTInvalidInput
|
11 | 13 |
|
12 | 14 |
|
@@ -178,6 +180,10 @@ def vectors_to_arrays(vectors):
|
178 | 180 | >>> [i.ndim for i in data] # Check that they are 1-D arrays
|
179 | 181 | [1, 1, 1]
|
180 | 182 |
|
| 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 | +
|
181 | 187 | >>> import datetime
|
182 | 188 | >>> import pytest
|
183 | 189 | >>> pa = pytest.importorskip("pyarrow")
|
@@ -205,8 +211,20 @@ def vectors_to_arrays(vectors):
|
205 | 211 | }
|
206 | 212 | arrays = []
|
207 | 213 | 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) |
210 | 228 | return arrays
|
211 | 229 |
|
212 | 230 |
|
|
0 commit comments