Skip to content

Commit 2c024a6

Browse files
ERR: improve setitem error message for DatetimeArray (#54809)
1 parent a478e21 commit 2c024a6

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

pandas/core/arrays/datetimelike.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -625,15 +625,19 @@ def _validation_error_message(self, value, allow_listlike: bool = False) -> str:
625625
-------
626626
str
627627
"""
628+
if hasattr(value, "dtype") and getattr(value, "ndim", 0) > 0:
629+
msg_got = f"{value.dtype} array"
630+
else:
631+
msg_got = f"'{type(value).__name__}'"
628632
if allow_listlike:
629633
msg = (
630634
f"value should be a '{self._scalar_type.__name__}', 'NaT', "
631-
f"or array of those. Got '{type(value).__name__}' instead."
635+
f"or array of those. Got {msg_got} instead."
632636
)
633637
else:
634638
msg = (
635639
f"value should be a '{self._scalar_type.__name__}' or 'NaT'. "
636-
f"Got '{type(value).__name__}' instead."
640+
f"Got {msg_got} instead."
637641
)
638642
return msg
639643

pandas/tests/arrays/test_datetimelike.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -324,19 +324,12 @@ def test_searchsorted_castable_strings(self, arr1d, box, string_storage):
324324
):
325325
arr.searchsorted("foo")
326326

327-
if string_storage == "python":
328-
arr_type = "StringArray"
329-
elif string_storage == "pyarrow":
330-
arr_type = "ArrowStringArray"
331-
else:
332-
arr_type = "ArrowStringArrayNumpySemantics"
333-
334327
with pd.option_context("string_storage", string_storage):
335328
with pytest.raises(
336329
TypeError,
337330
match=re.escape(
338331
f"value should be a '{arr1d._scalar_type.__name__}', 'NaT', "
339-
f"or array of those. Got '{arr_type}' instead."
332+
"or array of those. Got string array instead."
340333
),
341334
):
342335
arr.searchsorted([str(arr[1]), "baz"])

0 commit comments

Comments
 (0)