How do I filter a table by time of day across all dates?
I have a table with a timestamp column that spans multiple dates. I want to filter rows to only include times between 2:00 PM and 4:00 PM ET, regardless of the date.
Use the hourOfDay function to extract the hour component from your timestamp, then filter based on the hour value:
from deephaven import empty_table
from deephaven.time import to_j_time_zone, to_j_instant
et = to_j_time_zone("ET")
start_time = to_j_instant("2024-01-15T13:00:00 ET")
# Sample table with various timestamps across different dates
t = empty_table(10).update(
[
"Timestamp = start_time + i * 30 * MINUTE",
]
)
# Filter for times between 2:00 PM and 4:00 PM ET
result = t.where(
"hourOfDay(Timestamp, et, false) >= 14 && hourOfDay(Timestamp, et, false) < 16"
)
This approach works by:
- Using
hourOfDay(Timestamp, et, false)to extract the hour (0-23) from the timestamp in ET timezone. - Filtering for hours between 14 (2:00 PM) and 15 (3:00 PM, up to but not including 4:00 PM).
- The condition checks that the hour is greater than or equal to 14 and less than 16, ensuring times from 2:00:00 PM through 3:59:59 PM are included.
Filtering with minutes
If you need more precise time filtering (e.g., 2:30 PM to 4:15 PM), combine hourOfDay and minuteOfHour:
start_time2 = to_j_instant("2024-01-15T14:15:00 ET")
t2 = empty_table(10).update(
[
"Timestamp = start_time2 + i * 15 * MINUTE",
]
)
# Filter for times between 2:30 PM and 4:15 PM ET
result2 = t2.where(
"(hourOfDay(Timestamp, et, false) == 14 && minuteOfHour(Timestamp, et) >= 30) || "
+ "(hourOfDay(Timestamp, et, false) == 15) || "
+ "(hourOfDay(Timestamp, et, false) == 16 && minuteOfHour(Timestamp, et) <= 15)"
)
This filter includes:
- Hour 14 (2:00 PM) with minutes greater than or equal to 30
- All of hour 15 (3:00 PM)
- Hour 16 (4:00 PM) with minutes less than or equal to 15
Note
These FAQ pages contain answers to questions about Deephaven Community Core that our users have asked in our Community Slack. If you have a question that is not in our documentation, join our Community and we'll be happy to help!