Skip to content

Commit

Permalink
Allow np.object dtypes into virtualfile_from_vectors (#684)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
weiji14 authored Nov 7, 2020
1 parent 67538dd commit 1b01f79
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
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

0 comments on commit 1b01f79

Please sign in to comment.