Skip to content

Commit

Permalink
Transition to python using native python time types instead of Java t…
Browse files Browse the repository at this point in the history
…ypes (#4388)

* Implementing new time conversions.

* Code formatting

* Better error handling

* Support string conversions.

* Added docs strings and removed deprecated methods.

* Cleanup

* Docs

* Cleanup

* Unit testing

* Unit testing

* Unit testing

* Unit testing

* Unit testing

* Unit testing

* Unit testing

* Todos.  Docs.

* Todos.  Docs.

* Unit test fixes

* Unit test fixes.  Support other methods that accept other time types.

* Addressing review comments

* Addressing review comments

* Addressing review comments

* Addressing review comments

* Addressing review comments

* Addressing review comments

* Addressing review comments.  Support more types.

* Addressing review comments.  Code format.

* Addressing review comments.

* Addressing review comments.

* Addressing review comments.

* Update py/server/deephaven/time.py

Co-authored-by: JJ Brosnan <84038776+jjbrosnan@users.noreply.github.com>

* Addressing review comments.

* Addressing review comments.

* Addressing review comments.

* Addressing review comments.

* Addressing review comments.

* Addressing review comments.

* Removed todos

* Addressing review.  Removing unneeded jpy imports.

* Addressing review.

* Addressing review.  Reduce exception depth.

---------

Co-authored-by: JJ Brosnan <84038776+jjbrosnan@users.noreply.github.com>
  • Loading branch information
chipkent and jjbrosnan authored Sep 1, 2023
1 parent c200c38 commit 7a6060a
Show file tree
Hide file tree
Showing 17 changed files with 1,104 additions and 2,569 deletions.
6 changes: 4 additions & 2 deletions py/server/deephaven/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import jpy

import deephaven.dtypes as dtypes
from deephaven import DHError
from deephaven import DHError, time
from deephaven.dtypes import DType

_JColumnHeader = jpy.get_type("io.deephaven.qst.column.header.ColumnHeader")
Expand Down Expand Up @@ -196,11 +196,13 @@ def datetime_col(name: str, data: Sequence) -> InputColumn:
Args:
name (str): the column name
data (Any): a sequence of Datetime instances
data (Any): a sequence of Datetime instances or values that can be converted to Datetime instances
(e.g. Instant, int nanoseconds since the Epoch, str, datetime.datetime, numpy.datetime64, pandas.Timestamp).
Returns:
a new input column
"""
data = [time.to_j_instant(d) for d in data]
return InputColumn(name=name, data_type=dtypes.Instant, input_data=data)


Expand Down
22 changes: 12 additions & 10 deletions py/server/deephaven/replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
from typing import Union
import jpy

import datetime
import numpy as np
import pandas as pd

from deephaven import dtypes, DHError, time
from deephaven._wrapper import JObjectWrapper
from deephaven.table import Table
Expand All @@ -23,23 +27,21 @@ class TableReplayer(JObjectWrapper):

j_object_type = _JReplayer

def __init__(self, start_time: Union[dtypes.Instant,str], end_time: Union[dtypes.Instant,str]):
def __init__(self, start_time: Union[dtypes.Instant, int, str, datetime.datetime, np.datetime64, pd.Timestamp],
end_time: Union[dtypes.Instant, int, str, datetime.datetime, np.datetime64, pd.Timestamp]):
"""Initializes the replayer.
Args:
start_time (DateTime): replay start time
end_time (DateTime): replay end time
start_time (Union[dtypes.Instant, int, str, datetime.datetime, np.datetime64, pd.Timestamp]):
replay start time. Integer values are nanoseconds since the Epoch.
end_time (Union[dtypes.Instant, int, str, datetime.datetime, np.datetime64, pd.Timestamp]):
replay end time. Integer values are nanoseconds since the Epoch.
Raises:
DHError
"""

if isinstance(start_time, str):
start_time = time.parse_instant(start_time)

if isinstance(end_time, str):
end_time = time.parse_instant(end_time)

start_time = time.to_j_instant(start_time)
end_time = time.to_j_instant(end_time)
self.start_time = start_time
self.end_time = end_time
try:
Expand Down
29 changes: 22 additions & 7 deletions py/server/deephaven/table_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
""" This module provides various ways to make a Deephaven table. """

from typing import List, Dict, Any, Union, Sequence
import datetime

import jpy
import numpy as np
import pandas as pd

from deephaven import DHError
from deephaven import DHError, time
from deephaven._wrapper import JObjectWrapper
from deephaven.column import InputColumn, Column
from deephaven.dtypes import DType
from deephaven.dtypes import DType, Duration, Instant
from deephaven.jcompat import to_sequence
from deephaven.table import Table
from deephaven.update_graph import auto_locking_ctx
Expand Down Expand Up @@ -47,14 +50,18 @@ def empty_table(size: int) -> Table:
raise DHError(e, "failed to create an empty table.") from e


def time_table(period: Union[str, int], start_time: str = None, blink_table: bool = False) -> Table:
def time_table(period: Union[Duration, int, str, datetime.timedelta, np.timedelta64, pd.Timedelta],
start_time: Union[None, Instant, int, str, datetime.datetime, np.datetime64, pd.Timestamp] = None,
blink_table: bool = False) -> Table:
"""Creates a table that adds a new row on a regular interval.
Args:
period (Union[str, int]): time interval between new row additions, can be expressed as an integer in
nanoseconds or a time interval string, e.g. "PT00:00:00.001" or "PT1s"
start_time (str, optional): start time for adding new rows, defaults to None which means use the current time
as the start time
period (Union[dtypes.Duration, int, str, datetime.timedelta, np.timedelta64, pd.Timedelta]):
time interval between new row additions, can be expressed as an integer in nanoseconds,
a time interval string, e.g. "PT00:00:00.001" or "PT1s", or other time duration types.
start_time (Union[None, str, datetime.datetime, np.datetime64], optional):
start time for adding new rows, defaults to None which means use the current time
as the start time.
blink_table (bool, optional): if the time table should be a blink table, defaults to False
Returns:
Expand All @@ -65,11 +72,19 @@ def time_table(period: Union[str, int], start_time: str = None, blink_table: boo
"""
try:
builder = _JTableTools.timeTableBuilder()

if not isinstance(period, str) and not isinstance(period, int):
period = time.to_j_duration(period)

builder.period(period)

if start_time:
start_time = time.to_j_instant(start_time)
builder.startTime(start_time)

if blink_table:
builder.blinkTable(blink_table)

return Table(j_table=builder.build())
except Exception as e:
raise DHError(e, "failed to create a time table.") from e
Expand Down
Loading

0 comments on commit 7a6060a

Please sign in to comment.