Skip to content

Commit a927202

Browse files
committed
Revert "Enable passing pyarrow.StringArray to clib.Session.put_strings"
This reverts commit d379e46.
1 parent 44d01ed commit a927202

File tree

4 files changed

+28
-50
lines changed

4 files changed

+28
-50
lines changed

pygmt/clib/conversion.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import pandas as pd
1212
import xarray as xr
1313
from packaging.version import Version
14-
from pygmt._typing import StringArrayTypes
1514
from pygmt.exceptions import GMTInvalidInput
1615

1716

@@ -279,15 +278,15 @@ def sequence_to_ctypes_array(
279278
return (ctype * size)(*sequence)
280279

281280

282-
def strings_to_ctypes_array(strings: StringArrayTypes) -> ctp.Array:
281+
def strings_to_ctypes_array(strings: Sequence[str] | np.ndarray) -> ctp.Array:
283282
"""
284-
Convert a sequence (e.g., a list) or numpy.ndarray of strings or a
285-
pyarrow.StringArray into a ctypes array.
283+
Convert a sequence (e.g., a list) of strings or numpy.ndarray of strings into a
284+
ctypes array.
286285
287286
Parameters
288287
----------
289288
strings
290-
A sequence of strings, a numpy.ndarray of str dtype, or a pyarrow.StringArray.
289+
A sequence of strings, or a numpy.ndarray of str dtype.
291290
292291
Returns
293292
-------
@@ -303,7 +302,7 @@ def strings_to_ctypes_array(strings: StringArrayTypes) -> ctp.Array:
303302
>>> [s.decode() for s in ctypes_array]
304303
['first', 'second', 'third']
305304
"""
306-
return (ctp.c_char_p * len(strings))(*[s.encode() for s in np.asarray(strings)])
305+
return (ctp.c_char_p * len(strings))(*[s.encode() for s in strings])
307306

308307

309308
def array_to_datetime(array: Sequence[Any] | np.ndarray) -> np.ndarray:

pygmt/clib/session.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import numpy as np
1717
import pandas as pd
1818
import xarray as xr
19-
from pygmt._typing import StringArrayTypes
2019
from pygmt.clib.conversion import (
2120
array_to_datetime,
2221
dataarray_to_matrix,
@@ -1000,40 +999,39 @@ def put_vector(self, dataset: ctp.c_void_p, column: int, vector: np.ndarray):
1000999
)
10011000
raise GMTCLibError(msg)
10021001

1003-
def put_strings(
1004-
self, dataset: ctp.c_void_p, family: str, strings: StringArrayTypes
1005-
):
1002+
def put_strings(self, dataset: ctp.c_void_p, family: str, strings: np.ndarray):
10061003
"""
1007-
Attach a 1-D numpy array of dtype str or pyarrow.StringArray as a column on a
1008-
GMT dataset.
1004+
Attach a numpy 1-D array of dtype str as a column on a GMT dataset.
10091005
1010-
Use this function to attach string type numpy array data to a GMT dataset and
1011-
pass it to GMT modules. Wraps ``GMT_Put_Strings``.
1006+
Use this function to attach string type numpy array data to a GMT
1007+
dataset and pass it to GMT modules. Wraps ``GMT_Put_Strings``.
10121008
1013-
The dataset must be created by :meth:`pygmt.clib.Session.create_data` first.
1009+
The dataset must be created by :meth:`pygmt.clib.Session.create_data`
1010+
first.
10141011
10151012
.. warning::
1016-
The numpy array must be C contiguous in memory. If it comes from a column
1017-
slice of a 2-D array, for example, you will have to make a copy. Use
1018-
:func:`numpy.ascontiguousarray` to make sure your vector is contiguous (it
1019-
won't copy if it already is).
1013+
The numpy array must be C contiguous in memory. If it comes from a
1014+
column slice of a 2-D array, for example, you will have to make a
1015+
copy. Use :func:`numpy.ascontiguousarray` to make sure your vector
1016+
is contiguous (it won't copy if it already is).
10201017
10211018
Parameters
10221019
----------
1023-
dataset
1024-
The ctypes void pointer to a ``GMT_VECTOR``/``GMT_MATRIX`` data container.
1025-
Create it with :meth:`pygmt.clib.Session.create_data`.
1026-
family
1020+
dataset : :class:`ctypes.c_void_p`
1021+
The ctypes void pointer to a ``GMT_Dataset``. Create it with
1022+
:meth:`pygmt.clib.Session.create_data`.
1023+
family : str
10271024
The family type of the dataset. Can be either ``GMT_IS_VECTOR`` or
10281025
``GMT_IS_MATRIX``.
1029-
strings
1030-
The array that will be attached to the dataset. Must be a 1-D C contiguous
1031-
array.
1026+
strings : numpy 1-D array
1027+
The array that will be attached to the dataset. Must be a 1-D C
1028+
contiguous array.
10321029
10331030
Raises
10341031
------
10351032
GMTCLibError
1036-
If given invalid input or ``GMT_Put_Strings`` exits with a non-zero status.
1033+
If given invalid input or ``GMT_Put_Strings`` exits with
1034+
status != 0.
10371035
"""
10381036
c_put_strings = self.get_libgmt_func(
10391037
"GMT_Put_Strings",

pygmt/tests/test_clib_put_strings.py

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,12 @@
88
from pygmt import clib
99
from pygmt.exceptions import GMTCLibError
1010
from pygmt.helpers import GMTTempFile
11-
from pygmt.helpers.testing import skip_if_no
12-
13-
try:
14-
import pyarrow as pa
15-
except ImportError:
16-
pa = None
1711

1812

1913
@pytest.mark.benchmark
20-
@pytest.mark.parametrize(
21-
("array_func", "dtype"),
22-
[
23-
pytest.param(np.array, {"dtype": np.str_}, id="str"),
24-
pytest.param(
25-
getattr(pa, "array", None),
26-
{"type": "string"}, # pa.string()
27-
marks=skip_if_no(package="pyarrow"),
28-
id="pyarrow",
29-
),
30-
],
31-
)
32-
def test_put_strings(array_func, dtype):
14+
def test_put_strings():
3315
"""
34-
Check that assigning a numpy array of dtype str, or a pyarrow.StringArray to a
35-
dataset works.
16+
Check that assigning a numpy array of dtype str to a dataset works.
3617
"""
3718
with clib.Session() as lib:
3819
dataset = lib.create_data(
@@ -43,7 +24,7 @@ def test_put_strings(array_func, dtype):
4324
)
4425
x = np.array([1, 2, 3, 4, 5], dtype=np.int32)
4526
y = np.array([6, 7, 8, 9, 10], dtype=np.int32)
46-
strings = array_func(["a", "bc", "defg", "hijklmn", "opqrst"], **dtype)
27+
strings = np.array(["a", "bc", "defg", "hijklmn", "opqrst"], dtype=str)
4728
lib.put_vector(dataset, column=lib["GMT_X"], vector=x)
4829
lib.put_vector(dataset, column=lib["GMT_Y"], vector=y)
4930
lib.put_strings(

pygmt/tests/test_clib_virtualfiles.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def test_open_virtual_file():
153153
pytest.param(np.array, {"dtype": np.object_}, id="object"),
154154
pytest.param(
155155
getattr(pa, "array", None),
156-
{"type": "string"}, # pa.string()
156+
{}, # pa.string()
157157
marks=skip_if_no(package="pyarrow"),
158158
id="pyarrow",
159159
),

0 commit comments

Comments
 (0)