Repository: pandas-dev/pandas

Issue:
BUG/DOC: searchsorted with sorter giving ValueError could not parse sorter
I get an error while building the docs with `searchsorted` (but there is not error on the travis docs, http://pandas-docs.github.io/pandas-docs-travis/basics.html#sorting-by-index-and-value):

```
In [22]: ser = Series([3, 1, 2])

In [23]: a = ser.values

In [24]: a.searchsorted([0,3], sorter=np.argsort(a))
Out[24]: array([0, 2])

In [25]: ser.searchsorted([0,3], sorter=np.argsort(ser))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-25-d96921d13837> in <module>()
----> 1 ser.searchsorted([0,3], sorter=np.argsort(ser))

/home/joris/scipy/pandas/pandas/core/series.pyc in searchsorted(self, v, side, sorter)
   1431         """
   1432         return self.values.searchsorted(Series(v).values, side=side,
-> 1433                                         sorter=sorter)
   1434 
   1435     #------------------------------------------------------------------------------

ValueError: could not parse sorter argument

In [26]: pd.__version__ 
Out[26]: '0.14.1-641-gd65c9d7'

In [27]: np.__version__ 
Out[27]: '1.9.0'
```



Discussion:
@cpcloud any idea?

Probably needs getattr(sorter, "values", sorter)

Though I thhink I have similar versions so maybe something in the array interface is broken. 

Depending on where I place `values` it works:

```
In [28]: ser.searchsorted([0,3], sorter=np.argsort(ser.values))
Out[28]: array([0, 2])

In [29]: ser.searchsorted([0,3], sorter=np.argsort(ser).values)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-29-b2017c4a68af> in <module>()
----> 1 ser.searchsorted([0,3], sorter=np.argsort(ser).values)

/home/joris/scipy/pandas/pandas/core/series.pyc in searchsorted(self, v, side, sorter)
   1431         """
   1432         return self.values.searchsorted(Series(v).values, side=side,
-> 1433                                         sorter=sorter)
   1434 
   1435     #------------------------------------------------------------------------------

ValueError: could not parse sorter argument

In [30]: np.argsort(ser).values
Out[30]: array([1, 2, 0], dtype=int64)

In [31]: np.argsort(ser.values)
Out[31]: array([1, 2, 0])

In [32]: np.argsort(ser.values).dtype
Out[32]: dtype('int32')
```

something is fishy there, can you show the output of `np.argsort(ser)`?

``` python
In [12]: ser = pd.Series([3,1,2])

In [13]: ser
Out[13]:
0    3
1    1
2    2
dtype: int64

In [14]: ser.searchsorted([0,3],sorter=ser.argsort())
Out[14]: array([0, 2])

In [15]: ser.searchsorted([0,3],sorter=np.argsort(ser))
Out[15]: array([0, 2])

In [16]: ser.searchsorted([0,3],sorter=np.argsort(ser).values)
Out[16]: array([0, 2])

In [17]: ser.searchsorted([0,3],sorter=np.argsort(ser.values))
Out[17]: array([0, 2])
```

both work fine for me under numpy 1.8/1.9 

@jorisvandenbossche are you picking up the installed version when you are showing this?

@jreback which installed version? of pandas? it's my built dev version it is picking up correctly:

```
In [33]: pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.8.final.0
python-bits: 32
OS: Linux
OS-release: 3.2.0-69-generic-pae
machine: i686
processor: i686
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.14.1-641-gd65c9d7
nose: 1.3.4
Cython: 0.20.2
numpy: 1.9.0
scipy: 0.14.0
...
```

fwiw, it is 32 bit, which causes a difference between `np.argsort(ser.values)` and `np.argsort(ser).values`

ahh, could definitely be a 32-bit issue...hmm

@jorisvandenbossche 

try this

```
diff --git a/pandas/core/series.py b/pandas/core/series.py
index f313352..dca5df8 100644
--- a/pandas/core/series.py
+++ b/pandas/core/series.py
@@ -1429,8 +1429,8 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
         >>> x.searchsorted([1, 2], side='right', sorter=[0, 2, 1])
         array([1, 3])
         """
-        return self.values.searchsorted(Series(v).values, side=side,
-                                        sorter=sorter)
+        return self.values.searchsorted(com._ensure_platform_int(Series(v).values), side=side,
+                                        sorter=com._ensure_platform_int(sorter))
```

ah sorry about this. i tend to be biased against 32-bit because i haven't had to use a 32-bit machine a long while, my apologies.

This _would_ have failed on windows for me, but we don't have that doc-entry as a test.

@jorisvandenbossche if that works, pls add that as a test as well

yes, thanks, that is working, will do a PR


Resolving patch:
diff --git a/pandas/core/series.py b/pandas/core/series.py
--- a/pandas/core/series.py
+++ b/pandas/core/series.py
@@ -1429,6 +1429,9 @@ def searchsorted(self, v, side='left', sorter=None):
         >>> x.searchsorted([1, 2], side='right', sorter=[0, 2, 1])
         array([1, 3])
         """
+        if sorter is not None:
+            sorter = com._ensure_platform_int(sorter)
+
         return self.values.searchsorted(Series(v).values, side=side,
                                         sorter=sorter)
 


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
@@ -6199,6 +6199,12 @@ def test_search_sorted_datetime64_list(self):
         e = np.array([1, 2])
         tm.assert_array_equal(r, e)
 
+    def test_searchsorted_sorter(self):
+        # GH8490
+        s = Series([3, 1, 2])
+        r = s.searchsorted([0, 3], sorter=np.argsort(s))
+        e = np.array([0, 2])
+        tm.assert_array_equal(r, e)