Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the conversion error for pandas.Series with missing values in pandas<=2.1 #3505

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions pygmt/clib/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from collections.abc import Sequence

import numpy as np
import pandas as pd
from packaging.version import Version
from pygmt.exceptions import GMTInvalidInput


Expand Down Expand Up @@ -178,6 +180,10 @@ def vectors_to_arrays(vectors):
>>> [i.ndim for i in data] # Check that they are 1-D arrays
[1, 1, 1]

>>> series = pd.Series(data=[0, 4, pd.NA, 8, 6], dtype=pd.Int32Dtype())
>>> vectors_to_arrays([series])
[array([ 0., 4., nan, 8., 6.])]

>>> import datetime
>>> import pytest
>>> pa = pytest.importorskip("pyarrow")
Expand Down Expand Up @@ -205,8 +211,20 @@ def vectors_to_arrays(vectors):
}
arrays = []
for vector in vectors:
vec_dtype = str(getattr(vector, "dtype", ""))
arrays.append(np.ascontiguousarray(vector, dtype=dtypes.get(vec_dtype)))
if (
hasattr(vector, "isna")
and vector.isna().any()
and Version(pd.__version__) < Version("2.2")
):
# Workaround for dealing with pd.NA with pandas < 2.2.
# Bug report at: https://github.com/GenericMappingTools/pygmt/issues/2844
# Following SPEC0, pandas 2.1 will be dropped in 2025 Q3, so it's likey
seisman marked this conversation as resolved.
Show resolved Hide resolved
# we can remove the workaround in PyGMT v0.17.0.
array = np.ascontiguousarray(vector.astype(float))
else:
vec_dtype = str(getattr(vector, "dtype", ""))
array = np.ascontiguousarray(vector, dtype=dtypes.get(vec_dtype))
arrays.append(array)
return arrays


Expand Down
Loading