Skip to content

CLN: consolidate exception messages in datetimelike validate_listlike #37229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def _validate_comparison_value(self, other, opname: str):

else:
try:
other = self._validate_listlike(other, opname, allow_object=True)
other = self._validate_listlike(other, allow_object=True)
self._check_compatible_with(other)
except TypeError as err:
if is_object_dtype(getattr(other, "dtype", None)):
Expand Down Expand Up @@ -548,7 +548,7 @@ def _validate_scalar(self, value, msg: Optional[str] = None):

return value

def _validate_listlike(self, value, opname: str, allow_object: bool = False):
def _validate_listlike(self, value, allow_object: bool = False):
if isinstance(value, type(self)):
return value

Expand Down Expand Up @@ -578,18 +578,17 @@ def _validate_listlike(self, value, opname: str, allow_object: bool = False):

elif not type(self)._is_recognized_dtype(value.dtype):
raise TypeError(
f"{opname} requires compatible dtype or scalar, "
f"not {type(value).__name__}"
f"value should be a '{self._scalar_type.__name__}', 'NaT', "
f"or array of those. Got '{type(value).__name__}' instead."
)

return value

def _validate_searchsorted_value(self, value):
msg = "searchsorted requires compatible dtype or scalar"
if not is_list_like(value):
value = self._validate_scalar(value, msg)
else:
value = self._validate_listlike(value, "searchsorted")
value = self._validate_listlike(value)

rv = self._unbox(value)
return self._rebox_native(rv)
Expand All @@ -600,7 +599,7 @@ def _validate_setitem_value(self, value):
f"or array of those. Got '{type(value).__name__}' instead."
)
if is_list_like(value):
value = self._validate_listlike(value, "setitem")
value = self._validate_listlike(value)
else:
value = self._validate_scalar(value, msg)

Expand All @@ -622,7 +621,7 @@ def _validate_where_value(self, other):
if not is_list_like(other):
other = self._validate_scalar(other, msg)
else:
other = self._validate_listlike(other, "where")
other = self._validate_listlike(other)

return self._unbox(other, setitem=True)

Expand Down
4 changes: 1 addition & 3 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,9 +663,7 @@ def _wrap_joined_index(self, joined: np.ndarray, other):
@doc(Index._convert_arr_indexer)
def _convert_arr_indexer(self, keyarr):
try:
return self._data._validate_listlike(
keyarr, "convert_arr_indexer", allow_object=True
)
return self._data._validate_listlike(keyarr, allow_object=True)
except (ValueError, TypeError):
return com.asarray_tuplesafe(keyarr)

Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,10 @@ def test_setitem_raises(self):
def test_setitem_numeric_raises(self, arr1d, box):
# We dont case e.g. int64 to our own dtype for setitem

msg = "requires compatible dtype"
msg = (
f"value should be a '{arr1d._scalar_type.__name__}', "
"'NaT', or array of those. Got"
)
with pytest.raises(TypeError, match=msg):
arr1d[:2] = box([0, 1])

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def test_searchsorted_invalid_types(self, other, index):
msg = "|".join(
[
"searchsorted requires compatible dtype or scalar",
"Unexpected type for 'value'",
"value should be a 'Timestamp', 'NaT', or array of those. Got",
]
)
with pytest.raises(TypeError, match=msg):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/test_timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def test_searchsorted_invalid_types(self, other, index):
msg = "|".join(
[
"searchsorted requires compatible dtype or scalar",
"Unexpected type for 'value'",
"value should be a 'Timedelta', 'NaT', or array of those. Got",
]
)
with pytest.raises(TypeError, match=msg):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/period/test_searchsorted.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_searchsorted_invalid(self):
msg = "|".join(
[
"searchsorted requires compatible dtype or scalar",
"Unexpected type for 'value'",
"value should be a 'Period', 'NaT', or array of those. Got",
]
)
with pytest.raises(TypeError, match=msg):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/timedeltas/test_searchsorted.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ def test_searchsorted_different_argument_classes(self, klass):
)
def test_searchsorted_invalid_argument_dtype(self, arg):
idx = TimedeltaIndex(["1 day", "2 days", "3 days"])
msg = "searchsorted requires compatible dtype"
msg = "value should be a 'Timedelta', 'NaT', or array of those. Got"
with pytest.raises(TypeError, match=msg):
idx.searchsorted(arg)