diff --git a/pygmt/src/info.py b/pygmt/src/info.py index ca059952e57..34a7a59787e 100644 --- a/pygmt/src/info.py +++ b/pygmt/src/info.py @@ -74,6 +74,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 diff --git a/pygmt/tests/test_info.py b/pygmt/tests/test_info.py index 4c8884e17f6..1c40657ea1d 100644 --- a/pygmt/tests/test_info.py +++ b/pygmt/tests/test_info.py @@ -41,6 +41,20 @@ 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() + # Please remove coltypes="0T" workaround after + # https://github.com/GenericMappingTools/gmt/issues/4241 is resolved + output = info(table=table, coltypes="0T") + expected_output = ( + ": 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", @@ -115,6 +129,19 @@ 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() + # Please remove coltypes="0T" workaround after + # https://github.com/GenericMappingTools/gmt/issues/4241 is resolved + output = info(table=table, per_column=True, coltypes="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.