【问题标题】:AttributeError: 'Series' object has no attribute 'searchsorted' pandasAttributeError:“系列”对象没有属性“搜索排序”熊猫
【发布时间】:2014-05-05 08:09:29
【问题描述】:

我在第38页重现了本书python用于数据分析的代码

我写

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

and prop_cumsum.searchsorted(0.5)

然后有一个错误说:

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' 

我不明白为什么 我重新安装 numpy 和 lib pandas 它仍然无法工作 在 pandas 的文档中不是串联的 searchsorted 方法

在 [49] 中:

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

【问题讨论】:

标签: python pandas series


【解决方案1】:

您可能使用的是 0.13.0 或更高版本,其中 Series 现在是 NDFrame 的子类,您现在必须这样做才能返回一个 numpy 数组:

prop_cumsum.values.searchsorted(0.5)

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

online docs

【讨论】:

  • 烦人的是这不起作用:np.searchsorted(prop_cumsum, 0.5)
  • @AndyHayden 是的,我同意能够实现这一目标会很好
  • 我是另一个 numpy 没有正确调用 __array__ 的情况
  • @EdChum 我认为在系列 btw 上实际定义 searchsorted 没有问题(然后理论上可以正确处理 dtypes,例如 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 我们是否有问题要跟踪 numpy 执行此操作的所有地方?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-11
  • 2016-03-27
  • 2021-09-09
  • 1970-01-01
  • 1970-01-01
  • 2016-08-19
  • 2019-10-02
相关资源
最近更新 更多