9

I reproduce the code of book python for data analysis in page 38

I write

prop_cumsum = df.sort_index(by='prop', ascending=False).prop.cumsum()

and prop_cumsum.searchsorted(0.5)

Then there is an error say:

AttributeError                            Traceback (most recent call last)
<ipython-input-30-f2e2bb3f5ba0> in <module>()
----> 1 prop_cumsum.searchsorted(0.5)

C:\Users\xxx\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\generic.pyc in __getattr__(self, name)
   1813                 return self[name]
   1814             raise AttributeError("'%s' object has no attribute '%s'" %
-> 1815                                  (type(self).__name__, name))
   1816 
   1817     def __setattr__(self, name, value):

AttributeError: 'Series' object has no attribute 'searchsorted' 

I can't understand why i re-install numpy and lib pandas it still can't work It's no searchsorted methode in series in the document of pandas

In [49]:

http://nbviewer.ipython.org/github/lexual/pydata-book/blob/35fd20645c75128ae348a275848575e2eae7a025/ch02_us_baby_names.ipynb

1

1 Answer 1

14

You are probably using a version that is 0.13.0 or later where Series now subclasses NDFrame, you have to now do this to return a numpy array:

prop_cumsum.values.searchsorted(0.5)

as searchsorted is a numpy function and not a Pandas Series function.

See the online docs

Sign up to request clarification or add additional context in comments.

6 Comments

Annoying that this doesn't work: np.searchsorted(prop_cumsum, 0.5)
@AndyHayden yes I agree it would be nice to be able to achieve this
I that is another case of numpy not calling __array__ properly
@EdChum I don't see a problem with actually defining searchsorted on a Series btw (and then in theory could properly handle dtypes, e.g. datetime64[ns], want to do a PR? (and it would return the index, rather than the indicies; as an aside also easy to auto-sort this too, e.g. check Series.index.is_monotonic``)
@Jeff Do we have an issue to keep track of all places where numpy does that?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.