diff --git a/pygmt/clib/conversion.py b/pygmt/clib/conversion.py index e924fad5332..092ccab11bb 100644 --- a/pygmt/clib/conversion.py +++ b/pygmt/clib/conversion.py @@ -9,11 +9,16 @@ import numpy as np from pygmt.exceptions import GMTInvalidInput +StringArrayTypes = Sequence[str] + try: import pyarrow as pa + + StringArrayTypes |= pa.StringArray except ImportError: pa = None + def dataarray_to_matrix(grid): """ Transform an xarray.DataArray into a data 2-D array and metadata. @@ -267,7 +272,7 @@ def sequence_to_ctypes_array( return (ctype * size)(*sequence) -def strings_to_ctypes_array(strings: Sequence[str] | pa.StringArray) -> ctp.Array: +def strings_to_ctypes_array(strings: StringArrayTypes) -> ctp.Array: """ Convert a sequence (e.g., a list) of strings or a pyarrow.StringArray into a ctypes array. diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index 959baac1540..fe55d241536 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -34,11 +34,16 @@ tempfile_from_image, ) +StringArrayTypes = Sequence[str] + try: import pyarrow as pa + + StringArrayTypes |= pa.StringArray except ImportError: pa = None + FAMILIES = [ "GMT_IS_DATASET", # Entity is a data table "GMT_IS_GRID", # Entity is a grid @@ -945,7 +950,7 @@ def put_strings( self, dataset: ctp.c_void_p, family: Literal["GMT_IS_VECTOR", "GMT_IS_MATRIX"], - strings: Sequence[str] | pa.StringArray, + strings: StringArrayTypes, ): """ Attach a 1-D numpy array of dtype str or pyarrow.StringArray as a column on a