nautechsystems/nautilus_trader · error · TypeError
value must be datetime-like
Error message
value must be datetime-like
What it means
The pandas-less fallback of dt_to_unix_nanos only understands int (raw ns), ISO strings, and datetime instances; anything else — float, date, numpy datetime64, pd.Timestamp-without-pandas — reaches the terminal 'raise TypeError("value must be datetime-like")'. With pandas installed the same inputs are instead funneled through pd.Timestamp(value), which accepts a wider set.
Source
Thrown at python/nautilus_trader/core/datetime.py:91
def dt_to_unix_nanos(value: Any) -> 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 += 1View on GitHub (pinned to a4b06ed870)
Solutions
- Convert before calling: dt_to_unix_nanos(datetime(2024, 1, 1, tzinfo=timezone.utc)) or pass int ns
- Coerce floats to int ns explicitly (int(1_700_000_000.5 * 1e9)) if that is the true unit
- Install pandas to broaden accepted input types via pd.Timestamp
Example fix
# before (no pandas installed) dt_to_unix_nanos(1_700_000_000.5) # TypeError: value must be datetime-like # after dt_to_unix_nanos(int(1_700_000_000.5 * 1e9)) # or dt_to_unix_nanos(datetime.fromtimestamp(1_700_000_000.5, tz=timezone.utc))
Defensive patterns
Strategy: type-guard
Validate before calling
from datetime import datetime, date
if not isinstance(value, (int, str, datetime)):
if isinstance(value, date):
value = datetime(value.year, value.month, value.day)
elif isinstance(value, float):
value = int(value)
else:
raise TypeError(f'Unsupported timestamp type: {type(value).__name__}')
ts = dt_to_unix_nanos(value) Type guard
from datetime import datetime
def is_datetime_like(value: object) -> bool:
return isinstance(value, (int, str, datetime)) Try / catch
try:
ts = dt_to_unix_nanos(value)
except TypeError as e:
if 'datetime-like' in str(e):
ts = int(pd_or_manual_conversion(value)) # coerce explicitly, then retry
else:
raise Prevention
- Normalize timestamp columns to datetime/int before conversion loops
- In pandas-free deployments, add an input coercion layer for date/float/numpy scalars
When it happens
Trigger: dt_to_unix_nanos(1_700_000_000.5) or dt_to_unix_nanos(date(2024, 1, 1)) or a numpy scalar, in an environment where pandas import fails. The int branch returns early, the str branch parses ISO, the datetime branch converts — everything else falls through to the TypeError.
Common situations: Lightweight deployments without pandas receiving mixed-type timestamp columns (floats from CSV parses, numpy scalars, date objects); code that worked under pandas accepting datetime64/date then run pandas-free.
Related errors
- pandas is required for nanosecond-precision datetimes
- Chart renderer must be callable, was {type(renderer)}
- value must not be None
- No tearsheet chart registered under '{chart_name}'.{hint} Re
- Grid has {rows * cols} cells but {len(charts)} charts were c
AI-assisted analysis of nautechsystems/nautilus_trader@a4b06ed870 (2026-08-16).
Data as JSON: /api/errors/98741bdf77040214.
Report an issue: GitHub.