Skip to content

Commit 3ad0c86

Browse files
weiji14seisman
andauthored
Use np.asarray to convert pa.StringArray instead of .to_pylist()
Faster for longer string arrays. Co-authored-by: Dongdong Tian <[email protected]>
1 parent 0105d64 commit 3ad0c86

File tree

1 file changed

+1
-6
lines changed

1 file changed

+1
-6
lines changed

pygmt/clib/conversion.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,7 @@ def strings_to_ctypes_array(strings: StringArrayTypes) -> ctp.Array:
306306
>>> [s.decode() for s in ctypes_array]
307307
['first', 'second', 'third']
308308
"""
309-
try:
310-
bytes_string_list = [s.encode() for s in strings]
311-
except AttributeError: # 'pyarrow.StringScalar' object has no attribute 'encode'
312-
# Convert pyarrow.StringArray to Python list first
313-
bytes_string_list = [s.encode() for s in strings.to_pylist()] # type: ignore[union-attr]
314-
return (ctp.c_char_p * len(strings))(*bytes_string_list)
309+
return (ctp.c_char_p * len(strings))(*[s.encode() for s in np.asarray(strings)])
315310

316311

317312
def array_to_datetime(array):

0 commit comments

Comments
 (0)