{"record":{"id":"0046e891e9fb5f3d","repo":"pandas-dev/pandas","slug":"unit-must-be-one-of-s-ms-us-ns-0046e8","errorCode":null,"errorMessage":"'unit' must be one of 's', 'ms', 'us', 'ns'","messagePattern":"'unit' must be one of 's', 'ms', 'us', 'ns'","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/timedeltas.py","lineNumber":300,"sourceCode":"    ) -> Self:\n        periods = dtl.validate_periods(periods)\n        if freq is None and any(x is None for x in [periods, start, end]):\n            raise ValueError(\"Must provide freq argument if no data is supplied\")\n\n        if com.count_not_none(start, end, periods, freq) != 3:\n            raise ValueError(\n                \"Of the four parameters: start, end, periods, \"\n                \"and freq, exactly three must be specified\"\n            )\n\n        if start is not None:\n            start = Timedelta(start).as_unit(\"ns\")\n\n        if end is not None:\n            end = Timedelta(end).as_unit(\"ns\")\n\n        if unit not in [\"s\", \"ms\", \"us\", \"ns\"]:\n            raise ValueError(\"'unit' must be one of 's', 'ms', 'us', 'ns'\")\n\n        if start is not None and unit is not None:\n            start = start.as_unit(unit, round_ok=False)\n        if end is not None and unit is not None:\n            end = end.as_unit(unit, round_ok=False)\n\n        left_closed, right_closed = validate_endpoints(closed)\n\n        if freq is not None:\n            index = generate_regular_range(start, end, periods, freq, unit=unit)\n        else:\n            index = np.linspace(start._value, end._value, periods).astype(\"i8\")\n\n        if not left_closed:\n            index = index[1:]\n        if not right_closed:\n            index = index[:-1]\n","sourceCodeStart":282,"sourceCodeEnd":318,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/timedeltas.py#L282-L318","documentation":"Raised by TimedeltaArray._generate_range when the `unit` argument is not one of the supported resolutions 's','ms','us','ns'. TimedeltaArray is backed by int64 nanosecond-or-coarser ticks, and only those four resolutions are representable; any other string (e.g. 'm' for minutes, 'h', or a typo) is rejected before range generation proceeds.","triggerScenarios":"Calling timedelta_range(..., unit='m'), unit='minutes', or unit='h'; passing a custom unit string. The check at timedeltas.py:299 is `unit not in ['s','ms','us','ns']`.","commonSituations":"Confusing freq strings (which accept '1H','30min') with unit strings (which only take s/ms/us/ns); passing a numpy unit abbreviation by mistake; schema/config typos.","solutions":["Use one of 's','ms','us','ns' for the unit argument.","If you want minute/hour granularity, express it via freq='1min'/'1H' instead of unit.","Pick the coarsest unit that fits your precision need to reduce memory."],"exampleFix":"# before\npd.timedelta_range('1 day', periods=3, unit='m')  # ValueError\n# after\npd.timedelta_range('1 day', periods=3, freq='1min')\n# or: unit='s'","handlingStrategy":"validation","validationCode":"SUPPORTED_UNITS = {'s','ms','us','ns'}\n\ndef validate_td_unit(unit):\n    if unit not in SUPPORTED_UNITS:\n        raise ValueError(f\"unit must be one of {SUPPORTED_UNITS}, got {unit!r}\")\n    return unit","typeGuard":"def is_supported_td_unit(unit) -> bool:\n    return unit in {'s','ms','us','ns'}","tryCatchPattern":"try:\n    return pd.timedelta_range(start, periods=3, unit=unit)\nexcept ValueError as e:\n    if \"'unit' must be one of\" in str(e):\n        return pd.timedelta_range(start, periods=3, unit='s')\n    raise","preventionTips":["Do not confuse unit (s/ms/us/ns) with freq strings (1H, 30min).","Whitelist the unit at config boundaries.","Document the distinction between unit and freq in your helpers."],"tags":["timedelta","unit","resolution","valueerror"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}