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

firstTime = to_datetime("2021-07-04T08:00:00 NY")
secondTime = to_datetime("2021-09-06T12:30:00 NY")
thirdTime = to_datetime("2021-12-25T21:15:00 NY")

result = new_table([
datetime_col("DateTimes", [firstTime, secondTime, thirdTime])
])

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

firstTime = to_datetime("2021-07-04T08:00:00 NY")
secondTime = to_datetime("2021-09-06T12:30:00 NY")
thirdTime = to_datetime("2021-12-25T21:15:00 NY")
result = new_table([
datetime_col("DateTimes", [firstTime, secondTime, thirdTime])
])
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

firstTime = to_datetime("2021-09-06T12:29:58 NY")
secondTime = to_datetime("2021-09-06T12:30:00 NY")
thirdTime = to_datetime("2021-09-06T12:30:01 NY")
fourthTime = to_datetime("2021-09-06T12:30:05 NY")
fifthTime = to_datetime("2021-09-06T12:30:09 NY")
dataTable = new_table([
datetime_col("DateTimes", [firstTime, secondTime, thirdTime, fourthTime, fifthTime]),
int_col("DataCol", [0, 1, 2, 3, 4]),
])
binnedTable = dataTable.update_view(formulas=["UpperBin=upperBin(DateTimes, 5 * SECOND)", "LowerBin=lowerBin(DateTimes, 5 * SECOND)"])

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

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