{"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":317,"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":299,"sourceCodeEnd":335,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/timedeltas.py#L299-L335","documentation":"TimedeltaArray._generate_range restricts the unit parameter to the supported set {s, ms, us, ns}. Coarser (e.g. minute, hour, day) or finer (ns below) units are not allowed for the array's internal storage resolution; only the resolutions supported by numpy timedelta64 without overflow are accepted.","triggerScenarios":"pd.timedelta_range(..., unit='m'); passing unit='h' or unit='D' to _generate_range; constructing with a custom TimeUnit outside {s,ms,us,ns}.","commonSituations":"Assuming unit accepts the same strings as to_timedelta (which accepts 'D','h','m'); reusing a unit string parsed from user config without validation.","solutions":["Use one of 's', 'ms', 'us', 'ns' for the unit argument.","If you need day/hour/minute granularity, specify freq in those units (e.g. freq='1D') but keep unit in the supported set.","Post-process with .as_unit(...) if a different display unit is desired."],"exampleFix":"// before\npd.timedelta_range('1 day', periods=3, freq='1D', unit='D')  # ValueError\n// after\npd.timedelta_range('1 day', periods=3, freq='1D', unit='s')","handlingStrategy":"validation","validationCode":"SUPPORTED_UNITS = {'s','ms','us','ns'}\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    pd.timedelta_range(start, periods=periods, freq=freq, unit=unit)\nexcept ValueError as e:\n    if \"'unit' must be one of\" in str(e):\n        pd.timedelta_range(start, periods=periods, freq=freq, unit='s')\n    else:\n        raise","preventionTips":["Restrict unit to {s, ms, us, ns}.","Use freq strings (e.g. '1D','1h') for coarser granularity.","Validate user-supplied unit strings before passing."],"tags":["pandas","timedelta","unit","argument-validation","resolution"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}