diff --git a/pyintegration/deephaven2/__init__.py b/pyintegration/deephaven2/__init__.py index 5219de1cf36..87476f2e193 100644 --- a/pyintegration/deephaven2/__init__.py +++ b/pyintegration/deephaven2/__init__.py @@ -17,6 +17,7 @@ from .dherror import DHError from .constants import SortDirection from .csv import read as read_csv +from .csv import write as write_csv from .table import empty_table, time_table -__all__ = ["read_csv", "DHError", "time_table", "empty_table", "SortDirection"] +__all__ = ["read_csv", "write_csv", "DHError", "time_table", "empty_table", "SortDirection"] diff --git a/pyintegration/deephaven2/column.py b/pyintegration/deephaven2/column.py index 58f0d4d419f..8668016ba3b 100644 --- a/pyintegration/deephaven2/column.py +++ b/pyintegration/deephaven2/column.py @@ -4,7 +4,7 @@ from dataclasses import dataclass -@dataclass +@dataclass(frozen=True) class Column: """ A Column object represents a column in a Deephaven Table. """ name: str diff --git a/pyintegration/deephaven2/csv.py b/pyintegration/deephaven2/csv.py index 3d55fb2241f..a4e448fce88 100644 --- a/pyintegration/deephaven2/csv.py +++ b/pyintegration/deephaven2/csv.py @@ -5,7 +5,7 @@ Deephaven table out as a CSV file. """ from enum import Enum -from typing import Dict, Any +from typing import Dict, Any, List import jpy @@ -112,4 +112,18 @@ def read(path: str, return Table(j_table=j_table) except Exception as e: - raise DHError(e, "read_csv failed") from e + raise DHError(e, "read csv failed") from e + + +def write(table: Table, path: str, cols: List[str] = []) -> None: + """ WriteC a table to a standard CSV file. + + Args: + table (Table): the source table + path (str): the path of the CSV file + cols (List[str]): the names of the columns to be written out + + Raises: + DHError + """ + table.write_csv(path, cols) diff --git a/pyintegration/deephaven2/table.py b/pyintegration/deephaven2/table.py index 7195d70f64b..03637da3d4c 100644 --- a/pyintegration/deephaven2/table.py +++ b/pyintegration/deephaven2/table.py @@ -93,6 +93,12 @@ def __repr__(self): def get_dh_table(self): return self._j_table + def write_csv(self: Table, path: str, cols: List[str] = []) -> None: + try: + _JTableTools.writeCsv(self._j_table, path, *cols) + except Exception as e: + raise DHError("write csv failed.") from e + @property def size(self) -> int: """ The current number of rows in the table. """ diff --git a/pyintegration/tests/test_csv.py b/pyintegration/tests/test_csv.py index 74a4554723e..6d2edeee48a 100644 --- a/pyintegration/tests/test_csv.py +++ b/pyintegration/tests/test_csv.py @@ -4,7 +4,7 @@ import unittest from deephaven2 import dtypes, DHError -from deephaven2 import read_csv +from deephaven2 import read_csv, write_csv from tests.testbase import BaseTestCase @@ -49,6 +49,24 @@ def test_read_error_quote(self): self.assertIsNotNone(cm.exception.compact_traceback) + def test_write(self): + t = read_csv("tests/data/small_sample.csv") + write_csv(t, "./test_write.csv") + t_cols = [col.name for col in t.columns] + t = read_csv("./test_write.csv") + self.assertEqual(t_cols, [col.name for col in t.columns]) + + col_names = ["Strings", "Longs", "Floats"] + col_types = [dtypes.string, dtypes.long, dtypes.float_] + table_header = {k: v for k, v in zip(col_names, col_types)} + t = read_csv('tests/data/test_csv.csv', header=table_header) + write_csv(t, "./test_write.csv", cols=col_names) + t = read_csv('./test_write.csv') + self.assertEqual(col_names, [c.name for c in t.columns]) + + import os + os.remove("./test_write.csv") + if __name__ == '__main__': unittest.main()