Skip to content

ENH/BUG: consistently cast strs to datetimelike for searchsorted #36346

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 4 commits into from
Sep 17, 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ Other enhancements
- :meth:`DataFrame.explode` and :meth:`Series.explode` now support exploding of sets (:issue:`35614`)
- `Styler` now allows direct CSS class name addition to individual data cells (:issue:`36159`)
- :meth:`Rolling.mean()` and :meth:`Rolling.sum()` use Kahan summation to calculate the mean to avoid numerical problems (:issue:`10319`, :issue:`11645`, :issue:`13254`, :issue:`32761`, :issue:`36031`)
- :meth:`DatetimeIndex.searchsorted`, :meth:`TimedeltaIndex.searchsorted`, :meth:`PeriodIndex.searchsorted`, and :meth:`Series.searchsorted` with datetimelike dtypes will now try to cast string arguments (listlike and scalar) to the matching datetimelike type (:issue:`36346`)

.. _whatsnew_120.api_breaking.python:

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,8 +845,7 @@ def _validate_searchsorted_value(self, value):
if not is_list_like(value):
value = self._validate_scalar(value, msg, cast_str=True)
else:
# TODO: cast_str? we accept it for scalar
value = self._validate_listlike(value, "searchsorted")
value = self._validate_listlike(value, "searchsorted", cast_str=True)

rv = self._unbox(value)
return self._rebox_native(rv)
Expand Down
8 changes: 0 additions & 8 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,6 @@ def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):

@doc(IndexOpsMixin.searchsorted, klass="Datetime-like Index")
def searchsorted(self, value, side="left", sorter=None):
if isinstance(value, str):
raise TypeError(
"searchsorted requires compatible dtype or scalar, "
f"not {type(value).__name__}"
)
if isinstance(value, Index):
value = value._data

return self._data.searchsorted(value, side=side, sorter=sorter)

_can_hold_na = True
Expand Down
41 changes: 41 additions & 0 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,47 @@ def test_searchsorted(self):
else:
assert result == 10

@pytest.mark.parametrize("box", [None, "index", "series"])
def test_searchsorted_castable_strings(self, arr1d, box):
if isinstance(arr1d, DatetimeArray):
tz = arr1d.tz
if (
tz is not None
and tz is not pytz.UTC
and not isinstance(tz, pytz._FixedOffset)
):
# If we have e.g. tzutc(), when we cast to string and parse
# back we get pytz.UTC, and then consider them different timezones
# so incorrectly raise.
pytest.xfail(reason="timezone comparisons inconsistent")

arr = arr1d
if box is None:
pass
elif box == "index":
# Test the equivalent Index.searchsorted method while we're here
arr = self.index_cls(arr)
else:
# Test the equivalent Series.searchsorted method while we're here
arr = pd.Series(arr)

# scalar
result = arr.searchsorted(str(arr[1]))
assert result == 1

result = arr.searchsorted(str(arr[2]), side="right")
assert result == 3

result = arr.searchsorted([str(x) for x in arr[1:3]])
expected = np.array([1, 2], dtype=np.intp)
tm.assert_numpy_array_equal(result, expected)

with pytest.raises(TypeError):
arr.searchsorted("foo")

with pytest.raises(TypeError):
arr.searchsorted([str(arr[1]), "baz"])

def test_getitem_2d(self, arr1d):
# 2d slicing on a 1D array
expected = type(arr1d)(arr1d._data[:, np.newaxis], dtype=arr1d.dtype)
Expand Down