Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Series method replaced searchsorted?

Tags:

python

pandas

In his video, [Data analysis in Python with pandas] (http://youtu.be/w26x-z-BdWQ?t=2h14s), Wes McKinney presents a series method names searchsorted(), which given a value, gives back the index in which the series is crossing that value. It appears this function is not available any more, did something else replace it?

like image 445
idoda Avatar asked Feb 17 '14 07:02

idoda


People also ask

What is searchsorted in numpy?

searchsorted() function is used to find the indices into a sorted array arr such that, if elements are inserted before the indices, the order of arr would be still preserved. Here, binary search is used to find the required insertion indices.

How can you change the index of a Pandas series example?

The pandas series constructor will automatically create series index labels based on the given data. If you want to specify those index labels, we can give those index values separately by using the index keyword argument of the pandas series function.


1 Answers

I believe this is due to the refactoring that occurred in Pandas 0.13.0 where Pandas Series now sub-class NDFrame rather than ndarray see this:

In [33]:

import pandas as pd
import numpy as np
df = pd.DataFrame({'a':arange(10)})
df
Out[33]:

   a
0  0
1  1
2  2
3  3
4  4
5  5
6  6
7  7
8  8
9  9

[10 rows x 1 columns]

[10 rows x 3 columns]
In [28]:

# you now have to call `.values` to return a ndarray 
df.a.values.cumsum().searchsorted(11)
Out[28]:
5

Now compare what happens if we use a numpy array:

In [29]:

temp = np.array(arange(10))

In [32]:

temp.cumsum().searchsorted(11)
Out[32]:
5
like image 200
EdChum Avatar answered Nov 15 '22 11:11

EdChum