Pandas数据框架-远程数据错误-Python[英] Pandas Dataframe - RemoteDataError - Python

本文是小编为大家收集整理的关于Pandas数据框架-远程数据错误-Python的处理方法,想解了Pandas数据框架-远程数据错误-Python的问题怎么解决?Pandas数据框架-远程数据错误-Python问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

我正在尝试从Yahoo Finance获取数据.

这是我遇到的错误:

File "banana.py", line 35, in <module>
    data = web.DataReader(ticker, "yahoo", datetime(2011,1,1), datetime(2015,12,31))
  File "C:\Users\ll\Anaconda2\lib\site-packages\pandas_datareader\data.py", line 94, in DataReader
    session=session).read()
  File "C:\Users\ll\Anaconda2\lib\site-packages\pandas_datareader\yahoo\daily.py", line 77, in read
    df = super(YahooDailyReader, self).read()
  File "C:\Users\ll\Anaconda2\lib\site-packages\pandas_datareader\base.py", line 173, in read
    df = self._read_one_data(self.url, params=self._get_params(self.symbols))
  File "C:\Users\ll\Anaconda2\lib\site-packages\pandas_datareader\base.py", line 80, in _read_one_data
    out = self._read_url_as_StringIO(url, params=params)
  File "C:\Users\ll\Anaconda2\lib\site-packages\pandas_datareader\base.py", line 91, in _read_url_as_StringIO
    response = self._get_response(url, params=params)
  File "C:\Users\ll\Anaconda2\lib\site-packages\pandas_datareader\base.py", line 117, in _get_response
    raise RemoteDataError('Unable to read URL: {0}'.format(url))
pandas_datareader._utils.RemoteDataError: Unable to read URL: http://ichart.finance.yahoo.com/table.csv

当我从.csv文件而不是股票列表中阅读时,出现了错误:

这有效:

for ticker in ['MSFT']:

这不是:

input_file = open("testlist.csv", 'r')
for ticker in input_file: 

我什至没有例外(请参阅下文),但仍然无法正常工作:

    except RemoteDataError:
        print("No information for ticker '%s'" % t)
        continue

    except IndexError:
        print("Something went wacko for ticker '%s', trying again..." % t)
        continue

    except Exception, e:
        continue

    except:
        print "Can't find ", ticker

我的代码:

from datetime import datetime
from pandas_datareader import data, wb
import pandas_datareader.data as web
import pandas as pd
from pandas_datareader._utils import RemoteDataError
import csv
import sys 
import os

class MonthlyChange(object):
    months = { 0:'JAN', 1:'FEB', 2:'MAR', 3:'APR', 4:'MAY',5:'JUN', 6:'JUL', 7:'AUG', 8:'SEP', 9:'OCT',10:'NOV', 11:'DEC' }

def __init__(self,month):
    self.month = MonthlyChange.months[month-1]
    self.sum_of_pos_changes=0
    self.sum_of_neg_changes=0
    self.total_neg=0
    self.total_pos=0
def add_change(self,change):
    if change < 0:
        self.sum_of_neg_changes+=change
        self.total_neg+=1
    elif change > 0:
        self.sum_of_pos_changes+=change
        self.total_pos+=1
def get_data(self):
    if self.total_pos == 0:
        return (self.month,0.0,0,self.sum_of_neg_changes/self.total_neg,self.total_neg)
    elif self.total_neg == 0:
        return (self.month,self.sum_of_pos_changes/self.total_pos,self.total_pos,0.0,0)
    else:
        return (self.month,self.sum_of_pos_changes/self.total_pos,self.total_pos,self.sum_of_neg_changes/self.total_neg,self.total_neg)

input_file = open("Companylistnysenasdaq.csv", 'r')

for ticker in input_file:  #for ticker in input_file:


print(ticker)
data = web.DataReader(ticker, "yahoo", datetime(2011,1,1), datetime(2015,12,31))
data['ymd'] = data.index
year_month = data.index.to_period('M')
data['year_month'] = year_month
first_day_of_months = data.groupby(["year_month"])["ymd"].min()
first_day_of_months = first_day_of_months.to_frame().reset_index(level=0)
last_day_of_months = data.groupby(["year_month"])["ymd"].max()
last_day_of_months = last_day_of_months.to_frame().reset_index(level=0)
fday_open = data.merge(first_day_of_months,on=['ymd'])
fday_open = fday_open[['year_month_x','Open']]
lday_open = data.merge(last_day_of_months,on=['ymd'])
lday_open = lday_open[['year_month_x','Open']]

fday_lday = fday_open.merge(lday_open,on=['year_month_x'])
monthly_changes = {i:MonthlyChange(i) for i in range(1,13)}
for index,ym, openf,openl in fday_lday.itertuples():
    month = ym.strftime('%m')
    month = int(month)
    diff = (openf-openl)/openf
    monthly_changes[month].add_change(diff)
changes_df = pd.DataFrame([monthly_changes[i].get_data() for i in monthly_changes],columns=["Month","Avg Inc.","Inc","Avg.Dec","Dec"])




t = ticker.strip()                                       
j = 0
while j < 13:

    try:
        if len(changes_df.loc[changes_df.Inc > 2,'Month']) != 0:
            print ticker
            print ("Increase Months: ")
            print (changes_df.loc[changes_df.Inc > 2,'Month'])

        if len(changes_df.loc[changes_df.Dec > 2,'Month']) != 0:
            print ticker
            print ("Decrease Months: ")
            print (changes_df.loc[changes_df.Dec > 2,'Month'])

        j += 13


    except RemoteDataError:
        print("No information for ticker '%s'" % t)
        j += 13
        continue

    except IndexError:
        print("Something went googoo for ticker '%s', trying again..." % t)
        j += 1
        time.sleep(30)
        continue

    except Exception, e:
        j+=13
        time.sleep(30)
        continue

    except:
        print "Can't find ", ticker


input_file.close()

推荐答案

pandas_datareader当Yahoo不为通过其API提供的有关ticker数据时,会抛出此错误.

读取您的.csv文件时,您包括newline字符,因此pandas_datareader不识别attricker.

data = web.DataReader(ticker.strip('\n'), "yahoo", datetime(2011, 1, 1), datetime(2015, 12, 31))

当我创建一个在第一列中列出ticker的文件时工作.

可能更容易做:

tickers = pd.read_csv('Companylistnysenasdaq.csv')
for ticker in tickers.iloc[:, 0].tolist():

假设您的文件是第一列中带有脚注的简单列表.可能需要header=None在read_csv中,具体取决于您的文件格式.

要处理错误,您可以:

from pandas_datareader._utils import RemoteDataError

try:
    stockData = DataReader(ticker, 'yahoo', datetime(2015, 1, 1), datetime.today())
except RemoteDataError:
    # handle error

其他推荐答案

尝试以下方法: 您打开文件,但没有在其中打开.read().

 input_file = open("testlist.csv", 'r').read()

请运行以下:

input_file = open("Companylistnysenasdaq.csv", 'r').read()
for x in input_file: print x

input_file = open("Companylistnysenasdaq.csv", 'r')
for x in input_file: print x

本文地址:https://www.itbaoku.cn/post/1728186.html