我正在第二次收到股票报价,并将其存储在数据框中。我需要对其重新采样以获得一分钟的ohlc值。这是我的代码:
def on_ticks(ws, ticks):
global time_second, df_cols, tick_cols, data_frame
for company_data in ticks:
ltp = company_data['last_price']
timestamp = company_data['timestamp']
lowest_sell = company_data['depth']['sell'][0]['price']
highest_buy = company_data['depth']['buy'][0]['price']
data = [timestamp, ltp, lowest_sell, highest_buy]
tick_df = pd.DataFrame([data], columns=tick_cols)
#print(tick_df)
data_frame = pd.concat([data_frame, tick_df], axis=0, sort=True, ignore_index='true')
#print("time_second is ", time_second)
if time_second > timestamp.second:
#print("now we will print data_frame")
#print(data_frame)
print("Resampling dataframe & Calculating the EMAs............")
resamp_df = data_frame.resample('1T', on='Timestamp').ohlc()
运行此代码时,它会触发以下错误 DataError:没有要聚合的数字类型 :
---------------------------------------------------------------------------
DataError Traceback (most recent call last)
<ipython-input-8-166d9105fb91> in <module>
----> 1 resamp = df.resample('1T', on='Timestamp').ohlc()
~\Anaconda3\lib\site-packages\pandas\core\resample.py in g(self, _method, *args, **kwargs)
904 def g(self, _method=method, *args, **kwargs):
905 nv.validate_resampler_func(_method, args, kwargs)
--> 906 return self._downsample(_method)
907
908 g.__doc__ = getattr(GroupBy, method).__doc__
~\Anaconda3\lib\site-packages\pandas\core\resample.py in _downsample(self, how, **kwargs)
1068 # we are downsampling
1069 # we want to call the actual grouper method here
-> 1070 result = obj.groupby(self.grouper, axis=self.axis).aggregate(how, **kwargs)
1071
1072 result = self._apply_loffset(result)
~\Anaconda3\lib\site-packages\pandas\core\groupby\generic.py in aggregate(self, arg, *args, **kwargs)
1453 @Appender(_shared_docs["aggregate"])
1454 def aggregate(self, arg=None, *args, **kwargs):
-> 1455 return super().aggregate(arg, *args, **kwargs)
1456
1457 agg = aggregate
~\Anaconda3\lib\site-packages\pandas\core\groupby\generic.py in aggregate(self, func, *args, **kwargs)
227 func = _maybe_mangle_lambdas(func)
228
--> 229 result, how = self._aggregate(func, _level=_level, *args, **kwargs)
230 if how is None:
231 return result
~\Anaconda3\lib\site-packages\pandas\core\base.py in _aggregate(self, arg, *args, **kwargs)
354
355 if isinstance(arg, str):
--> 356 return self._try_aggregate_string_function(arg, *args, **kwargs), None
357
358 if isinstance(arg, dict):
~\Anaconda3\lib\site-packages\pandas\core\base.py in _try_aggregate_string_function(self, arg, *args, **kwargs)
303 if f is not None:
304 if callable(f):
--> 305 return f(*args, **kwargs)
306
307 # people may try to aggregate on a non-callable attribute
~\Anaconda3\lib\site-packages\pandas\core\groupby\groupby.py in ohlc(self)
1438 """
1439
-> 1440 return self._apply_to_column_groupbys(lambda x: x._cython_agg_general("ohlc"))
1441
1442 @Appender(DataFrame.describe.__doc__)
~\Anaconda3\lib\site-packages\pandas\core\groupby\generic.py in _apply_to_column_groupbys(self, func)
1579 (func(col_groupby) for _, col_groupby in self._iterate_column_groupbys()),
1580 keys=self._selected_obj.columns,
-> 1581 axis=1,
1582 )
1583
~\Anaconda3\lib\site-packages\pandas\core\reshape\concat.py in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity, sort, copy)
253 verify_integrity=verify_integrity,
254 copy=copy,
--> 255 sort=sort,
256 )
257
~\Anaconda3\lib\site-packages\pandas\core\reshape\concat.py in __init__(self, objs, axis, join, join_axes, keys, levels, names, ignore_index, verify_integrity, copy, sort)
299 objs = [objs[k] for k in keys]
300 else:
--> 301 objs = list(objs)
302
303 if len(objs) == 0:
~\Anaconda3\lib\site-packages\pandas\core\groupby\generic.py in <genexpr>(.0)
1577
1578 return concat(
-> 1579 (func(col_groupby) for _, col_groupby in self._iterate_column_groupbys()),
1580 keys=self._selected_obj.columns,
1581 axis=1,
~\Anaconda3\lib\site-packages\pandas\core\groupby\groupby.py in <lambda>(x)
1438 """
1439
-> 1440 return self._apply_to_column_groupbys(lambda x: x._cython_agg_general("ohlc"))
1441
1442 @Appender(DataFrame.describe.__doc__)
~\Anaconda3\lib\site-packages\pandas\core\groupby\groupby.py in _cython_agg_general(self, how, alt, numeric_only, min_count)
886
887 if len(output) == 0:
--> 888 raise DataError("No numeric types to aggregate")
889
890 return self._wrap_aggregated_output(output, names)
DataError: No numeric types to aggregate
有人可以帮我找出我要去哪里的地方吗?
答案 0 :(得分:1)
问题是没有数字列,Timestamp
中只有日期时间。
我认为您可以创建DatetimeIndex
,然后将所有列转换为float
,也有必要在on
中删除参数resample
:
resamp_df = data_frame.set_index('Timestamp').astype(float).resample('1T').ohlc()
另一个想法(如果使用标量)是将它们转换为浮点数:
for company_data in ticks:
ltp = float(company_data['last_price'])
timestamp = company_data['timestamp']
lowest_sell = float(company_data['depth']['sell'][0]['price'])
highest_buy = float(company_data['depth']['buy'][0]['price'])