在熊猫,python,pandas,numpy,math"/>

首页 > 解决方案 > TypeError:无法将系列转换为在熊猫

问题描述

我有下面的代码来测试一只股票是否创造了新的生命周期新高。布尔输出应使用 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)

此处提供的此解决方案遍历 pandas 中的每一行以评估条件,应该可以工作,但我收到此错误。

TypeError:无法将系列转换为 <class 'float'>

我做了一些查看,似乎该math函数需要一个值而不是数组。我如何验证是否是这种情况?TypeError:无法将系列转换为 <class 'float'>

标签: pythonpandasnumpymath

解决方案


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

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

推荐阅读