Skip to content

Commit a4d414a

Browse files
weiji14seisman
andauthored
Refactor info to allow datetime inputs from xarray.Dataset and pandas.DataFrame tables (#619)
Changes the backend mechanism of `info` from using lib.virtualfile_from_matrix() (which only supports single non-datetime dtypes) to using lib.virtualfile_from_vectors() (which supports datetime inputs as of #464). * Refactor info to use virtualfile_from_vectors to support datetime inputs * Test that xarray.Dataset inputs into pygmt.info works too * Expect failures on test_info_*_time_column on GMT 6.1.1 * Document xarray.Datasets with 1D data_vars as allowed inputs to info Co-authored-by: Dongdong Tian <[email protected]>
1 parent 9e0a868 commit a4d414a

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

pygmt/modules.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ def info(table, **kwargs):
7878
7979
Parameters
8080
----------
81-
table : pandas.DataFrame or np.ndarray or str
82-
Either a pandas dataframe, a 1D/2D numpy.ndarray or a file name to an
83-
ASCII data table.
81+
table : str or np.ndarray or pandas.DataFrame or xarray.Dataset
82+
Pass in either a file name to an ASCII data table, a 1D/2D numpy array,
83+
a pandas dataframe, or an xarray dataset made up of 1D xarray.DataArray
84+
data variables.
8485
per_column : bool
8586
Report the min/max values per column in separate columns.
8687
spacing : str
@@ -107,10 +108,13 @@ def info(table, **kwargs):
107108
if kind == "file":
108109
file_context = dummy_context(table)
109110
elif kind == "matrix":
110-
_table = np.asanyarray(table)
111-
if table.ndim == 1: # 1D arrays need to be 2D and transposed
112-
_table = np.transpose(np.atleast_2d(_table))
113-
file_context = lib.virtualfile_from_matrix(_table)
111+
try:
112+
# pandas.DataFrame and xarray.Dataset types
113+
arrays = [array for _, array in table.items()]
114+
except AttributeError:
115+
# Python lists, tuples, and numpy ndarray types
116+
arrays = np.atleast_2d(np.asanyarray(table).T)
117+
file_context = lib.virtualfile_from_vectors(*arrays)
114118
else:
115119
raise GMTInvalidInput(f"Unrecognized data type: {type(table)}")
116120

pygmt/tests/test_info.py

+49-4
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88
import pandas as pd
99
import pytest
1010
import xarray as xr
11+
from packaging.version import Version
1112

12-
from .. import info
13+
from .. import clib, info
1314
from ..exceptions import GMTInvalidInput
1415

1516
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
1617
POINTS_DATA = os.path.join(TEST_DATA_DIR, "points.txt")
1718

19+
with clib.Session() as _lib:
20+
gmt_version = Version(_lib.info["version"])
21+
1822

1923
def test_info():
2024
"Make sure info works on file name inputs"
@@ -33,7 +37,48 @@ def test_info_dataframe():
3337
table = pd.read_csv(POINTS_DATA, sep=" ", header=None)
3438
output = info(table=table)
3539
expected_output = (
36-
"<matrix memory>: N = 20 <11.5309/61.7074> <-2.9289/7.8648> <0.1412/0.9338>\n"
40+
"<vector memory>: N = 20 <11.5309/61.7074> <-2.9289/7.8648> <0.1412/0.9338>\n"
41+
)
42+
assert output == expected_output
43+
44+
45+
@pytest.mark.xfail(
46+
condition=gmt_version <= Version("6.1.1"),
47+
reason="UNIX timestamps returned instead of ISO datetime, should work on GMT 6.2.0 "
48+
"after https://github.com/GenericMappingTools/gmt/issues/4241 is resolved",
49+
)
50+
def test_info_pandas_dataframe_time_column():
51+
"Make sure info works on pandas.DataFrame inputs with a time column"
52+
table = pd.DataFrame(
53+
data={
54+
"z": [10, 13, 12, 15, 14],
55+
"time": pd.date_range(start="2020-01-01", periods=5),
56+
}
57+
)
58+
output = info(table=table)
59+
expected_output = (
60+
"<vector memory>: N = 5 <10/15> <2020-01-01T00:00:00/2020-01-05T00:00:00>\n"
61+
)
62+
assert output == expected_output
63+
64+
65+
@pytest.mark.xfail(
66+
condition=gmt_version <= Version("6.1.1"),
67+
reason="UNIX timestamp returned instead of ISO datetime, should work on GMT 6.2.0 "
68+
"after https://github.com/GenericMappingTools/gmt/issues/4241 is resolved",
69+
)
70+
def test_info_xarray_dataset_time_column():
71+
"Make sure info works on xarray.Dataset 1D inputs with a time column"
72+
table = xr.Dataset(
73+
coords={"index": [0, 1, 2, 3, 4]},
74+
data_vars={
75+
"z": ("index", [10, 13, 12, 15, 14]),
76+
"time": ("index", pd.date_range(start="2020-01-01", periods=5)),
77+
},
78+
)
79+
output = info(table=table)
80+
expected_output = (
81+
"<vector memory>: N = 5 <10/15> <2020-01-01T00:00:00/2020-01-05T00:00:00>\n"
3782
)
3883
assert output == expected_output
3984

@@ -43,15 +88,15 @@ def test_info_2d_array():
4388
table = np.loadtxt(POINTS_DATA)
4489
output = info(table=table)
4590
expected_output = (
46-
"<matrix memory>: N = 20 <11.5309/61.7074> <-2.9289/7.8648> <0.1412/0.9338>\n"
91+
"<vector memory>: N = 20 <11.5309/61.7074> <-2.9289/7.8648> <0.1412/0.9338>\n"
4792
)
4893
assert output == expected_output
4994

5095

5196
def test_info_1d_array():
5297
"Make sure info works on 1D numpy.ndarray inputs"
5398
output = info(table=np.arange(20))
54-
expected_output = "<matrix memory>: N = 20 <0/19>\n"
99+
expected_output = "<vector memory>: N = 20 <0/19>\n"
55100
assert output == expected_output
56101

57102

0 commit comments

Comments
 (0)