python - 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'>
解决方案
用于numpy.isclose
处理数组,此处为列:
data['isclose'] = np.where(np.isclose(data['High'], data['lifetime'], rel_tol = 0.003), 1, 0)
推荐阅读
- windows - 高对比度隐藏菜单项文本
- mysql - 如何使用 INNER JOIN 进行选择,给出相关但不相关的记录
- cognitoforms - Cognito 表单预填充失败
- sql - 为什么表变量 SQL 不返回 R Markdown 中的值?
- git - CephFs 和 CephRBD 对 git clone 的速度很慢
- flutter - 为什么控制台中没有显示错误?
- jquery - 当页面在 Laravel 上重新加载时如何保留选定的表
- sed - 使用 sed 在树结构中打印文本行
- php - 在没有作曲家的情况下使用 Google 电子表格 API:如何使其工作?
- jquery - 保持索引紧凑并与 jquery 中动态生成的输入同步