Skip to content

Commit 6827c82

Browse files
committed
clib: Add the return_table method for output table data
1 parent a5d8b14 commit 6827c82

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

pygmt/clib/session.py

+36
Original file line numberDiff line numberDiff line change
@@ -1738,6 +1738,42 @@ def read_virtualfile(
17381738
dtype = {"dataset": _GMT_DATASET, "grid": _GMT_GRID}[kind]
17391739
return ctp.cast(pointer, ctp.POINTER(dtype))
17401740

1741+
def return_table(
1742+
self,
1743+
output_type: Literal["pandas", "numpy", "file"],
1744+
vfile: str,
1745+
column_names: list[str] | None = None,
1746+
) -> pd.DataFrame | np.ndarray | None:
1747+
"""
1748+
Return an output table from a virtual file based on the output type.
1749+
1750+
Parameters
1751+
----------
1752+
output_type
1753+
The output type. Valid values are ``"pandas"``, ``"numpy"``, or ``"file"``.
1754+
vfile
1755+
The virtual file name.
1756+
column_names
1757+
The column names for the :class:`pandas.DataFrame` output.
1758+
1759+
Returns
1760+
-------
1761+
:class:`pandas.DataFrame` or :class:`numpy.ndarray` or None
1762+
The output table. If ``output_type`` is ``"file"``, returns ``None``.
1763+
"""
1764+
if output_type == "file": # Already written to file, so return None
1765+
return None
1766+
# Read the virtual file as a GMT dataset and convert to pandas.DataFrame
1767+
result = self.read_virtualfile(vfile, kind="dataset").contents.to_dataframe()
1768+
# Assign column names
1769+
if column_names is not None:
1770+
result.columns = column_names
1771+
# Pandas.DataFrame output
1772+
if output_type == "pandas":
1773+
return result
1774+
# NumPy.ndarray output
1775+
return result.to_numpy()
1776+
17411777
def extract_region(self):
17421778
"""
17431779
Extract the WESN bounding box of the currently active figure.

0 commit comments

Comments
 (0)