【发布时间】:2015-01-20 11:21:58
【问题描述】:
如何有效地找到 SecurityID 列中每个唯一值的最后一个时间戳(来自 Datetime 列)? SecurityID 列中大约有 1000 个唯一值。
目前我在整个表中查询 SecurityID 中的每个唯一值,然后查找最后一个时间戳。正如您可能想象的那样,它非常缓慢。该表超过 40GB,并且还在不断增长。
我会这样做:
os.chdir('E:\HDFStores')
store = pd.HDFStore('mysuperawesomehdfstore.h5')
assets = skBase.bbg_helper_assets('minutely')
df_timestamp = pd.Dataframe()
tags = ['T', 'B', 'A']
for asset in assets:
for tag in tags:
print asset, " ", tag
timestamp = (store.select('table', where = "SecurityID = ['" + asset + "'] & Tag = ['" + tag + "'] & columns = ['Datetime']")).tail(1)
if len(timestamp_.index) == 0:
print "value DNE"
else:
dt = (str(timestamp_iloc[0][0])).split(' ', 1)[0]
tm = (str(timestamp_iloc[0][0])).split(' ', 1)[1]
我曾考虑在我的 4 核机器上运行单独的 python 进程。但我宁愿有一种更清洁的做事方式,也不愿诉诸黑客。
任何想法都将不胜感激。
【问题讨论】: