【发布时间】:2020-01-12 06:12:22
【问题描述】:
我需要在条件发生前 60 分钟对 pandas 时间序列进行切片,例如在“信号”列中的数字之前 60 秒 == 1
现在,我在整个数据帧上使用 .tail(60) 直到所需的索引,但这非常低效
def create_sequences(signal, label, data):
"""Function to return seqs of 60 secs prior to condition"""
sequences = []
for i in signal:
sequence = data.loc[:i].tail(60)
if len(sequence) == 60:
sequences.append((np.array(sequence.drop('Signal',
axis=1)).transpose(), label))
return sequences
# To generate some data for reproduction
periods = 7 * 24 * 60
tidx = pd.date_range('2019-09-01', periods=periods, freq='T')
ts = pd.DataFrame(data=data, index=tidx)
ts['Signal'] = ts[0].apply(lambda x: 1 if x > 0 else 0)
ones = ts[ts.Signal == 1].index.values
x = create_sequences(ones, 1, ts)
【问题讨论】: