Python-如何在仍然获取数据的同时避免在 pandas 中发生错误(异常)? [英] Python - How to avoid error (Exceptions) in Pandas while still getting data ?

查看:84
本文介绍了Python-如何在仍然获取数据的同时避免在 pandas 中发生错误(异常)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Pandas从yahoo获取期权数据.直到有没有选择权的股票,它会正常运行,此时程序崩溃.我试图创建例外,只是让它通过但没有运气.我如何让程序识别没有选择权的股票而只是跳过?谢谢.

I'm currently using Pandas to get options data from yahoo. It works fine until there is a stock that does not have options, at which point the program crashes. I attempted to create exceptions and just have it pass but without luck. How can I have the program identify stocks with no options and just skip? Thanks.

我得到的错误是:RemoteDataError:数据不可用

The error I get is this: RemoteDataError: Data not available

这是代码(我用没有选择权的股票来测试->'GHC'):

Here's the code (I used a stock with no options to test--> 'GHC'):

from pandas_datareader.data import Options
import pandas as pd
from pandas import DataFrame
import datetime
import csv
import time
import sys

tickers = ['GHC']

for i in tickers:
option = Options(i,'yahoo')
data = option.get_all_data()

try:
    print data.head

except AttributeError:
    pass

except RemoteDataError:
    pass

推荐答案

您可以处理从pandas_datareader._utils导入的RemoteDataError异常:

You can handle the RemoteDataError exception imported from pandas_datareader._utils:

from pandas_datareader._utils import RemoteDataError
from pandas_datareader.data import Options

tickers = ['GHC']

for i in tickers:
    try:
        option = Options(i, 'yahoo')
        data = option.get_all_data()
    except RemoteDataError:
        print("No information for ticker '%s'" % i)
        continue

这篇关于Python-如何在仍然获取数据的同时避免在 pandas 中发生错误(异常)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆