Skip to content

Commit 1b01f79

Browse files
authored
Allow np.object dtypes into virtualfile_from_vectors (#684)
Loosen the check in `virtualfile_from_vectors` to allow for any string-like dtype (np.str, np.object) by performing the check using `pd.api.types.is_string_dtype()`. The array is then converted (if needed) to a proper `np.str` dtype before giving it to `put_strings`.
1 parent 67538dd commit 1b01f79

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

pygmt/clib/session.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from packaging.version import Version
1111
import numpy as np
12+
import pandas as pd
1213

1314
from ..exceptions import (
1415
GMTCLibError,
@@ -1160,7 +1161,7 @@ def virtualfile_from_vectors(self, *vectors):
11601161
# Assumes that first 2 columns contains coordinates like longitude
11611162
# latitude, or datetime string types.
11621163
for col, array in enumerate(arrays[2:]):
1163-
if np.issubdtype(array.dtype, np.str_):
1164+
if pd.api.types.is_string_dtype(array.dtype):
11641165
columns = col + 2
11651166
break
11661167

@@ -1189,6 +1190,7 @@ def virtualfile_from_vectors(self, *vectors):
11891190
strings = np.apply_along_axis(
11901191
func1d=" ".join, axis=0, arr=string_arrays
11911192
)
1193+
strings = np.asanyarray(a=strings, dtype=np.str)
11921194
self.put_strings(
11931195
dataset, family="GMT_IS_VECTOR|GMT_IS_DUPLICATE", strings=strings
11941196
)

pygmt/tests/test_clib.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,12 +397,16 @@ def test_virtualfile_from_vectors():
397397
assert output == expected
398398

399399

400-
def test_virtualfile_from_vectors_one_string_column():
401-
"Test passing in one column with string dtype into virtual file dataset"
400+
@pytest.mark.parametrize("dtype", [np.str, np.object])
401+
def test_virtualfile_from_vectors_one_string_or_object_column(dtype):
402+
"""
403+
Test passing in one column with string or object dtype into virtual file
404+
dataset
405+
"""
402406
size = 5
403407
x = np.arange(size, dtype=np.int32)
404408
y = np.arange(size, size * 2, 1, dtype=np.int32)
405-
strings = np.array(["a", "bc", "defg", "hijklmn", "opqrst"], dtype=np.str)
409+
strings = np.array(["a", "bc", "defg", "hijklmn", "opqrst"], dtype=dtype)
406410
with clib.Session() as lib:
407411
with lib.virtualfile_from_vectors(x, y, strings) as vfile:
408412
with GMTTempFile() as outfile:
@@ -412,13 +416,17 @@ def test_virtualfile_from_vectors_one_string_column():
412416
assert output == expected
413417

414418

415-
def test_virtualfile_from_vectors_two_string_columns():
416-
"Test passing in two columns of string dtype into virtual file dataset"
419+
@pytest.mark.parametrize("dtype", [np.str, np.object])
420+
def test_virtualfile_from_vectors_two_string_or_object_columns(dtype):
421+
"""
422+
Test passing in two columns of string or object dtype into virtual file
423+
dataset
424+
"""
417425
size = 5
418426
x = np.arange(size, dtype=np.int32)
419427
y = np.arange(size, size * 2, 1, dtype=np.int32)
420-
strings1 = np.array(["a", "bc", "def", "ghij", "klmno"], dtype=np.str)
421-
strings2 = np.array(["pqrst", "uvwx", "yz!", "@#", "$"], dtype=np.str)
428+
strings1 = np.array(["a", "bc", "def", "ghij", "klmno"], dtype=dtype)
429+
strings2 = np.array(["pqrst", "uvwx", "yz!", "@#", "$"], dtype=dtype)
422430
with clib.Session() as lib:
423431
with lib.virtualfile_from_vectors(x, y, strings1, strings2) as vfile:
424432
with GMTTempFile() as outfile:

0 commit comments

Comments
 (0)