{"record":{"id":"69d73030927b1407","repo":"pandas-dev/pandas","slug":"value-should-be-a-self-scalar-type-name-o","errorCode":null,"errorMessage":"value should be a '{self._scalar_type.__name__}' or 'NaT'. Got {msg_got} instead.","messagePattern":"value should be a '(.+?)' or 'NaT'\\. Got (.+?) instead\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimelike.py","lineNumber":558,"sourceCode":"            listlike inputs are allowed.\n        unbox : bool, default True\n            Whether to unbox the result before returning.  Note: unbox=False\n            skips the setitem compatibility check.\n\n        Returns\n        -------\n        self._scalar_type or NaT\n        \"\"\"\n        if isinstance(value, self._scalar_type):\n            pass\n\n        elif isinstance(value, str):\n            # NB: Careful about tzawareness\n            try:\n                value = self._scalar_from_string(value)\n            except ValueError as err:\n                msg = self._validation_error_message(value, allow_listlike)\n                raise TypeError(msg) from err\n\n        elif is_valid_na_for_dtype(value, self.dtype):\n            # GH#18295\n            value = NaT\n\n        elif isna(value):\n            # if we are dt64tz and value is dt64(\"NaT\"), dont cast to NaT,\n            #  or else we'll fail to raise in _unbox_scalar\n            msg = self._validation_error_message(value, allow_listlike)\n            raise TypeError(msg)\n\n        elif isinstance(value, self._recognized_scalars):\n            # error: Argument 1 to \"Timestamp\" has incompatible type \"object\"; expected\n            # \"integer[Any] | float | str | date | datetime | datetime64\"\n            value = self._scalar_type(value)  # type: ignore[arg-type]\n\n        else:\n            msg = self._validation_error_message(value, allow_listlike)","sourceCodeStart":540,"sourceCodeEnd":576,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/datetimelike.py#L540-L576","documentation":"Raised in DatetimeLikeArray._validate_scalar (allow_listlike=False branch) when value is a string that fails to parse as the expected scalar type (e.g., a malformed date string passed to a DatetimeArray, or a non-duration string to a TimedeltaArray). The helper _validation_error_message builds the message; the allow_listlike=False variant tells the user only a scalar or NaT is accepted.","triggerScenarios":"Setting a single datetime value on a DatetimeIndex with a string like 'not-a-date'. Filling a datetime column with fillna('garbage'). Assigning to a TimedeltaArray with 'abc'. Internal _validate_scalar calls from setitem/fillna where listlike input is disallowed.","commonSituations":"User feeds a free-text column into a datetime slot expecting pandas to coerce. Locale/format mismatch (e.g., '31/12/2020' vs '%m/%d/%Y'). Whitespace or invisible characters in strings. Copy-paste of partial timestamps.","solutions":["Pre-parse with pd.to_datetime(value, errors='coerce') and check for NaT.","Pass a Timestamp/Timedelta object directly: Timestamp('2020-01-01') instead of a raw string.","Use NaT (pandas.NaT) explicitly for missing values instead of None or ''.","Validate format with pd.to_datetime(series, format='%Y-%m-%d') before assignment."],"exampleFix":"# before\ndti[0] = 'not-a-date'  # TypeError\n\n# after\ndti[0] = pd.Timestamp('2020-01-01')","handlingStrategy":"validation","validationCode":"import pandas as pd\nval = pd.to_datetime(value, errors='coerce')\nif pd.isna(val) and not (value is pd.NaT or value == 'NaT'):\n    raise ValueError(f'unparseable datetime scalar: {value!r}')\ndti[0] = val","typeGuard":"import pandas as pd\n\ndef is_valid_datetime_scalar(v) -> bool:\n    return isinstance(v, (pd.Timestamp, type(pd.NaT))) or (\n        isinstance(v, str) and not pd.isna(pd.to_datetime(v, errors='coerce'))\n    )","tryCatchPattern":"try:\n    dti[0] = raw\nexcept TypeError as e:\n    if 'value should be' in str(e):\n        dti[0] = pd.to_datetime(raw, errors='coerce')\n    else:\n        raise","preventionTips":["Pass Timestamp objects instead of raw strings to datetimelike setters.","Pre-validate date strings with pd.to_datetime(errors='coerce')."],"tags":["datetimelike","validation","string-parse","scalar"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}