From 5d4bff56115e3575f4ed315c105f91f36ea06679 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 11 Oct 2024 00:41:42 +0800 Subject: [PATCH 1/3] Add workaround for pandas.NA support with pandas<=2.1 --- pygmt/clib/conversion.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/pygmt/clib/conversion.py b/pygmt/clib/conversion.py index 0739d767567..0d3b10149b8 100644 --- a/pygmt/clib/conversion.py +++ b/pygmt/clib/conversion.py @@ -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 @@ -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") @@ -198,6 +204,7 @@ def vectors_to_arrays(vectors): True >>> all(isinstance(a.dtype, np.dtypes.DateTime64DType) for a in arrays) True + """ dtypes = { "date32[day][pyarrow]": np.datetime64, @@ -205,8 +212,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 + # 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 From e110015d1d3ca1aa1a6537f86a0c4f28258cc795 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 11 Oct 2024 09:56:29 +0800 Subject: [PATCH 2/3] Remove a blank line --- pygmt/clib/conversion.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pygmt/clib/conversion.py b/pygmt/clib/conversion.py index 0d3b10149b8..446627d0b1c 100644 --- a/pygmt/clib/conversion.py +++ b/pygmt/clib/conversion.py @@ -204,7 +204,6 @@ def vectors_to_arrays(vectors): True >>> all(isinstance(a.dtype, np.dtypes.DateTime64DType) for a in arrays) True - """ dtypes = { "date32[day][pyarrow]": np.datetime64, From aa5d839f7c151f8ea99bcb515a40e58d6d0f64ac Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 11 Oct 2024 10:33:09 +0800 Subject: [PATCH 3/3] Fix a typo [skip ci] Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/clib/conversion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/clib/conversion.py b/pygmt/clib/conversion.py index 446627d0b1c..ee80ec1a187 100644 --- a/pygmt/clib/conversion.py +++ b/pygmt/clib/conversion.py @@ -218,7 +218,7 @@ def vectors_to_arrays(vectors): ): # 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 + # Following SPEC0, pandas 2.1 will be dropped in 2025 Q3, so it's likely # we can remove the workaround in PyGMT v0.17.0. array = np.ascontiguousarray(vector.astype(float)) else: