Skip to content

Allow np.object dtypes into virtualfile_from_vectors #684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from packaging.version import Version
import numpy as np
import pandas as pd

from ..exceptions import (
GMTCLibError,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
)
Expand Down
22 changes: 15 additions & 7 deletions pygmt/tests/test_clib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down