{"record":{"id":"eb025f3518c3f748","repo":"pandas-dev/pandas","slug":"unit-must-be-one-of-s-ms-us-ns","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/datetimes.py","lineNumber":426,"sourceCode":"        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        freq = to_offset(freq)\n\n        if start is not None:\n            start = Timestamp(start)\n\n        if end is not None:\n            end = Timestamp(end)\n\n        if start is NaT or end is NaT:\n            raise ValueError(\"Neither `start` nor `end` can be NaT\")\n\n        if unit is not None:\n            if unit not in [\"s\", \"ms\", \"us\", \"ns\"]:\n                raise ValueError(\"'unit' must be one of 's', 'ms', 'us', 'ns'\")\n        else:\n            unit = \"ns\"\n\n        if start is not None:\n            start = start.as_unit(unit, round_ok=False)\n        if end is not None:\n            end = end.as_unit(unit, round_ok=False)\n\n        left_inclusive, right_inclusive = validate_inclusive(inclusive)\n        start, end = _maybe_normalize_endpoints(start, end, normalize)\n        tz = _infer_tz_from_endpoints(start, end, tz)\n\n        if tz is not None:\n            # Localize the start and end arguments\n            start = _maybe_localize_point(start, freq, tz, ambiguous, nonexistent)\n            end = _maybe_localize_point(end, freq, tz, ambiguous, nonexistent)\n\n        if freq is not None:","sourceCodeStart":408,"sourceCodeEnd":444,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/datetimes.py#L408-L444","documentation":"Raised in _generate_range when the unit keyword is not one of the four valid storage resolutions ('s','ms','us','ns'). This controls the i8 precision of the generated datetime64 backing array; coarser/finer aliases are not valid storage units.","triggerScenarios":"pd.date_range(..., unit='m'), pd.date_range(..., unit='us5'), pd.date_range(..., unit='ps'), or passing a frequency alias like 'h'/'T' as unit.","commonSituations":"Confusing the unit= parameter (storage resolution) with the freq= parameter (grid spacing). Copying unit strings from numpy dtype expressions like 'datetime64[m]'. Wanting minute/hour spacing.","solutions":["Pass unit='s'|'ms'|'us'|'ns' to set storage precision.","For minute/hourly spacing use freq='min'|'h' instead of unit.","If you genuinely need coarser-than-second storage, store in 's' and downcast at display time."],"exampleFix":"# before\npd.date_range('2020-01-01', periods=3, freq='D', unit='m')\n\n# after\npd.date_range('2020-01-01', periods=3, freq='min', unit='s')","handlingStrategy":"validation","validationCode":"VALID_UNITS = {'s','ms','us','ns'}\nif unit not in VALID_UNITS:\n    raise ValueError(f'unit must be one of {VALID_UNITS}, got {unit!r}')","typeGuard":"from typing import Literal\ndef is_storage_unit(u: str) -> bool:\n    return u in {'s','ms','us','ns'}","tryCatchPattern":"try:\n    pd.date_range(start, periods=3, freq='D', unit=unit)\nexcept ValueError as e:\n    if \"'unit' must be one of\" in str(e):\n        pd.date_range(start, periods=3, freq='D', unit='ns')\n    else: raise","preventionTips":["Type unit as Literal['s','ms','us','ns'] in signatures.","Use freq= for spacing and unit= for precision; never conflate."],"tags":["date-range","unit","resolution","validation"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}