我有一个具有以下结构的 DataFrame:
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 3333 entries, 2000-01-03 00:00:00+00:00 to 2012-11-21 00:00:00+00:00
Data columns:
open 3333 non-null values
high 3333 non-null values
low 3333 non-null values
close 3333 non-null values
volume 3333 non-null values
amount 3333 non-null values
pct_change 3332 non-null values
dtypes: float64(7)
该pct_change
列包含百分比变化数据。
从上面的 DataFrame 中给定一个过滤的 DatetimeIndex:
<class 'pandas.tseries.index.DatetimeIndex'>
[2000-03-01 00:00:00, ..., 2012-11-01 00:00:00]
Length: 195, Freq: None, Timezone: UTC
我想过滤开始每个日期条目并返回pct_change
列低于 0.015 的第一行。
我想出了这个解决方案,但它很慢:
stops = []
#dates = DatetimeIndex
for d in dates:
#check if pct_change is below -0.015 starting from date of signal. return date of first match
match = df[df["pct_change"] < -0.015].ix[d:][:1].index
stops.append([df.ix[d]["close"], df.ix[match]["close"].values[0]])
关于如何改进这一点的任何建议?