Repository: pandas-dev/pandas

Issue:
ENH: define searchsorted on a Series
http://stackoverflow.com/questions/22669208/attributeerror-series-object-has-no-attribute-searchsorted-pandas

essentially a pass thru to `np.searchsorted`
- should return the index (rather than indices)
- can better handle `datetime64[ns]` (e.g. coerce input to/fro)
- could check if sorted internally (e.g. `is_monotonic` on the index), other pass the `sorter` argument)



Discussion:

Resolving patch:
diff --git a/pandas/core/series.py b/pandas/core/series.py
--- a/pandas/core/series.py
+++ b/pandas/core/series.py
@@ -1373,6 +1373,62 @@ def dot(self, other):
         else:  # pragma: no cover
             raise TypeError('unsupported type: %s' % type(other))
 
+    def searchsorted(self, v, side='left', sorter=None):
+        """Find indices where elements should be inserted to maintain order.
+
+        Find the indices into a sorted Series `self` such that, if the
+        corresponding elements in `v` were inserted before the indices, the
+        order of `self` would be preserved.
+
+        Parameters
+        ----------
+        v : array_like
+            Values to insert into `a`.
+        side : {'left', 'right'}, optional
+            If 'left', the index of the first suitable location found is given.
+            If 'right', return the last such index.  If there is no suitable
+            index, return either 0 or N (where N is the length of `a`).
+        sorter : 1-D array_like, optional
+            Optional array of integer indices that sort `self` into ascending
+            order. They are typically the result of ``np.argsort``.
+
+        Returns
+        -------
+        indices : array of ints
+            Array of insertion points with the same shape as `v`.
+
+        See Also
+        --------
+        Series.sort
+        Series.order
+        numpy.searchsorted
+
+        Notes
+        -----
+        Binary search is used to find the required insertion points.
+
+        Examples
+        --------
+        >>> x = pd.Series([1, 2, 3])
+        >>> x
+        0    1
+        1    2
+        2    3
+        dtype: int64
+        >>> x.searchsorted(4)
+        array([3])
+        >>> x.searchsorted([0, 4])
+        array([0, 3])
+        >>> x.searchsorted([1, 3], side='left')
+        array([0, 2])
+        >>> x.searchsorted([1, 3], side='right')
+        array([1, 3])
+        >>> x.searchsorted([1, 2], side='right', sorter=[0, 2, 1])
+        array([1, 3])
+        """
+        return self.values.searchsorted(Series(v).values, side=side,
+                                        sorter=sorter)
+
     #------------------------------------------------------------------------------
     # Combination
 


Test patch:
diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py
--- a/pandas/tests/test_series.py
+++ b/pandas/tests/test_series.py
@@ -5956,7 +5956,6 @@ def test_replace_with_single_list(self):
             s.replace([1,2,3],inplace=True,method='crash_cymbal')
         assert_series_equal(s, ser)
 
-
     def test_replace_mixed_types(self):
         s = Series(np.arange(5),dtype='int64')
 
@@ -6164,6 +6163,37 @@ def test_concat_empty_series_dtypes(self):
         self.assertEqual(pd.concat([Series(dtype=np.bool_),
                                     Series(dtype=np.int32)]).dtype, np.int32)
 
+    def test_searchsorted_numeric_dtypes_scalar(self):
+        s = Series([1, 2, 90, 1000, 3e9])
+        r = s.searchsorted(30)
+        e = 2
+        tm.assert_equal(r, e)
+
+        r = s.searchsorted([30])
+        e = np.array([2])
+        tm.assert_array_equal(r, e)
+
+    def test_searchsorted_numeric_dtypes_vector(self):
+        s = Series([1, 2, 90, 1000, 3e9])
+        r = s.searchsorted([91, 2e6])
+        e = np.array([3, 4])
+        tm.assert_array_equal(r, e)
+
+    def test_search_sorted_datetime64_scalar(self):
+        s = Series(pd.date_range('20120101', periods=10, freq='2D'))
+        v = pd.Timestamp('20120102')
+        r = s.searchsorted(v)
+        e = 1
+        tm.assert_equal(r, e)
+
+    def test_search_sorted_datetime64_list(self):
+        s = Series(pd.date_range('20120101', periods=10, freq='2D'))
+        v = [pd.Timestamp('20120102'), pd.Timestamp('20120104')]
+        r = s.searchsorted(v)
+        e = np.array([1, 2])
+        tm.assert_array_equal(r, e)
+
+
 
 
 class TestSeriesNonUnique(tm.TestCase):
diff --git a/pandas/util/testing.py b/pandas/util/testing.py
--- a/pandas/util/testing.py
+++ b/pandas/util/testing.py
@@ -21,6 +21,7 @@
 
 from numpy.random import randn, rand
 import numpy as np
+from numpy.testing import assert_array_equal
 
 import pandas as pd
 from pandas.core.common import _is_sequence, array_equivalent