问题描述
当我使用DataReader从FRED抓取数据时,我的DataFrame中有一些NaN值,但是,在FRED官方网站上,它们的数据和图形似乎是完整的而不会丢失。所以我想知道是什么原因导致NaN值出现在我的DataFrame中?以及如何解决?
import pandas as pd
import datetime as dt
import pandas_datareader as web
import matplotlib.pyplot as plt
from matplotlib import style
import matplotlib.ticker as ticker
style.use('dark_background')
prev3=620
end3=dt.datetime.today().date()
st3=end-pd.to_timedelta(prev3,unit='d')
tgt=['DFII5','DGS5','DFII10','DFII20','DFII30','GOLdamGBD228NLBM','USD3MTD156N','USD12MD156N','FEDFUNDS']
fig,ax = plt.subplots(figsize=(20,10))
ax1=ax.twinx()
tgt=['USD3MTD156N','DGS5']
#draw lines
interest=pd.DataFrame()
for i in tgt:
interest[i]=web.DataReader(i,'fred',st3,end3)[i]
interest['GOLdamGBD228NLBM']=1/interest['GOLdamGBD228NLBM']*1000
for i in tgt:
if i!='GOLdamGBD228NLBM':
ax.plot(interest[i],label=i+'(L)',linewidth=0.8)
else:
ax1.plot(interest[i],label=i+'(R)',color='r',linewidth=1,linestyle=':')
#draw stackplots
dfii=['DFII5','DFII30']
df=pd.DataFrame()
for i in dfii:
df[i]=web.DataReader(i,end3)[i]
for i in df:
ax.stackplot(df.index,df[i],labels=df.columns,alpha=0.3)
ax.legend(loc=1)
ax1.legend(loc=2)
plt.show()
解决方法
您能否检查2020年9月月份的DGS5
代码(在上面的列表中)?
- 2020年9月7日(星期一)是美国假期,因此FRED在此行情历/日期中均输入NaN。
- 要删除图中的“断裂”,可以在绘制之前删除NaN值。