nautechsystems/nautilus_trader · error · TypeError

value must be datetime-like

Error message

value must be datetime-like

What it means

Generic type guard in dt_to_unix_nanos: the value matched none of the accepted kinds (int nanos, ISO-8601 string, datetime, pd.Timestamp). Notably it fires for duck-typed objects that merely expose a `.value` attribute, which the converter deliberately refuses to unwrap.

Solutions

  1. Pass an int, ISO-8601 string, datetime, or pd.Timestamp directly
  2. Unwrap custom wrapper objects yourself, e.g. dt_to_unix_nanos(obj.value), instead of relying on attribute sniffing
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at python/nautilus_trader/core/datetime.py:99 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/98741bdf77040214. Report an issue: GitHub.

Appendix: source

Thrown at python/nautilus_trader/core/datetime.py:99

def dt_to_unix_nanos(value: int | str | datetime | pd.Timestamp | None) -> int:
    """
    Return the UNIX timestamp in nanoseconds for the given datetime-like value.
    """
    if value is None:
        raise ValueError("value must not be None")

    try:
        import pandas as pd
    except ImportError:
        if isinstance(value, int):
            return value
        if isinstance(value, str):
            if _has_more_than_microsecond_precision(value):
                raise ValueError("pandas is required for nanosecond-precision datetimes") from None
            value = datetime.fromisoformat(value)
        if isinstance(value, datetime):
            return _datetime_to_unix_nanos(value)
        raise TypeError("value must be datetime-like") from None

    if isinstance(value, pd.Timestamp):
        return int(value.value)

    return int(pd.Timestamp(value).value)


def _has_more_than_microsecond_precision(value: str) -> bool:
    _, separator, remainder = value.partition(".")
    if not separator:
        return False

    digits = 0

    for char in remainder:
        if not char.isdigit():
            break
        digits += 1

View on GitHub (pinned to 18893faf8b)