使用 datareader 中的股票数据时,Pandas 错误“没有要绘制的数字数据”

Posted

技术标签:

【中文标题】使用 datareader 中的股票数据时,Pandas 错误“没有要绘制的数字数据”【英文标题】:Pandas error "No numeric data to plot" when using stock data from datareader 【发布时间】:2020-02-05 12:19:07 【问题描述】:

我有一个包含收盘价的数据框:

import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt
import seaborn; seaborn.set()
from pandas_datareader import data
import pandas_datareader.data as web
from pandas.tseries.offsets import BDay

f = web.DataReader('^DJI', 'stooq')
CLOSE = f['Close']
CLOSE.plot(alpha= 0.5,style='-')
CLOSE.resample('BA').mean().plot(style=':') 
CLOSE.asfreq(freq='BA').plot(style='--') 
plt.legend(['input','resample','asfreq'],loc='upper left')

使用 resample() 我得到上一年的平均值。这行得通。 使用 asfreq() 我试图在年底获得收盘价。这行不通。 我在 asfreq() 行中收到以下错误:TypeError: no numeric data to plot

f.info() 显示 close 是一个非空的 float64 类型。

可能出了什么问题?

【问题讨论】:

【参考方案1】:

索引没有按层次排序:

f= f.sort_index(axis=0) 解决了。

【讨论】:

以上是关于使用 datareader 中的股票数据时,Pandas 错误“没有要绘制的数字数据”的主要内容,如果未能解决你的问题,请参考以下文章