yggdrasil.enums.timeunit¶
timeunit ¶
Centralized time-unit enum shared by every temporal DataType.
Temporal types (DateType / TimeType / TimestampType /
DurationType) carry a unit field — a short string token like
"us" or "ns" — that decides Arrow precision, Polars dtype
mapping, Spark unit conversion, and scalar epoch math. Before this
module the canonical token list lived inline in temporal.py as a
loose dict, and aliases ("microsecond", "nanoseconds",
"millis", …) coming in from user dicts had no one place to be
normalized.
:class:TimeUnit provides:
- canonical members for the eight units the temporal types actually
understand (
s,ms,us,ns,d, plus the interval formsyear_month/day_time/month_day_nano); - :meth:
parsefor forgiving string /TimeUnit/Noneinput with alias support; - :meth:
is_validfor boolean checks without raising; - :attr:
seconds/ :attr:orderfor scalar conversion and precision-rank comparisons.
The enum subclasses :class:str so a TimeUnit instance is
interchangeable with the string token everywhere it lands —
unit: str = "us" field declarations don't need to change.
TimeUnit ¶
Bases: str, Enum
Canonical time-unit token for temporal DataType instances.
Members carry the lowercase short form so the enum is a drop-in string replacement::
TimeType(unit=TimeUnit.MICROSECOND)
TimeType(unit="us") # equivalent — both store ``"us"``
Use :meth:parse when accepting external input — it canonicalizes
aliases ("microseconds", "micros", "µs") to a member
and raises :class:ValueError for unknown tokens.
seconds
property
¶
Seconds per one of this unit (used for scalar epoch math).
Calendar-style interval units (year_month / day_time /
month_day_nano) have no fixed second-count and return
float('nan') so comparisons surface the mismatch instead of
silently truncating to zero.
order
property
¶
Precision rank — higher = finer.
Used by TemporalType._merge_with_same_id to pick the wider
of two units. Calendar interval units sit at rank -1 so
they don't outrank fixed-precision ones in normal merges.
from_
classmethod
¶
Coerce any Python value into a :class:TimeUnit.
Accepts:
- :class:
TimeUnit(returned as-is); - any string the alias table or canonical member values know —
s/ms/us/ns/d/ interval forms, plurals (microseconds), long forms (millisecond), mixed case,µs, hyphens / spaces; - objects exposing a
time_unit/unitattribute (PolarsDatetime/Duration, PyArrowTimestampType/DurationType/Time32Type/Time64Type); the attribute is re-funneled throughfrom_; None— returns default if supplied, else raises.
default swallows unknown / unparseable input. Without it,
unknown tokens raise :class:ValueError and unsupported types
raise :class:TypeError.
is_valid
classmethod
¶
Return True when :meth:from_ would succeed for value.