pandas-dev/pandas · error · TypeError
value should be a ' ', 'NaT', or array of those. Got…
Error message
value should be a '{self._scalar_type.__name__}', 'NaT', or array of those. Got {msg_got} instead. What it means
Same validation path as error 255 but raised from a call site that sets allow_listlike=True (e.g., __setitem__ accepting array-like values, line 558). The message additionally tells the user an array of the scalar type is acceptable. Otherwise the trigger is identical: a string that cannot be parsed as the scalar type for the array.
Solutions
- Pre-coerce the whole batch: pd.to_datetime(values, errors='coerce') then assign.
- Pass Timestamp/Timedelta objects or a DatetimeIndex instead of strings.
- Filter out unparseable values before assignment.
- Validate each element with pd.to_datetime(value, errors='coerce') in a loop for small batches.
Example fix
# before dti[0:2] = ['2020-01-01', 'bad'] # TypeError # after vals = pd.to_datetime(['2020-01-01', 'bad'], errors='coerce') dti[0:2] = vals
Defensive patterns
Strategy: validation
Validate before calling
import pandas as pd parsed = pd.to_datetime(list(values), errors='coerce') dti[0:len(parsed)] = parsed
Type guard
import pandas as pd
def all_parse_as_datetime(values) -> bool:
parsed = pd.to_datetime(list(values), errors='coerce')
return not (parsed.isna() ^ pd.isna(list(values))).any() Try / catch
try:
dti[0:n] = values
except TypeError as e:
if 'value should be' in str(e):
dti[0:n] = pd.to_datetime(list(values), errors='coerce')
else:
raise Prevention
- Coerce entire batches with pd.to_datetime before assignment.
- Reject or quarantine rows with unparseable date strings upstream.
When it happens
Trigger: Assigning a list/array containing malformed date strings to a DatetimeArray slice: dti[0:3] = ['2020','bad']. fillna on a datetime column with a list whose element fails parsing. _validate_scalar called with allow_listlike=True from setitem.
Common situations: Bulk-assigning string dates where one row has a bad value. Mixed-format date strings in a single batch assignment. Locale mismatches surfacing only for some rows.
Related errors
- value should be a ' ' or 'NaT'. Got instead.
- 'value' should be an interval type, got
- can only insert Interval objects and NA into an…
- cannot assign without a target object
- Cannot cast to dtype
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/527010d3ae489144.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/arrays/datetimelike.py:558
listlike inputs are allowed.
unbox : bool, default True
Whether to unbox the result before returning. Note: unbox=False
skips the setitem compatibility check.
Returns
-------
self._scalar_type or NaT
"""
if isinstance(value, self._scalar_type):
pass
elif isinstance(value, str):
# NB: Careful about tzawareness
try:
value = self._scalar_from_string(value)
except ValueError as err:
msg = self._validation_error_message(value, allow_listlike)
raise TypeError(msg) from err
elif is_valid_na_for_dtype(value, self.dtype):
# GH#18295
value = NaT
elif isna(value):
# if we are dt64tz and value is dt64("NaT"), dont cast to NaT,
# or else we'll fail to raise in _unbox_scalar
msg = self._validation_error_message(value, allow_listlike)
raise TypeError(msg)
elif isinstance(value, self._recognized_scalars):
# error: Argument 1 to "Timestamp" has incompatible type "object"; expected
# "integer[Any] | float | str | date | datetime | datetime64"
value = self._scalar_type(value) # type: ignore[arg-type]
else:
msg = self._validation_error_message(value, allow_listlike)View on GitHub (pinned to 3b7651241d)