diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index f76c8d6eccc..7d53566cb51 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -9,6 +9,7 @@ from packaging.version import Version import numpy as np +import pandas as pd from ..exceptions import ( GMTCLibError, @@ -1160,7 +1161,7 @@ def virtualfile_from_vectors(self, *vectors): # Assumes that first 2 columns contains coordinates like longitude # latitude, or datetime string types. for col, array in enumerate(arrays[2:]): - if np.issubdtype(array.dtype, np.str_): + if pd.api.types.is_string_dtype(array.dtype): columns = col + 2 break @@ -1189,6 +1190,7 @@ def virtualfile_from_vectors(self, *vectors): strings = np.apply_along_axis( func1d=" ".join, axis=0, arr=string_arrays ) + strings = np.asanyarray(a=strings, dtype=np.str) self.put_strings( dataset, family="GMT_IS_VECTOR|GMT_IS_DUPLICATE", strings=strings ) diff --git a/pygmt/tests/test_clib.py b/pygmt/tests/test_clib.py index 45dcc5da6db..1d15be613fe 100644 --- a/pygmt/tests/test_clib.py +++ b/pygmt/tests/test_clib.py @@ -397,12 +397,16 @@ def test_virtualfile_from_vectors(): assert output == expected -def test_virtualfile_from_vectors_one_string_column(): - "Test passing in one column with string dtype into virtual file dataset" +@pytest.mark.parametrize("dtype", [np.str, np.object]) +def test_virtualfile_from_vectors_one_string_or_object_column(dtype): + """ + Test passing in one column with string or object dtype into virtual file + dataset + """ size = 5 x = np.arange(size, dtype=np.int32) y = np.arange(size, size * 2, 1, dtype=np.int32) - strings = np.array(["a", "bc", "defg", "hijklmn", "opqrst"], dtype=np.str) + strings = np.array(["a", "bc", "defg", "hijklmn", "opqrst"], dtype=dtype) with clib.Session() as lib: with lib.virtualfile_from_vectors(x, y, strings) as vfile: with GMTTempFile() as outfile: @@ -412,13 +416,17 @@ def test_virtualfile_from_vectors_one_string_column(): assert output == expected -def test_virtualfile_from_vectors_two_string_columns(): - "Test passing in two columns of string dtype into virtual file dataset" +@pytest.mark.parametrize("dtype", [np.str, np.object]) +def test_virtualfile_from_vectors_two_string_or_object_columns(dtype): + """ + Test passing in two columns of string or object dtype into virtual file + dataset + """ size = 5 x = np.arange(size, dtype=np.int32) y = np.arange(size, size * 2, 1, dtype=np.int32) - strings1 = np.array(["a", "bc", "def", "ghij", "klmno"], dtype=np.str) - strings2 = np.array(["pqrst", "uvwx", "yz!", "@#", "$"], dtype=np.str) + strings1 = np.array(["a", "bc", "def", "ghij", "klmno"], dtype=dtype) + strings2 = np.array(["pqrst", "uvwx", "yz!", "@#", "$"], dtype=dtype) with clib.Session() as lib: with lib.virtualfile_from_vectors(x, y, strings1, strings2) as vfile: with GMTTempFile() as outfile: