【问题标题】:np.where and np.select returning error. AttributeError: 'numpy.ndarray' object has no attribute 'select'np.where 和 np.select 返回错误。 AttributeError:“numpy.ndarray”对象没有属性“select”
【发布时间】:2021-09-24 20:51:54
【问题描述】:

尝试使用 np.select 或 np.where 在 DataFrame 列上运行方法。无论哪种方式,我都会收到错误消息,提示“对象没有属性'select'(如果我使用np.where,则为'where')。当我转换列并检查特定的列类型时,它会以numpy形式返回.ndarray。根据 numpy 文档,np.wherenp.select 都应该适用于 numpy 数组。

在我转换为 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'

【问题讨论】:

  • 你为什么使用np.array(x).select?如果您认为这是正确的,请用文档证明它的合理性!
  • selectnympy 中的 function,而不是 method 数组中的 method

标签: python numpy numpy-ndarray attributeerror


【解决方案1】:

不是np.array 具有select 属性,而只是np 具有该属性。

所以而不是:

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

试试这个:

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

阅读docs 了解更多示例。

【讨论】:

    猜你喜欢
    • 2020-01-25
    • 2020-12-03
    • 2020-11-29
    • 2020-10-06
    • 2018-01-25
    • 2016-06-29
    • 2020-03-25
    • 2013-12-07
    • 2017-10-16
    相关资源
    最近更新 更多