刚入小白 ,这是哪里出的问题?

import tushare as ts
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import talib as ta
import seaborn as sns

# 利用tushare.pro获取平安银行股票2014-9-1至2019-9-1的股价数据 

ts.set_token('bd277871613fdf621f6d96bbee83fa20c5f08c6959457463efb8cd8e')

pro = ts.pro_api()

df = pro.daily(ts_code='000001.SZ',start_date='20140901', end_date='20190901')[['trade_date','close']]

df.set_index('trade_date', inplace=True)

#df1.rename(columns={'close':'600519'}, inplace=True )

df.index = pd.to_datetime(df.index)

df.sort_index(ascending=True, inplace=True)

df    这是第一段 顺利执行

df['cci'] = ta.CCI(np.asarray(df['high']), np.asarray(df['low']), np.asarray(df['close']), timeperiod=20)

df    导入CCI指标就不行了 报错   high错了?哪里错了到底

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2896             try:
-> 2897                 return self._engine.get_loc(key)
   2898             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'high'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-8-126ef3dc2680> in <module>
      1 # 计算CCI,取时间周期为20
      2 
----> 3 df['cci'] = ta.CCI(np.asarray(df['high']), np.asarray(df['low']), np.asarray(df['close']), timeperiod=20)
      4 
      5 df

~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2978             if self.columns.nlevels > 1:
   2979                 return self._getitem_multilevel(key)
-> 2980             indexer = self.columns.get_loc(key)
   2981             if is_integer(indexer):
   2982                 indexer = [indexer]

~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2897                 return self._engine.get_loc(key)
   2898             except KeyError:
-> 2899                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2900         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2901         if indexer.ndim > 1 or indexer.size > 1:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'high'

是不是数据类型不对

df['high'] 的数据类型是series,你用np转化后就变成了np了,如果Tushare调取的数据是字符串可以这样转化,df['high'].astype('float')

试一下

 

我其实正在看视屏教学。核对了半天了,没觉得有问题啊,np后续使用转换计算更方便!