我在Pandas数据框中有OHLC(打开,高,低,关闭)数据。我想从中获取三行断行数据。我已经为此编写了一个函数。但是,从具有近1400行的OHLC数据帧中创建Line Break数据帧大约需要2.5秒。我可以理解,由于我要对每一行进行迭代以进行计算,因此花费了时间。我需要帮助来转换函数,而无需迭代每一行或通过Pandas实现。
我写了下面提到的函数。
def get_lbdata(data, lines = 3):
data = data.reset_index().drop(['index'], axis=1)
lbdata = pd.DataFrame(columns = ['open', 'high', 'low', 'close', 'timestamp', 'date', 'month', 'year', 'weekday', 'hour', 'minute', 'second'])
lbdata.at[0, "open"] = data.open.iloc[0]
lbdata.at[0, "close"] = data.close.iloc[0]
lbdata.at[0, "timestamp"] = data.timestamp.iloc[0]
lbdata.at[0, "date"] = data.date.iloc[0]
lbdata.at[0, "month"] = data.month.iloc[0]
lbdata.at[0, "year"] = data.year.iloc[0]
lbdata.at[0, "weekday"] = data.weekday.iloc[0]
lbdata.at[0, "hour"] = data.hour.iloc[0]
lbdata.at[0, "minute"] = data.minute.iloc[0]
lbdata.at[0, "second"] = data.second.iloc[0]
if lbdata.close.iloc[-1] > lbdata.open.iloc[-1]:
lbdata.at[0, "high"] = data.close.iloc[0]
lbdata.at[0, "low"] = data.open.iloc[0]
uptrend = 1
else:
lbdata.at[0, "high"] = data.open.iloc[0]
lbdata.at[0, "low"] = data.close.iloc[0]
uptrend = 0
for ri,row in data.iterrows():
if ri == 0:
continue
i = len(lbdata)
vals = []
for x in range(-(min(lines, i)), 0):
vals.insert(len(vals), lbdata.open.iloc[x])
vals.insert(len(vals), lbdata.close.iloc[x])
minbreak = min(vals)
maxbreak = max(vals)
if row.close > maxbreak:
if uptrend:
lbdata.at[i, "open"] = lbdata.close.iloc[-1]
else:
lbdata.at[i, "open"] = lbdata.open.iloc[-1]
uptrend = 1
lbdata.at[i, "high"] = row.close
lbdata.at[i, "low"] = lbdata.open.iloc[i]
lbdata.at[i, "close"] = row.close
lbdata.at[i, "timestamp"] = row.timestamp
lbdata.at[i, "date"] = row.date
lbdata.at[i, "month"] = row.month
lbdata.at[i, "year"] = row.year
lbdata.at[i, "weekday"] = row.weekday
lbdata.at[i, "hour"] = row.hour
lbdata.at[i, "minute"] = row.minute
lbdata.at[i, "second"] = row.second
elif row.close < minbreak:
if not uptrend:
lbdata.at[i, "open"] = lbdata.close.iloc[-1]
else:
lbdata.at[i, "open"] = lbdata.open.iloc[-1]
uptrend = 0
lbdata.at[i, "high"] = lbdata.open.iloc[i]
lbdata.at[i, "low"] = row.close
lbdata.at[i, "close"] = row.close
lbdata.at[i, "timestamp"] = row.timestamp
lbdata.at[i, "date"] = row.date
lbdata.at[i, "month"] = row.month
lbdata.at[i, "year"] = row.year
lbdata.at[i, "weekday"] = row.weekday
lbdata.at[i, "hour"] = row.hour
lbdata.at[i, "minute"] = row.minute
lbdata.at[i, "second"] = row.second
return lbdata
我需要熊猫对应的代码来大大减少计算时间。