Skip to content

Commit 7b38109

Browse files
committed
Let pygmt.info load datetime columns into a str dtype array
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.
1 parent e057927 commit 7b38109

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

pygmt/src/info.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ def info(table, **kwargs):
9494
# instead of a raw string that is less useful.
9595
if result.startswith(("-R", "-T")): # e.g. -R0/1/2/3 or -T0/9/1
9696
result = result[2:].replace("/", " ")
97-
result = np.loadtxt(result.splitlines())
97+
try:
98+
result = np.loadtxt(result.splitlines())
99+
except ValueError:
100+
# Load non-numerical outputs in str type, e.g. for datetime
101+
result = np.loadtxt(result.splitlines(), dtype="str")
98102

99103
return result

pygmt/tests/test_info.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ def test_info_dataframe():
4141
assert output == expected_output
4242

4343

44+
def test_info_numpy_array_time_column():
45+
"""
46+
Make sure info works on a numpy.ndarray input with a datetime type.
47+
"""
48+
table = pd.date_range(start="2020-01-01", periods=5).to_numpy()
49+
output = info(table=table, f="0T")
50+
expected_output = (
51+
"<vector memory>: N = 5 <2020-01-01T00:00:00/2020-01-05T00:00:00>\n"
52+
)
53+
assert output == expected_output
54+
55+
4456
@pytest.mark.xfail(
4557
reason="UNIX timestamps returned instead of ISO datetime, should work on GMT 6.2.0 "
4658
"after https://github.com/GenericMappingTools/gmt/issues/4241 is resolved",
@@ -115,6 +127,17 @@ def test_info_per_column():
115127
)
116128

117129

130+
def test_info_per_column_with_time_inputs():
131+
"""
132+
Make sure the per_column option works with time inputs.
133+
"""
134+
table = pd.date_range(start="2020-01-01", periods=5).to_numpy()
135+
output = info(table=table, per_column=True, f="0T")
136+
npt.assert_equal(
137+
actual=output, desired=["2020-01-01T00:00:00", "2020-01-05T00:00:00"]
138+
)
139+
140+
118141
def test_info_spacing():
119142
"""
120143
Make sure the spacing option works.

0 commit comments

Comments
 (0)