Skip to main content
Version: Python

Time operations cheat sheet

Formats

hh:mm:ss

Deephaven time tables use timestamps in the hh:mm:ss format to specify ticking intervals.

from deephaven import time_table

result = time_table('00:00:01')

yyyy-mm-ddT:hh:mm:ss.[millis|micros|nanos] TZ

Deephaven time tables may also use the yyyy-mm-ddT:hh:mm:ss.[millis|micros|nanos] TZ format to specify a start date.

TZ represents the time zone. A few examples are:

  • UTC
  • NY (New York)
  • JP (Tokyo)
from deephaven import time_table

result = time_table("00:00:01", "2021-08-06T13:21:00 NY")

Date-time constants

Deephaven comes with some predefined constants:

  • DAY
  • HOUR
  • MINUTE
  • SECOND
  • WEEK
  • YEAR

These constants all show the time in nanoseconds.

from deephaven.time import DAY, HOUR, MINUTE, SECOND, WEEK, YEAR

print(DAY)
print(HOUR)
print(MINUTE)
print(SECOND)
print(WEEK)
print(YEAR)

See the Pydoc or Javadoc for further details.

Convert string to Deephaven date-time object

Deephaven supports converting a string to a Deephaven date-time object via the to_datetime method.

Strings should be in the yyyy-mm-ddT:hh:mm:ss.[millis|micros|nanos] TZ format.

from deephaven.time import to_datetime

datetime_object = to_datetime("2021-07-04T08:00:00 NY")

Date-time columns

Deephaven tables have built-in support for Deephaven date-time objects via the datetime_col method.

from deephaven.time import to_datetime
from deephaven import new_table
from deephaven.column import datetime_col

first_time = to_datetime("2021-07-04T08:00:00 NY")
second_time = to_datetime("2021-09-06T12:30:00 NY")
third_time = to_datetime("2021-12-25T21:15:00 NY")

result = new_table([
datetime_col("DateTimes", [first_time, second_time, third_time])
])

Timestamp comparison

Deephaven's filtering has support for timestamp comparisons.

note

Query strings require single quotes ' around timestamps.

from deephaven.time import to_datetime
from deephaven import new_table
from deephaven.column import datetime_col

first_time = to_datetime("2021-07-04T08:00:00 NY")
second_time = to_datetime("2021-09-06T12:30:00 NY")
third_time = to_datetime("2021-12-25T21:15:00 NY")
result = new_table([
datetime_col("DateTimes", [first_time, second_time, third_time])
])
filtered = result.where(filters=["DateTimes >= '2021-09-06T12:30:00 NY'"])

See our How to use filters guide for more information.

Time zones

from deephaven.time import TimeZone, to_datetime, day_of_month

datetime_object = to_datetime("2021-07-04T08:00:00 NY")
day_of_month_result = day_of_month(datetime_object, TimeZone.NY)
print(day_of_month_result)

See our full list of time zones

Downsampling temporal data via time binning

Downsampling time series data may be accomplished by calculating binning-intervals for time values and using appropriate aggregation methods, grouped by the binned interval.

from deephaven.time import to_datetime
from deephaven import new_table
from deephaven.column import datetime_col, int_col
from deephaven import agg as agg

first_time = to_datetime("2021-09-06T12:29:58 NY")
second_time = to_datetime("2021-09-06T12:30:00 NY")
third_time = to_datetime("2021-09-06T12:30:01 NY")
fourth_time = to_datetime("2021-09-06T12:30:05 NY")
fifth_time = to_datetime("2021-09-06T12:30:09 NY")
data_table = new_table([
datetime_col("DateTimes", [first_time, second_time, third_time, fourth_time, fifth_time]),
int_col("DataCol", [0, 1, 2, 3, 4]),
])
binned_table = data_table.update_view(formulas=["UpperBin=upperBin(DateTimes, 5 * SECOND)", "LowerBin=lowerBin(DateTimes, 5 * SECOND)"])

grouped_upper = binned_table.agg_by([agg.first(cols=["FirstDateTime=DateTimes"])], by=["UpperBin"])

grouped_lower = binned_table.agg_by([agg.first(cols=["FirstDateTime=DateTimes"])], by=["LowerBin"])