首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用mplfinance绘制蜡烛图

无法使用mplfinance绘制蜡烛图
EN

Stack Overflow用户
提问于 2020-05-18 16:42:57
回答 1查看 194关注 0票数 2

我正在尝试导入以下文件并绘制蜡烛图

下面的内容存储在1.txt:

代码语言:javascript
复制
Date, Open, High, Low, Close, Volume
1589792400000, 9591.57, 9541.34, 9616.43, 9579.11, 4416.357582
1589796000000, 9579.1, 9566.96, 9666.66, 9628.39, 3873.626636
1589799600000, 9629.24, 9625.0, 9714.95, 9634.89, 3042.221677
1589803200000, 9634.89, 9565.1, 9670.0, 9665.0, 3245.471332
1589806800000, 9665.0, 9638.64, 9694.04, 9661.96, 2886.476716
1589810400000, 9661.97, 9631.82, 9735.0, 9708.56, 3105.444884
1589814000000, 9708.5, 9639.74, 9723.0, 9670.96, 2663.862566
1589817600000, 9670.97, 9608.2, 9710.0, 9648.3, 2175.585011

这是我的代码:

代码语言:javascript
复制
df=pd.read_csv(1.txt,index_col=0,parse_dates=True)
df.index=(pd.to_datetime(df.index,unit='ms'))
mplfinance.plot(df,type='candle')

--这是数据格式的样子,即df

代码语言:javascript
复制
                       Open     High      Low    Close       Volume
Date                                                                
2020-05-18 09:00:00  9591.57  9541.34  9616.43  9579.11  4416.357582
2020-05-18 10:00:00  9579.10  9566.96  9666.66  9628.39  3873.626636
2020-05-18 11:00:00  9629.24  9625.00  9714.95  9634.89  3042.221677
2020-05-18 12:00:00  9634.89  9565.10  9670.00  9665.00  3245.471332
2020-05-18 13:00:00  9665.00  9638.64  9694.04  9661.96  2886.476716
2020-05-18 14:00:00  9661.97  9631.82  9735.00  9708.56  3105.444884
2020-05-18 15:00:00  9708.50  9639.74  9723.00  9670.96  2663.862566
2020-05-18 16:00:00  9670.97  9608.20  9710.00  9648.30  2175.585011

我得到了以下错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "C:\Users\bilal\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 2646, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Open'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#38>", line 1, in <module>
    mplfinance.plot(df,type='candle')
  File "C:\Users\bilal\AppData\Local\Programs\Python\Python37\lib\site-packages\mplfinance\plotting.py", line 46, in decorator
    return func(*args, **kwargs)
  File "C:\Users\bilal\AppData\Local\Programs\Python\Python37\lib\site-packages\mplfinance\plotting.py", line 205, in plot
    dates,opens,highs,lows,closes,volumes = _check_and_prepare_data(data, config)
  File "C:\Users\bilal\AppData\Local\Programs\Python\Python37\lib\site-packages\mplfinance\_arg_validators.py", line 33, in _check_and_prepare_data
    opens   = data[o].values
  File "C:\Users\bilal\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\frame.py", line 2800, in __getitem__
    indexer = self.columns.get_loc(key)
  File "C:\Users\bilal\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 2648, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Open'

感谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-18 18:53:14

只要稍作改动,就能解决这一问题:

代码语言:javascript
复制
df=pd.read_csv('1.txt',index_col=0,parse_dates=True,skipinitialspace=True) 
#                                                   ^^^^^^^^^^^^^^^^^^^^^

这个关键字参数之所以存在,是因为CSV文件在每个条目之后可能有或可能没有空格。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61874617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档