Skip to content

Commit

Permalink
ERR: improve setitem error message for DatetimeArray
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche committed Aug 28, 2023
1 parent ca42994 commit f91fbb9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
8 changes: 6 additions & 2 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,15 +625,19 @@ def _validation_error_message(self, value, allow_listlike: bool = False) -> str:
-------
str
"""
if hasattr(value, "dtype") and getattr(value, "ndim", 0) > 0:
msg_got = f"{value.dtype} array"
else:
msg_got = f"'{type(value).__name__}'"
if allow_listlike:
msg = (
f"value should be a '{self._scalar_type.__name__}', 'NaT', "
f"or array of those. Got '{type(value).__name__}' instead."
f"or array of those. Got {msg_got} instead."
)
else:
msg = (
f"value should be a '{self._scalar_type.__name__}' or 'NaT'. "
f"Got '{type(value).__name__}' instead."
f"Got {msg_got} instead."
)
return msg

Expand Down
9 changes: 1 addition & 8 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,19 +324,12 @@ def test_searchsorted_castable_strings(self, arr1d, box, string_storage):
):
arr.searchsorted("foo")

if string_storage == "python":
arr_type = "StringArray"
elif string_storage == "pyarrow":
arr_type = "ArrowStringArray"
else:
arr_type = "ArrowStringArrayNumpySemantics"

with pd.option_context("string_storage", string_storage):
with pytest.raises(
TypeError,
match=re.escape(
f"value should be a '{arr1d._scalar_type.__name__}', 'NaT', "
f"or array of those. Got '{arr_type}' instead."
"or array of those. Got string array instead."
),
):
arr.searchsorted([str(arr[1]), "baz"])
Expand Down

0 comments on commit f91fbb9

Please sign in to comment.