pandas AttributeError: 'Series' 对象没有属性 'searchsorted' 熊猫

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22669208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:51:21  来源:igfitidea点击:

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

pythonpandasseries

提问by user3465447

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

我在第 38 页复制了用于数据分析的书 python 的代码

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

我不明白为什么我重新安装了 numpy 和 lib pandas 它仍然无法工作它不是 pandas 文档中的系列搜索排序方法

In [49]:

在 [49] 中:

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

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

回答by EdChum

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:

您可能正在使用 0.13.0 或更高版本的版本,其中 Series 现在是 subclasses NDFrame,您现在必须执行此操作以返回一个 numpy 数组:

prop_cumsum.values.searchsorted(0.5)

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

因为 searchsorted 是一个 numpy 函数而不是 Pandas Series 函数。

See the online docs

查看在线文档