class FutBarData(object):
"""K线数据"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
self.vtSymbol = EMPTY_STRING # vt系统代码
self.marketCode = EMPTY_STRING # 市场代码
self.symbol = EMPTY_STRING # 合约代码
self.datetime = EMPTY_STRING # 时间
self.price = EMPTY_FLOAT # 最新
self.openInterest = EMPTY_INT # 持仓量
self.newInterest = EMPTY_INT # 增仓
self.amt = EMPTY_STRING # 成交额
self.volume = EMPTY_INT # 成交量
self.offsetOpen = EMPTY_INT # 开仓
self.offsetClose = EMPTY_INT # 平仓
self.type = EMPTY_STRING # 成交类型
self.direction = EMPTY_STRING # 方向
self.bidPrice1 = EMPTY_FLOAT # 买一价
self.askPrice1 = EMPTY_FLOAT # 卖一价
self.bidVolume1 = EMPTY_INT # 买一量
self.askVolume1 = EMPTY_INT # 卖一量
value.head()
Out[75]:
vtSymbol marketCode symbol datetime price openInterest \
0 SF SF IF1005 2010-04-16 09:14:00 3450.0 187
1 SF SF IF1005 2010-04-16 09:15:00 3456.0 190
2 SF SF IF1005 2010-04-16 09:15:01 3456.0 190
3 SF SF IF1005 2010-04-16 09:15:02 3453.0 196
4 SF SF IF1005 2010-04-16 09:15:03 3455.0 198
newInterest amt volume offsetOpen offsetClose type direction \
0 187 193544992.0 187 187 0 双开 B
1 3 3108608.0 3 3 0 双开 B
2 0 1036800.0 1 1 1 空换 S
3 6 8292544.0 8 7 1 多开 B
4 2 2072400.0 2 2 0 双开 B
bidPrice1 askPrice1 bidVolume1 askVolume1
0 3443.0 3456.0 6 2
1 3456.0 3458.0 1 6
2 3451.8 3452.0 2 1
3 3451.8 3453.0 2 1
4 3452.0 3458.0 2 4
i = 0
bar = FutBarData()
bar.vtSymbol = value.loc[i, 'vtSymbol']
bar.marketCode = value.loc[i, 'marketCode']
bar.symbol = value.loc[i, 'symbol']
bar.datetime = datetime.strptime(value.loc[i, 'datetime'], '%Y-%m-%d %H:%M:%S')
bar.price = float(value.loc[i, 'price'])
bar.openInterest = value.loc[i, 'openInterest']
bar.newInterest = value.loc[i, 'newInterest']
bar.amt = value.loc[i, 'amt']
bar.volume = value.loc[i, 'volume']
bar.offsetOpen = value.loc[i, 'offsetOpen']
bar.offsetClose = value.loc[i, 'offsetClose']
bar.type = value.loc[i, 'type']
bar.direction = value.loc[i, 'direction']
bar.bidPrice1 = value.loc[i, 'bidPrice1']
bar.askPrice1 = value.loc[i, 'askPrice1']
bar.bidVolume1 = value.loc[i, 'bidVolume1']
bar.askVolume1 = value.loc[i, 'askVolume1']
flt = {'datetime': bar.datetime, 'symbol': bar.symbol}
collection.update_one(flt, {'$set':bar.__dict__}, upsert=True)
报错如下:
InvalidDocumentTraceback (most recent call last)
<ipython-input-76-2a4a8dd8bfaf> in <module>()
1 flt = {'datetime': bar.datetime, 'symbol': bar.symbol}
----> 2 collection.update_one(flt, {'$set':bar.__dict__}, upsert=True)
3
C:\Anaconda2\lib\site-packages\pymongo\collection.pyc in update_one(self, filter, update, upsert, bypass_document_validation)
839 result = self._update(sock_info, filter, update, upsert,
840 check_keys=False,
--> 841 bypass_doc_val=bypass_document_validation)
842 return UpdateResult(result, self.write_concern.acknowledged)
843
C:\Anaconda2\lib\site-packages\pymongo\collection.pyc in _update(self, sock_info, criteria, document, upsert, check_keys, multi, manipulate, write_concern, op_id, ordered, bypass_doc_val)
713 self.__database.name,
714 command,
--> 715 codec_options=self.__write_response_codec_options).copy()
716 _check_write_command_response([(0, result)])
717 # Add the updatedExisting field for compatibility.
C:\Anaconda2\lib\site-packages\pymongo\pool.pyc in command(self, dbname, spec, slave_ok, read_preference, codec_options, check, allowable_errors, check_keys, read_concern)
242 # Catch socket.error, KeyboardInterrupt, etc. and close ourselves.
243 except BaseException as error:
--> 244 self._raise_connection_failure(error)
245
246 def send_message(self, message, max_doc_size):
C:\Anaconda2\lib\site-packages\pymongo\pool.pyc in _raise_connection_failure(self, error)
370 _raise_connection_failure(self.address, error)
371 else:
--> 372 raise error
373
374 def __eq__(self, other):
InvalidDocument: Cannot encode object: 2
如果我没想错,你的数字格式都是numpy.int64或者numpy.float64的,要转一下int和float再存储应该就没问题了