python - 使用 FRED 的 pandas 数据读取器时缺少值
问题描述
当我使用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','USD12MD156N','GOLDAMGBD228NLBM','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','DFII10','DFII20','DFII30']
df=pd.DataFrame()
for i in dfii:
df[i]=web.DataReader(i,'fred',st3,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()
解决方案
您能否查看DGS5
2020 年 9 月的股票代码(从您上面的列表中)?
- 2020 年 9 月 7 日星期一是美国假日,因此 FRED 的此代码/日期为 NaN。
- 要删除图中的“中断”,您可以在绘图之前删除 NaN 值。
推荐阅读
- laravel - 在vuejs中使用if语句循环
- javascript - 在 NodeJS 中验证签名
- c# - 检测订阅状态 Unity IAP
- c# - VS/C# 调用方法时自动生成命名参数
- java - 仅使用 src/test/resources/application.properties 覆盖值以进行集成测试
- mysql - 为什么 != 对于 psql 中的这个自连接查询不正确
- android - Android WiFiManager enableNetwork 返回 false
- java - Java .equals 字符串的错误
- python - 使用“加载”时,Dill 会删除对象
- mule - Mule 4 - 使用 Anypoint studio 7 创建 WSDL Web 服务