Skip to content

Commit

Permalink
Let pygmt.info load datetime columns into a str dtype array
Browse files Browse the repository at this point in the history
Fixes problem with pygmt.info not being able to handle datetime64 inputs.
I.e. `ValueError: could not convert string to float: '2021-01-01T12:34:56'`
However, users will still need to use`pygmt.info(..., f="0T")` until
upstream issue at GenericMappingTools/gmt#4241
is resolved. Also added two extra unit tests using numpy datetime64 types.
  • Loading branch information
weiji14 committed Feb 24, 2021
1 parent e057927 commit 7b38109
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pygmt/src/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def info(table, **kwargs):
# instead of a raw string that is less useful.
if result.startswith(("-R", "-T")): # e.g. -R0/1/2/3 or -T0/9/1
result = result[2:].replace("/", " ")
result = np.loadtxt(result.splitlines())
try:
result = np.loadtxt(result.splitlines())
except ValueError:
# Load non-numerical outputs in str type, e.g. for datetime
result = np.loadtxt(result.splitlines(), dtype="str")

return result
23 changes: 23 additions & 0 deletions pygmt/tests/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ def test_info_dataframe():
assert output == expected_output


def test_info_numpy_array_time_column():
"""
Make sure info works on a numpy.ndarray input with a datetime type.
"""
table = pd.date_range(start="2020-01-01", periods=5).to_numpy()
output = info(table=table, f="0T")
expected_output = (
"<vector memory>: N = 5 <2020-01-01T00:00:00/2020-01-05T00:00:00>\n"
)
assert output == expected_output


@pytest.mark.xfail(
reason="UNIX timestamps returned instead of ISO datetime, should work on GMT 6.2.0 "
"after https://github.com/GenericMappingTools/gmt/issues/4241 is resolved",
Expand Down Expand Up @@ -115,6 +127,17 @@ def test_info_per_column():
)


def test_info_per_column_with_time_inputs():
"""
Make sure the per_column option works with time inputs.
"""
table = pd.date_range(start="2020-01-01", periods=5).to_numpy()
output = info(table=table, per_column=True, f="0T")
npt.assert_equal(
actual=output, desired=["2020-01-01T00:00:00", "2020-01-05T00:00:00"]
)


def test_info_spacing():
"""
Make sure the spacing option works.
Expand Down

0 comments on commit 7b38109

Please sign in to comment.