简体   繁体   English

np.where 和 np.select 返回错误。 AttributeError: 'numpy.ndarray' 对象没有属性 'select'

[英]np.where and np.select returning error. AttributeError: 'numpy.ndarray' object has no attribute 'select'

Trying to use np.select or np.where to run a method on a DataFrame Column.尝试使用 np.select 或 np.where 在 DataFrame 列上运行方法。 Either way I do it, I get the error saying the "object has no attribute 'select' (or 'where' if I use np.where). When I convert the column and check the specific column type, it returns as an numpy.ndarray. According to the numpy docs, both np.where and np.select should work on numpy arrays.无论采用哪种方式,我都会收到错误消息,指出“对象没有属性‘select’(或‘where’,如果我使用 np.where)。当我转换列并检查特定的列类型时,它返回为一个 numpy .ndarray. 根据 numpy 文档, np.wherenp.select都应该在 numpy 数组上工作。

Checking type on the Series I converted to a numpy array, after checking the type I try the method and receive the errors:检查系列上的类型我转换为 numpy 数组,在检查类型后我尝试该方法并收到错误:

type(np.array(daily_std_df['Daily_Std']))
numpy.ndarray

type(np.array(x))
numpy.ndarray

### Apply method to the data frame using np.where OR np.select:
x = daily_std_df['Daily_Std']
conditionList = [x > SP_500_std, x < SP_500_std]
choiceList = ['High', "Not High"]
daily_std_df["Risk"] = np.array(x).select(conditionList, choiceList)
daily_std_df
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-51-42673f1eeafa> in <module>
      3 conditionList = [x > SP_500_std, x < SP_500_std]
      4 choiceList = ['High', "Not High"]
----> 5 daily_std_df["Risk"] = np.array(x).select(conditionList, choiceList)
      6 daily_std_df

AttributeError: 'numpy.ndarray' object has no attribute 'select'

It is not np.array which has the select attribute, it's just simply np that has the attribute.这不是np.array它具有select属性,它只是简单的np具有属性。

So instead of:所以而不是:

daily_std_df["Risk"] = np.array(x).select(conditionList, choiceList)

Try this:试试这个:

daily_std_df["Risk"] = np.select(conditionList, choiceList)

Read the docs for more examples.阅读文档以获取更多示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 np.where 和 np.select 的条件语句 - Conditional statements using np.where and np.select AttributeError: &#39;numpy.ndarray&#39; 对象没有属性 &#39;where&#39; - AttributeError: 'numpy.ndarray' object has no attribute 'where' 如何使用 np.where 和 np.select 在 python 中编写条件逻辑和列表理解 - How to write conditional logic and list comprehension in python using np.where and np.select AttributeError:“ numpy.ndarray”对象没有属性“ A” - AttributeError: 'numpy.ndarray' object has no attribute 'A' AttributeError: 'numpy.ndarray' object has no attribute 'score' 错误 - AttributeError: 'numpy.ndarray' object has no attribute 'score' error AttributeError: 'numpy.ndarray' object 没有属性 'append' 错误 - AttributeError: 'numpy.ndarray' object has no attribute 'append' error np.array返回带有“…”的numpy.ndarray - np.array returning numpy.ndarray with “…” Numpy和Matplotlib-AttributeError:“ numpy.ndarray”对象没有属性“ replace” - Numpy and Matplotlib - AttributeError: 'numpy.ndarray' object has no attribute 'replace' AttributeError: &#39;numpy.ndarray&#39; 对象没有属性 &#39;insert&#39; - AttributeError: 'numpy.ndarray' object has no attribute 'insert' AttributeError - &#39;numpy.ndarray&#39; 对象没有属性 &#39;drop&#39; - AttributeError - 'numpy.ndarray' object has no attribute 'drop'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM