【问题标题】:Python function stop working after some iterationsPython函数在一些迭代后停止工作
【发布时间】:2020-06-06 05:09:50
【问题描述】:
from yahoo_fin.stock_info import *
n=0
Ptf_opp_2=[]
lenght_2=len(Ptf_opp)

def step2():
    for n in range(0,lenght_2):
        Bs=get_balance_sheet(Ptf_opp[n])
        tca=Bs.loc[Bs.get('Breakdown').str.match('Total Current Assets')]
        tl=Bs.loc[Bs.get('Breakdown').str.match('Total Liabilities')]
        tcl=Bs.loc[Bs.get('Breakdown').str.match('Total Current Liabilities')]
        if (tca.iloc[0,1] != None and \
            tca.iloc[0,1] != '-' and \
            tl.iloc[0,1] != None and \
            tl.iloc[0,1] != '-' and \
            tcl.iloc[0,1] != None and \
            tcl.iloc[0,1] != '-' and \
            (float(tcl.iloc[0,1]) / float(tca.iloc[0,1])) < 1.1 and\
            (float(tca.iloc[0,1]) / float(tl.iloc[0,1])) > 1.5):
            Ptf_opp_2.append(Ptf_opp[n]) 
step2()

大家好,我是 python 新手,我正在尝试为大学考试编写代码,这是我项目的一部分,它运行,但经过一些迭代,Bs=get_balance_sheet(Ptf_opp[n]) 停止工作(真的工作,但只是创建了一个无用且不正确的数据框[0,0]

代码的目的是在检查 Ptf_opp[n] 中包含的代码是否满足以下边界条件后创建一个新的数据帧。

此外,如果有人对改进我的代码有任何建议,我将不胜感激。

【问题讨论】:

  • 什么是Ptf_opp?作为一种猜测,你可能会因为太快地发出许多请求而被雅虎限制。
  • 问题很可能是Ptf_opp[n] 不包含您期望的内容,或者get_balance_sheet() 中存在错误。这个循环不是问题。
  • @DYZ Ptf_opp 只是一个包含我想要测试的所有代码的系列,老实说我认为这可能是一个雅虎问题(问题通常在或多或少之后出现250 次迭代)。你有什么建议来解决它吗?前任。获取财务报表或其他内容的另一种方式
  • @barmar, no "Ptf_opp[n]" 是正确的,我之前用其他函数检查过,这个函数也可以工作,但是在 200 次迭代后这个函数停止工作。
  • 将调试代码添加到get_balance_sheet(),看看为什么它会返回[0, 0]

标签: python pandas loops finance quantitative-finance


【解决方案1】:

不要这样做:from yahoo_fin.stock_info import *

做:from yahoo_fin import stock_info

stock_info.get_balance_sheet 然后识别它来自哪里的函数

我猜你在使用 API 时遇到了速率限制,前提是该函数实际上正在与 yahoo 联系

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 2022-01-02
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    相关资源
    最近更新 更多