timehash — Time bucket¶
Time buckets, on pl.Expr as .timehash.
A timehash is a short string that names the window of time an instant falls in. Two instants in the same window get the same hash, and a shorter hash names a wider window, so a comparison of prefixes is a coarser bucket.
from_datetime
¶
Encode an instant to the timehash of the window that holds it.
Datetime and Date columns work directly. Epoch seconds may be Float64 or any integer type. Float32 cannot hold one closely enough to land in the right window. The timestamp must fall between 1970-01-01 and 2098-01-01.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
precision
|
int | str | Expr
|
The number of characters in the hash, from 1 to 32. A higher precision means a shorter window: 10 covers about 4 seconds and 8 about 4 minutes. Past about 18 the hash stops changing for a present-day timestamp, and the extra characters are padding. |
10
|
strict
|
bool
|
With |
True
|
Returns:
| Type | Description |
|---|---|
Expr
|
Utf8. |
Tip
A when/then guard cannot skip an out-of-range timestamp, because
polars evaluates both branches over the whole column. Use
strict=False instead.
Examples:
>>> from datetime import datetime
>>> df = pl.DataFrame({"t": [datetime(2024, 5, 17, 12, 30, 45)]})
>>> df.select(plh.col("t").timehash.from_datetime()).item()
'bb1c00aaf0'
A lower precision gives a shorter hash and a wider window:
to_datetime
¶
Decode a timehash to the midpoint of the window it names.
Returns:
| Type | Description |
|---|---|
Expr
|
Datetime in microseconds, UTC. |
Note
The hash holds an instant and not a wall clock, so the original time
zone is gone. Use .dt.convert_time_zone(tz) for another zone. The
midpoint is not the instant you encoded. It is the center of the
window that instant fell in, so a round trip is exact only up to the
precision you used.
Examples: