{"record":{"id":"0ef22e6f32ed31a6","repo":"pandas-dev/pandas","slug":"dtype-dtype-is-invalid-should-be-np-timedelta","errorCode":null,"errorMessage":"dtype '{dtype}' is invalid, should be np.timedelta64 dtype","messagePattern":"dtype '(.+?)' is invalid, should be np\\.timedelta64 dtype","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/timedeltas.py","lineNumber":1416,"sourceCode":"    # coerce Index to np.ndarray, converting string-dtype if necessary\n    values = np.asarray(data, dtype=np.object_)\n\n    result = array_to_timedelta64(values, unit=unit, errors=errors)\n    return result\n\n\ndef _validate_td64_dtype(dtype) -> DtypeObj:\n    dtype = pandas_dtype(dtype)\n    if dtype == np.dtype(\"m8\"):\n        # no precision disallowed GH#24806\n        msg = (\n            \"Passing in 'timedelta' dtype with no precision is not allowed. \"\n            \"Please pass in 'timedelta64[ns]' instead.\"\n        )\n        raise ValueError(msg)\n\n    if not lib.is_np_dtype(dtype, \"m\"):\n        raise ValueError(f\"dtype '{dtype}' is invalid, should be np.timedelta64 dtype\")\n    elif not is_supported_dtype(dtype):\n        raise ValueError(\"Supported timedelta64 resolutions are 's', 'ms', 'us', 'ns'\")\n\n    return dtype\n","sourceCodeStart":1398,"sourceCodeEnd":1421,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/timedeltas.py#L1398-L1421","documentation":"Raised by _validate_td64_dtype when the supplied dtype is not a numpy timedelta64 dtype at all (e.g. int64, float64, datetime64, object). The error names the offending dtype and states the expected kind.","triggerScenarios":"`pd.TimedeltaIndex(data, dtype='int64')`, `.astype({'col':'datetime64[ns]'})` on a timedelta-typed object, or passing a non-timedelta dtype to APIs that validate the timedelta dtype.","commonSituations":"Wrong column in a dtype map; copy-paste from datetime code; programmatic dtype construction errors.","solutions":["Pass np.timedelta64 or a 'timedelta64[<unit>]' string.","Cross-check the dtype map keys against actual column semantics.","If you meant datetime, use datetime64[ns] instead."],"exampleFix":"// before\nidx = pd.TimedeltaIndex([1,2], dtype='int64')\n\n// after\nidx = pd.TimedeltaIndex([1,2], dtype='timedelta64[ns]')","handlingStrategy":"validation","validationCode":"import numpy as np\nd = pandas_dtype(dtype)\nif not lib.is_np_dtype(d, 'm'):\n    raise ValueError(f'dtype {d} is not timedelta64')","typeGuard":"def is_td_dtype(dtype) -> bool:\n    import numpy as np\n    from pandas.core.dtypes.common import pandas_dtype\n    try:\n        return lib.is_np_dtype(pandas_dtype(dtype), 'm')\n    except TypeError:\n        return False","tryCatchPattern":"try:\n    idx = pd.TimedeltaIndex(data, dtype=dtype)\nexcept ValueError as e:\n    if 'should be np.timedelta64 dtype' in str(e):\n        idx = pd.TimedeltaIndex(data, dtype='timedelta64[ns]')\n    else:\n        raise","preventionTips":["Validate dtype is timedelta64 before construction.","Build dtype strings from constants.","Distinguish datetime vs timedelta in configs."],"tags":["timedelta","dtype","valueerror"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}