Есть ли способ проверить код состояния HTTP в приведенном ниже коде, поскольку я не использовал библиотеки request
или urllib
, которые позволят это сделать.Проверка состояния HTTP (Python)
from pandas.io.excel import read_excel
url = 'http://www.bankofengland.co.uk/statistics/Documents/yieldcurve/uknom05_mdaily.xls'
#check the sheet number, spot: 9/9, short end 7/9
spot_curve = read_excel(url, sheetname=8) #Creates the dataframes
short_end_spot_curve = read_excel(url, sheetname=6)
# do some cleaning, keep NaN for now, as forward fill NaN is not recommended for yield curve
spot_curve.columns = spot_curve.loc['years:']
valid_index = spot_curve.index[4:]
spot_curve = spot_curve.loc[valid_index]
# remove all maturities within 5 years as those are duplicated in short-end file
col_mask = spot_curve.columns.values > 5
spot_curve = spot_curve.iloc[:, col_mask]
#Providing correct names
short_end_spot_curve.columns = short_end_spot_curve.loc['years:']
valid_index = short_end_spot_curve.index[4:]
short_end_spot_curve = short_end_spot_curve.loc[valid_index]
# merge these two, time index are identical
# ==============================================
combined_data = pd.concat([short_end_spot_curve, spot_curve], axis=1, join='outer')
# sort the maturity from short end to long end
combined_data.sort_index(axis=1, inplace=True)
def filter_func(group):
return group.isnull().sum(axis=1) <= 50
combined_data = combined_data.groupby(level=0).filter(filter_func)
Что такое 'read_excel'? – LittleQ
@LittleQ Отредактирован код. – Jojo
Pandas использует urllib для получения файла excel и, похоже, не сохраняет HTTP-ответ, насколько я могу судить. Не можете ли вы просто использовать запросы для загрузки файла excel самостоятельно, а затем передать его в своей первой строке? Соответствующие строки: https://github.com/pydata/pandas/blob/2fea54af7699bfeda267d598010a4004b079cd49/pandas/io/excel.py#L185 – jonnybazookatone