【问题标题】:TypeError: cannot convert the series to <class 'float'> in pandasTypeError:无法将系列转换为熊猫中的 <class 'float'>
【发布时间】:2021-10-15 18:56:23
【问题描述】:

我有以下代码来测试一只股票是否创造了新的生命周期新高。 Bool 输出应该使用 numpy 比较生成。

import pandas as pd
from pandas_datareader import data as web
import numpy as np
import math

data = web.DataReader('goog', 'yahoo')
data['lifetime'] = data['High'].asfreq('D').rolling(window=999999, min_periods=1).max() #To check if it is a lifetime high
data['lifetime'] = data['lifetime'].astype(float)
data['isclose'] = np.where(math.isclose(data['High'], data['lifetime'], rel_tol = 0.003), 1, 0)

iterating over each row in pandas to evaluate condition 此处提供的此解决方案应该可以工作,但我收到此错误。

TypeError: 无法将系列转换为

我看了一些,似乎math 函数需要一个值而不是数组。我怎样才能验证它是这种情况? TypeError: cannot convert the series to <class 'float'>

【问题讨论】:

    标签: python pandas numpy math


    【解决方案1】:

    使用numpy.isclose 处理数组,此处为列:

    data['isclose'] = np.where(np.isclose(data['High'], data['lifetime'], rel_tol = 0.003), 1, 0)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-12
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 2017-10-16
      相关资源
      最近更新 更多