集約データとmongoデータの照合

1894 ワード

コード:
	import sys
	import datetime
	import pymongo
	import pprint
	
	import pandas as pd
	import jqdatasdk as jqsdk
	
	#  
	jqsdk.auth(' ', ' ')  #  
	df1 = jqsdk.get_price("002694.XSHE", start_date="2013-03-25", end_date="2019-05-01",
	                      frequency='daily', fields=['paused'])
	df1.rename(index={}, columns={"paused": "ok"})
	df1.index.name = "date"
	df1['paused'] = df1['paused'].map({0: True, 1: False})
	
	# mongo  
	MongoUri = "mongodb://127.0.0.1:27017"
	db = pymongo.MongoClient(MongoUri)
	cld = db.JQdata.calendar
	res_info = list(cld.find({"code": "SZ002694",
	                          "date_int": {"$gte": 20130325, "$lte": 20190501}},
	                         {"_id": 0, "date": 1, "ok": 1}))
	
	
	# (1)
	dates = [res.get("date") for res in res_info]
	dates = sorted(list(set(dates)))
	dates_index = pd.DatetimeIndex(dates)
	
	oks = [res.get("ok") for res in res_info]
	
	data = {
	    "date": dates_index,
	    "ok": pd.Series(oks),
	}
	
	df = pd.DataFrame(data)
	df = df.set_index("date")
	
	# merge  
	ddf = pd.merge(df, df1, how="right", on="date")
	
	print(ddf[ddf['ok'] != ddf['paused']])
	
	
	# (2)
	# dates = [res.get("date") for res in res_info]
	# dates = sorted(list(set(dates)))
	# oks = [res.get("ok") for res in res_info]
	# data = {
	#     "date": dates,
	#     "ok": pd.Series(oks),
	# }
	# df = pd.DataFrame(data)
	# df["date"] = pd.to_datetime(df["date"])
	# df = df.set_index("date")
	#
	# # merge  
	# ddf = pd.merge(df, df1, how="right", on="date")
	# print(ddf[ddf['ok'] != ddf['paused']])