Skip to content

Commit c4bde9f

Browse files
committed
Make common impl. with Index.searchsorted part II
1 parent ea241c6 commit c4bde9f

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

pandas/core/common.py

+17-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from pandas.compat import iteritems, PY2, PY36, OrderedDict
1717
from pandas.core.dtypes.generic import ABCSeries, ABCIndex, ABCIndexClass
1818
from pandas.core.dtypes.common import (is_integer, is_integer_dtype,
19+
is_float_dtype, is_object_dtype,
20+
is_categorical_dtype,
1921
is_numeric_dtype, is_number,
2022
is_scalar, ensure_platform_int)
2123
from pandas.core.dtypes.inference import _iterable_not_string
@@ -447,10 +449,10 @@ def ensure_within_integer_bounds(value, dtype):
447449
ValueError : if value is outside the bounds set in iinfo(dtype)
448450
"""
449451
if PY2:
450-
# python 2 allows e.g. "a" < 1, avoid this
452+
# python 2 allows "a" < 1, avoid such nonsense
451453
if not (is_number(value) or is_numeric_dtype(value)):
452454
msg = "value must be a number, was type {}"
453-
raise ValueError(msg.format(value))
455+
raise TypeError(msg.format(value))
454456

455457
# check if value is within integer bounds
456458
iinfo = np.iinfo(dtype)
@@ -481,24 +483,30 @@ def searchsorted_integer(arr, value, side="left", sorter=None):
481483
# but float 2.2 should *not* be converted to int 2
482484
value = np.asarray(value, dtype=dtype)
483485

484-
return arr.searchsorted(value, side=side, sorter=sorter)
486+
return np.searchsorted(arr, value, side=side, sorter=sorter)
485487

486488

487489
def searchsorted(arr, value, side="left", sorter=None):
488490
"""
489-
Do a arr.searchsorted(value) with adjustments for dtypes.
491+
Find indices where elements should be inserted to maintain order.
490492
491-
:func:`numpy.searchsorted` is only fast if value is of same dtype
492-
as the searched array. Else numpy recasts arr to a higher dtype, which
493-
causes a slowdown. Below we ensure that value has the right dtype
494-
for giving fast results for arr.searchsorted, when possible.
493+
Find the indices into a sorted array-like `arr` such that, if the
494+
corresponding elements in `value` were inserted before the indices,
495+
the order of `arr` would be preserved.
495496
496-
See :meth:`Index.searchsorted` for details on parameters and return value.
497+
See :class:`IndexOpsMixin.searchsorted` for more details and examples.
497498
"""
498499
if sorter is not None:
499500
sorter = ensure_platform_int(sorter)
500501

501502
if is_integer_dtype(arr):
502503
return searchsorted_integer(arr, value, side=side, sorter=sorter)
504+
elif (is_object_dtype(arr) or is_float_dtype(arr) or
505+
is_categorical_dtype(arr)):
506+
return arr.searchsorted(value, side=side, sorter=sorter)
503507
else:
508+
# fallback solution. E.g. arr is an array with dtype='datetime64[ns]'
509+
# and value is a pd.Timestamp
510+
from pandas.core.series import Series
511+
value = Series(value)._values
504512
return arr.searchsorted(value, side=side, sorter=sorter)

pandas/core/series.py

+2-12
Original file line numberDiff line numberDiff line change
@@ -2087,18 +2087,8 @@ def __rmatmul__(self, other):
20872087
@Appender(base._shared_docs['searchsorted'])
20882088
@deprecate_kwarg(old_arg_name='v', new_arg_name='value')
20892089
def searchsorted(self, value, side='left', sorter=None):
2090-
simple_types = (is_integer_dtype, is_float_dtype, is_object_dtype,
2091-
is_categorical_dtype)
2092-
2093-
if any(is_dtype(self) for is_dtype in simple_types):
2094-
result = com.searchsorted(self._values, value,
2095-
side=side, sorter=sorter)
2096-
else:
2097-
# e.g. self is datetimelike and value is a pd.Timestamp
2098-
if sorter is not None:
2099-
sorter = ensure_platform_int(sorter)
2100-
value = Series(value)._values
2101-
result = self._values.searchsorted(value, side=side, sorter=sorter)
2090+
result = com.searchsorted(self._values, value,
2091+
side=side, sorter=sorter)
21022092

21032093
if is_scalar(result):
21042094
# ensure that a 1-dim array is returned

0 commit comments

Comments
 (0)