{"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":"validation","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/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/datetimes.py#L408-L444","documentation":"Raised by DatetimeArray._generate_range when the unit argument is not None and not in ['s','ms','us','ns']. date_range only supports those four resolutions for its backing datetime64 dtype; any other unit string (including numpy-style like 'm','h') is rejected up front so the range is never built at the wrong precision.","triggerScenarios":"pd.date_range(..., unit='m'); pd.date_range(..., unit='ps'); pd.date_range(..., unit='hours'); passing a config-driven unit string without normalization.","commonSituations":"Mapping from a different unit vocabulary (numpy timedelta codes, ISO durations); user-configurable unit fields; copy-paste from as_unit calls that also restrict to the same four.","solutions":["Use one of 's','ms','us','ns'; for minute/hour precision choose 's' (coarsest supported).","Normalize a user unit string at the config boundary: UNIT_MAP = {'minute':'s','hour':'s','second':'s'}.","If sub-nanosecond is required, keep values as int64 counts; pandas cannot represent them as datetime64."],"exampleFix":"// before\npd.date_range('2020-01-01', periods=3, unit='m')  # ValueError: 'unit' must be one of ...\n\n// after\npd.date_range('2020-01-01', periods=3, unit='s')","handlingStrategy":"validation","validationCode":"SUPPORTED_UNITS = {\"s\",\"ms\",\"us\",\"ns\"}\nUNIT_MAP = {\"minute\":\"s\",\"hour\":\"s\",\"second\":\"s\",\"millisecond\":\"ms\",\"microsecond\":\"us\",\"nanosecond\":\"ns\"}\ndef safe_unit(u):\n    u = UNIT_MAP.get(str(u).lower(), u)\n    if u not in SUPPORTED_UNITS:\n        raise ValueError(f\"unit must be one of {SUPPORTED_UNITS}\")\n    return u","typeGuard":"def is_supported_date_range_unit(unit: str) -> bool:\n    return unit in {\"s\",\"ms\",\"us\",\"ns\"}","tryCatchPattern":"try:\n    rng = pd.date_range(start, periods=5, freq=\"D\", unit=unit)\nexcept ValueError as e:\n    if \"'unit' must be one of\" in str(e):\n        rng = pd.date_range(start, periods=5, freq=\"D\", unit=\"s\")\n    else:\n        raise","preventionTips":["Expose unit through an enum/allowlist in public APIs.","Normalize free-text unit strings at the config boundary.","Document the four supported units near date_range usage."],"tags":["pandas","date-range","unit","resolution","validation"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}