{"record":{"id":"98741bdf77040214","repo":"nautechsystems/nautilus_trader","slug":"value-must-be-datetime-like","errorCode":null,"errorMessage":"value must be datetime-like","messagePattern":"value must be datetime-like","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"python/nautilus_trader/core/datetime.py","lineNumber":91,"sourceCode":"def dt_to_unix_nanos(value: Any) -> int:\n    \"\"\"\n    Return the UNIX timestamp in nanoseconds for the given datetime-like value.\n    \"\"\"\n    if value is None:\n        raise ValueError(\"value must not be None\")\n\n    try:\n        import pandas as pd\n    except ImportError:\n        if isinstance(value, int):\n            return value\n        if isinstance(value, str):\n            if _has_more_than_microsecond_precision(value):\n                raise ValueError(\"pandas is required for nanosecond-precision datetimes\") from None\n            value = datetime.fromisoformat(value)\n        if isinstance(value, datetime):\n            return _datetime_to_unix_nanos(value)\n        raise TypeError(\"value must be datetime-like\") from None\n\n    if isinstance(value, pd.Timestamp):\n        return int(value.value)\n\n    return int(pd.Timestamp(value).value)\n\n\ndef _has_more_than_microsecond_precision(value: str) -> bool:\n    _, separator, remainder = value.partition(\".\")\n    if not separator:\n        return False\n\n    digits = 0\n\n    for char in remainder:\n        if not char.isdigit():\n            break\n        digits += 1","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/a4b06ed870971b5671d12754ea138a3ab99b1dec/python/nautilus_trader/core/datetime.py#L73-L109","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before (no pandas installed)\ndt_to_unix_nanos(1_700_000_000.5)  # TypeError: value must be datetime-like\n\n# after\ndt_to_unix_nanos(int(1_700_000_000.5 * 1e9))\n# or\ndt_to_unix_nanos(datetime.fromtimestamp(1_700_000_000.5, tz=timezone.utc))","handlingStrategy":"type-guard","validationCode":"from datetime import datetime, date\n\nif not isinstance(value, (int, str, datetime)):\n    if isinstance(value, date):\n        value = datetime(value.year, value.month, value.day)\n    elif isinstance(value, float):\n        value = int(value)\n    else:\n        raise TypeError(f'Unsupported timestamp type: {type(value).__name__}')\nts = dt_to_unix_nanos(value)","typeGuard":"from datetime import datetime\n\ndef is_datetime_like(value: object) -> bool:\n    return isinstance(value, (int, str, datetime))","tryCatchPattern":"try:\n    ts = dt_to_unix_nanos(value)\nexcept TypeError as e:\n    if 'datetime-like' in str(e):\n        ts = int(pd_or_manual_conversion(value))  # coerce explicitly, then retry\n    else:\n        raise","preventionTips":["Normalize timestamp columns to datetime/int before conversion loops","In pandas-free deployments, add an input coercion layer for date/float/numpy scalars"],"tags":["python","datetime","type-validation","typeerror","pandas"],"backgroundTag":"type-validation-failed","analyzedSha":"a4b06ed870971b5671d12754ea138a3ab99b1dec","analyzedAt":"2026-08-16T22:54:50.089Z","schemaVersion":2},"datasetVersion":"2026-08-16T23:17:17.608Z"}