Skip to content

Insertion Point #6830

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

Closed
cancan101 opened this issue Apr 7, 2014 · 5 comments
Closed

Insertion Point #6830

cancan101 opened this issue Apr 7, 2014 · 5 comments

Comments

@cancan101
Copy link
Contributor

Is there something like this: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#binarySearch(float[], float) in Pandas that returns the insertion point (index) for a given value in a given Series?

@cpcloud
Copy link
Member

cpcloud commented Apr 7, 2014

You can do

In [18]: s = Series(arange(10).tolist() + [14, 15])

In [19]: s
Out[19]:
0      0
1      1
2      2
3      3
4      4
5      5
6      6
7      7
8      8
9      9
10    14
11    15
dtype: int64

In [20]: s.values.searchsorted(11)
Out[20]: 10

In [21]: s.index[s.values.searchsorted(11)]
Out[21]: 10

@cpcloud
Copy link
Member

cpcloud commented Apr 7, 2014

There's something strange that happens when you call the toplevel numpy version. I think @jreback answered an SO question about this recently.

In [29]: s
Out[29]:
0      0
1      1
2      2
3      3
4      4
5      5
6      6
7      7
8      8
9      9
10    14
11    15
dtype: int64

In [30]: searchsorted(s, [11])
Out[30]:
0     10
1     10
2     10
3     10
4     10
5     10
6     10
7     10
8     10
9     10
10    10
11    10
dtype: int64

I think the _wrapit uses the constructor and the size of the passed object to recreate the result therefore something like

Series([10] * s.size)

gets called.

@cpcloud
Copy link
Member

cpcloud commented Apr 7, 2014

I wonder if wrapping this might be useful. Seems simple enough. @cancan101 Care to do a PR?

@jreback
Copy link
Contributor

jreback commented Apr 7, 2014

see #6712

@cpcloud
Copy link
Member

cpcloud commented Apr 7, 2014

right then, closing.

@cpcloud cpcloud closed this as completed Apr 7, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants