{"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":1436,"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":1418,"sourceCodeEnd":1441,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/timedeltas.py#L1418-L1441","documentation":"Raised by _validate_td64_dtype when the resolved dtype is not a numpy timedelta64 dtype at all (after the unit-less 'm8' case is handled separately). This catches inputs like 'int64', 'float64', 'datetime64[ns]', or arbitrary object/category dtypes passed where a timedelta dtype is required.","triggerScenarios":"`pd.TimedeltaIndex([], dtype='int64')`, or passing a datetime64/category/object dtype string to a timedelta-validated constructor.","commonSituations":"Reusing a dtype variable intended for a different column; dynamically building dtype strings and supplying the wrong kind.","solutions":["Pass a valid numpy timedelta64 dtype (e.g. 'timedelta64[ns]').","If you have integer data, convert it via `pd.to_timedelta(..., unit=...)` instead of forcing a timedelta dtype.","Validate that `pandas_dtype(dtype).kind == 'm'` before constructing."],"exampleFix":"# before\npd.TimedeltaIndex([1, 2, 3], dtype='int64')  # ValueError\n\n# after\npd.TimedeltaIndex([1, 2, 3], dtype='timedelta64[ns]', unit='s')","handlingStrategy":"validation","validationCode":"import numpy as np\nfrom pandas.api.types import pandas_dtype\n\ndef is_valid_td_dtype(dtype) -> bool:\n    dt = pandas_dtype(dtype)\n    return dt.kind == 'm' and dt != np.dtype('m8')","typeGuard":"null","tryCatchPattern":"try:\n    idx = pd.TimedeltaIndex(data, dtype=dtype)\nexcept ValueError as e:\n    if 'should be np.timedelta64' in str(e):\n        idx = pd.TimedeltaIndex(data, dtype='timedelta64[ns]')\n    else:\n        raise","preventionTips":["Validate dtype.kind == 'm' (with a unit) before constructing timedelta objects.","Convert integer data via pd.to_timedelta(unit=...) instead of forcing a timedelta dtype."],"tags":["timedelta","dtype","validation","valueerror"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}