@@ -1760,6 +1760,78 @@ def return_table(
1760
1760
-------
1761
1761
:class:`pandas.DataFrame` or :class:`numpy.ndarray` or None
1762
1762
The output table. If ``output_type`` is ``"file"``, returns ``None``.
1763
+
1764
+ Examples
1765
+ --------
1766
+ >>> from pathlib import Path
1767
+ >>> import numpy as np
1768
+ >>> import pandas as pd
1769
+ >>>
1770
+ >>> from pygmt.helpers import GMTTempFile
1771
+ >>> from pygmt.clib import Session
1772
+ >>>
1773
+ >>> with GMTTempFile(suffix=".txt") as tmpfile:
1774
+ ... # prepare the sample data file
1775
+ ... with open(tmpfile.name, mode="w") as fp:
1776
+ ... print(">", file=fp)
1777
+ ... print("1.0 2.0 3.0 TEXT1 TEXT23", file=fp)
1778
+ ... print("4.0 5.0 6.0 TEXT4 TEXT567", file=fp)
1779
+ ... print(">", file=fp)
1780
+ ... print("7.0 8.0 9.0 TEXT8 TEXT90", file=fp)
1781
+ ... print("10.0 11.0 12.0 TEXT123 TEXT456789", file=fp)
1782
+ ...
1783
+ ... # file output
1784
+ ... with Session() as lib:
1785
+ ... with GMTTempFile(suffix=".txt") as outtmp:
1786
+ ... with lib.virtualfile_out(
1787
+ ... kind="dataset", fname=outtmp.name
1788
+ ... ) as vouttbl:
1789
+ ... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
1790
+ ... result = lib.return_table(output_type="file", vfile=vouttbl)
1791
+ ... assert result is None
1792
+ ... assert Path(outtmp.name).stat().st_size > 0
1793
+ ...
1794
+ ... # numpy output
1795
+ ... with Session() as lib:
1796
+ ... with lib.virtualfile_out(kind="dataset") as vouttbl:
1797
+ ... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
1798
+ ... outnp = lib.return_table(output_type="numpy", vfile=vouttbl)
1799
+ ... assert isinstance(outnp, np.ndarray)
1800
+ ...
1801
+ ... # pandas output
1802
+ ... with Session() as lib:
1803
+ ... with lib.virtualfile_out(kind="dataset") as vouttbl:
1804
+ ... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
1805
+ ... outpd = lib.return_table(output_type="pandas", vfile=vouttbl)
1806
+ ... assert isinstance(outpd, pd.DataFrame)
1807
+ ...
1808
+ ... # pandas output with specified column names
1809
+ ... with Session() as lib:
1810
+ ... with lib.virtualfile_out(kind="dataset") as vouttbl:
1811
+ ... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
1812
+ ... outpd2 = lib.return_table(
1813
+ ... output_type="pandas",
1814
+ ... vfile=vouttbl,
1815
+ ... column_names=["col1", "col2", "col3", "coltext"],
1816
+ ... )
1817
+ ... assert isinstance(outpd2, pd.DataFrame)
1818
+ >>> outnp
1819
+ array([[1.0, 2.0, 3.0, 'TEXT1 TEXT23'],
1820
+ [4.0, 5.0, 6.0, 'TEXT4 TEXT567'],
1821
+ [7.0, 8.0, 9.0, 'TEXT8 TEXT90'],
1822
+ [10.0, 11.0, 12.0, 'TEXT123 TEXT456789']], dtype=object)
1823
+ >>> outpd
1824
+ 0 1 2 3
1825
+ 0 1.0 2.0 3.0 TEXT1 TEXT23
1826
+ 1 4.0 5.0 6.0 TEXT4 TEXT567
1827
+ 2 7.0 8.0 9.0 TEXT8 TEXT90
1828
+ 3 10.0 11.0 12.0 TEXT123 TEXT456789
1829
+ >>> outpd2
1830
+ col1 col2 col3 coltext
1831
+ 0 1.0 2.0 3.0 TEXT1 TEXT23
1832
+ 1 4.0 5.0 6.0 TEXT4 TEXT567
1833
+ 2 7.0 8.0 9.0 TEXT8 TEXT90
1834
+ 3 10.0 11.0 12.0 TEXT123 TEXT456789
1763
1835
"""
1764
1836
if output_type == "file" : # Already written to file, so return None
1765
1837
return None
0 commit comments