import pandas as pd
import pandas_datareader.data as pdata
import matplotlib.pyplot as plt
# myDF = pd.read_csv('C:/Workarea/OneDrive/PyProjects/Learning/stocks_sentdex/GOOG-LON_TSCO.csv')
# myDF = myDF.set_index('Date')
myDF = pdata.get_data_google('LON:TSCO', start='2009-01-02', end='2009-12-31')
fig, ax = plt.subplots()
ax.fill_between(myDF.index, 0, myDF['Close'], alpha=0.3, label='LON:TSCO')
ax.set_xlabel('Date')
ax.set_ylabel('Price')
ax.set_title('Check it out')
ax.legend()
fig.subplots_adjust(left=0.09,bottom=0.16, right=0.94,top=0.90, wspace=0.2, hspace=0)
fig.autofmt_xdate()
plt.show()
错误信息
TypeError: 输入类型不支持 ufunc 'isfinite',并且根据转换规则 ''safe' 无法安全地将输入强制转换为任何支持的类型
如果myDF.index 或myDF['Close'] 是对象数组,则可能发生。举个简单的例子,
In [110]: plt.fill_between(np.array([1,2], dtype='O'), 0, np.array([1,2], dtype='O'))
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
Date 可能只是字符串而不是类似日期时间的对象。要解决此问题,请使用 pd.to_datetime(myDF['Date']) 将日期字符串转换为类似日期时间的对象。
myDF = pd.read_csv('C:/Workarea/OneDrive/PyProjects/Learning/stocks_sentdex/GOOG-LON_TSCO.csv')
myDF['Date'] = pd.to_datetime(myDF['Date'])
myDF = myDF.set_index('Date')