Skip to content

Commit 8dcf9b1

Browse files
authored
CLN: consolidate exception messages in datetimelike validate_listlike (#37229)
1 parent fc951f8 commit 8dcf9b1

File tree

7 files changed

+16
-16
lines changed

7 files changed

+16
-16
lines changed

pandas/core/arrays/datetimelike.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ def _validate_comparison_value(self, other, opname: str):
444444

445445
else:
446446
try:
447-
other = self._validate_listlike(other, opname, allow_object=True)
447+
other = self._validate_listlike(other, allow_object=True)
448448
self._check_compatible_with(other)
449449
except TypeError as err:
450450
if is_object_dtype(getattr(other, "dtype", None)):
@@ -548,7 +548,7 @@ def _validate_scalar(self, value, msg: Optional[str] = None):
548548

549549
return value
550550

551-
def _validate_listlike(self, value, opname: str, allow_object: bool = False):
551+
def _validate_listlike(self, value, allow_object: bool = False):
552552
if isinstance(value, type(self)):
553553
return value
554554

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

579579
elif not type(self)._is_recognized_dtype(value.dtype):
580580
raise TypeError(
581-
f"{opname} requires compatible dtype or scalar, "
582-
f"not {type(value).__name__}"
581+
f"value should be a '{self._scalar_type.__name__}', 'NaT', "
582+
f"or array of those. Got '{type(value).__name__}' instead."
583583
)
584-
585584
return value
586585

587586
def _validate_searchsorted_value(self, value):
588587
msg = "searchsorted requires compatible dtype or scalar"
589588
if not is_list_like(value):
590589
value = self._validate_scalar(value, msg)
591590
else:
592-
value = self._validate_listlike(value, "searchsorted")
591+
value = self._validate_listlike(value)
593592

594593
rv = self._unbox(value)
595594
return self._rebox_native(rv)
@@ -600,7 +599,7 @@ def _validate_setitem_value(self, value):
600599
f"or array of those. Got '{type(value).__name__}' instead."
601600
)
602601
if is_list_like(value):
603-
value = self._validate_listlike(value, "setitem")
602+
value = self._validate_listlike(value)
604603
else:
605604
value = self._validate_scalar(value, msg)
606605

@@ -622,7 +621,7 @@ def _validate_where_value(self, other):
622621
if not is_list_like(other):
623622
other = self._validate_scalar(other, msg)
624623
else:
625-
other = self._validate_listlike(other, "where")
624+
other = self._validate_listlike(other)
626625

627626
return self._unbox(other, setitem=True)
628627

pandas/core/indexes/datetimelike.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -663,9 +663,7 @@ def _wrap_joined_index(self, joined: np.ndarray, other):
663663
@doc(Index._convert_arr_indexer)
664664
def _convert_arr_indexer(self, keyarr):
665665
try:
666-
return self._data._validate_listlike(
667-
keyarr, "convert_arr_indexer", allow_object=True
668-
)
666+
return self._data._validate_listlike(keyarr, allow_object=True)
669667
except (ValueError, TypeError):
670668
return com.asarray_tuplesafe(keyarr)
671669

pandas/tests/arrays/test_datetimelike.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,10 @@ def test_setitem_raises(self):
416416
def test_setitem_numeric_raises(self, arr1d, box):
417417
# We dont case e.g. int64 to our own dtype for setitem
418418

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

pandas/tests/arrays/test_datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ def test_searchsorted_invalid_types(self, other, index):
396396
msg = "|".join(
397397
[
398398
"searchsorted requires compatible dtype or scalar",
399-
"Unexpected type for 'value'",
399+
"value should be a 'Timestamp', 'NaT', or array of those. Got",
400400
]
401401
)
402402
with pytest.raises(TypeError, match=msg):

pandas/tests/arrays/test_timedeltas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def test_searchsorted_invalid_types(self, other, index):
136136
msg = "|".join(
137137
[
138138
"searchsorted requires compatible dtype or scalar",
139-
"Unexpected type for 'value'",
139+
"value should be a 'Timedelta', 'NaT', or array of those. Got",
140140
]
141141
)
142142
with pytest.raises(TypeError, match=msg):

pandas/tests/indexes/period/test_searchsorted.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def test_searchsorted_invalid(self):
6262
msg = "|".join(
6363
[
6464
"searchsorted requires compatible dtype or scalar",
65-
"Unexpected type for 'value'",
65+
"value should be a 'Period', 'NaT', or array of those. Got",
6666
]
6767
)
6868
with pytest.raises(TypeError, match=msg):

pandas/tests/indexes/timedeltas/test_searchsorted.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ def test_searchsorted_different_argument_classes(self, klass):
2121
)
2222
def test_searchsorted_invalid_argument_dtype(self, arg):
2323
idx = TimedeltaIndex(["1 day", "2 days", "3 days"])
24-
msg = "searchsorted requires compatible dtype"
24+
msg = "value should be a 'Timedelta', 'NaT', or array of those. Got"
2525
with pytest.raises(TypeError, match=msg):
2626
idx.searchsorted(arg)

0 commit comments

Comments
 (0)