df20190807['time'] #
df20190807.set_index('time', inplace=True)
df20190807.sort_index = pd.to_datetime(df20190807.index)
df20190807 = df20190807.resample('5min').mean()
df20190807.drop(df20190807.index[26:43], implace=True)
df20190807.head(50)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-5-98643de13e75> in <module> 3 df20190807 = ts.get_tick_data('000001',date='2019-08-07', src='tt') #tick_data 4 ----> 5 df20190807['time'] = "2019-08-07" +df20190807['time'] # 6 7 df20190807.set_index('time', inplace=True) TypeError: 'NoneType' object is not subscriptable
问题你可以新建帖子一个一个提的
猜你碰到的pandas中日期数据处理, 相关的知识点,你先了解下:
1. 将字符串格式列转日期
df['日期'] = pd.to_datetime(data['日期'],format='%Y/%m/%d')
2. 计算日期间隔天数
data['间隔天数'] = list(map(lambda x: x.days, pd.to_datetime('today') - data['最近生产时间']))
data['天数'] = data['间隔天数'].apply(lambda x: x.days)
3. 日期加一个数值或者减去一个数值, 数值需要是 timedelta格式
from datetime import timedelta
aDay = timedelta(days=1)
df['morning_star'] = ta.CDLMORNINGSTAR(df['open'], df['high'], df['low'], df['close'])
df
--------------------------------------------------------------------------- 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: 'open' During handling of the above exception, another exception occurred: KeyError Traceback (most recent call last) <ipython-input-6-25220bd82382> in <module> ----> 1 df['morning_star'] = ta.CDLMORNINGSTAR(df['open'], df['high'], df['low'], df['close']) 2 3 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: 'open'
这是之前的问题,我按视频走的,咋就出错了类,刚学,求解!!
df['cci'] = ta.CCI(np.asarray(df['high']), np.asarray(df['low']), np.asarray(df['close']), timeperiod=20)
df
一直说我 KeyError: 'high' 我咋没觉得有问题类?
有没有大神,都给解了呗?!
第一个问题
这一句 df20190807.sort_index = pd.to_datetime(df20190807.index)
前面是索引排序, 后面是将索引转成转成日期, 中间怎么是等号呢
df20190807 = ts.get_tick_data('000001', date='2019-08-07', src='tt')
df20190807['time'] ="2019-08-07" + df20190807['time'] #
df20190807.set_index('time', inplace=True)
df20190807.sort_index(ascending=True, inplace=True)
df20190807.index = pd.to_datetime(df20190807.index)
df20190807 = df20190807.resample('5min').mean()
df20190807.drop(df20190807.index[26:43], implace=True)
df20190807.head(50)
应该这样 还是不对啊