我计划从比特币数据中获取烛台图。这是我在加载 csv 文件后选择我想要的数据框的代码。
df['Date'] = pd.to_datetime(df['Date'])
start_date = '2016-02-27'
end_date = '2021-02-27'
mask = (df['Date'] >= start_date) & (df['Date'] <= end_date)
df = df.loc[mask]
df
然后,我输入了制作烛台图的代码,如下所示:
import matplotlib.pyplot as plt
! pip install --upgrade mplfinance
import mplfinance as mpf
import matplotlib.dates as mpl_dates
mpf.plot(df, type = 'candle', style = 'charles',
title = 'Bitcoin Price',
ylabel = 'Price (USD$)',
volume = True,
ylabel_lower = 'Shares \nTraded',
mav = (3,6,9),
savefig = 'chart-mplfinance.png')
它说“TypeError:期望 data.index 作为 DatetimeIndex”。所以我在谷歌上查找了这个解决方案并尝试了这个:
df = dict()
df['Date'] = []
df['High'] = []
df['Low'] = []
df['Open'] = []
df['Close'] = []
df['Volume'] = []
for dict in df:
df['Date'].append(datetime.datetime.fromtimestamp(t).strftime('%Y-%m-%d %H:%M:%S')
df['High'].append(dict['High'])
df['Low'].append(dict['Low'])
df['Open'].append(dict['Open'])
df['Close'].append(dict['Close'])
df['Volume'].append(dict['Vol'])
print("df:", df)
pdata = pd.DataFrame.from_dict(df)
pdata.set_index('Date', inplace=True)
mpf.plot(pdata)
这一次,它说“无效的语法”我不确定我在哪里弄错了,有什么我遗漏的吗?