0
有沒有辦法檢查下面的代碼中的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