按时间序列计算连续值

时间:2017-12-13 13:28:15

标签: python pandas count trend

这是我第一次在这里提问,所以我希望我能做得对!

我有一个Pandas Dataframe:

df2.data
Out[66]: 
date
2016-01-02    0.0
2016-01-03    1.0
2016-01-04    1.0
2016-01-05    1.0
2016-01-06    0.0
2016-01-07    0.0
2016-01-08    1.0
2016-01-09    2.0
2016-01-10    1.0
2016-01-11    0.0
Name: data, dtype: float64

我想要以下结果:

            data  trend  trend_type
date                               
2016-01-02   0.0      0           0
2016-01-03   1.0      0           0
2016-01-04   1.0      1           1
2016-01-05   1.0      2           1
2016-01-06   0.0      0           0
2016-01-07   0.0      1           0
2016-01-08   1.0      0           0
2016-01-09   2.0      0           0
2016-01-10   1.0      0           0
2016-01-11   0.0      0           0

我的问题与How to use pandas to find consecutive same data in time series有点相关。

到目前为止,我设法得到了趋势,但效率不够(750行数据帧约为8秒)

df['grp'] = (df.close.diff(1) == 0).astype('int')
df['trend'] = 0
start_time = time.time()
for i in range(2, len(df['grp'])):
    if df.grp.iloc[i] == 1:
        df['trend'].iloc[i] = df['trend'].iloc[i-1] + 1 

2 个答案:

答案 0 :(得分:1)

第1步
要获得trend,请执行groupby + cumcount -

df['trend'] = df.data.groupby(df.data.ne(df.data.shift()).cumsum()).cumcount()
df

            data  trend
2016-01-02   0.0      0
2016-01-03   1.0      0
2016-01-04   1.0      1
2016-01-05   1.0      2
2016-01-06   0.0      0
2016-01-07   0.0      1
2016-01-08   1.0      0
2016-01-09   2.0      0
2016-01-10   1.0      0
2016-01-11   0.0      0

第2步
(IIUC),获取trend_type,比较连续的行并分配。

df['trend_type'] = 0
m = df.data.eq(df.data.shift())
df.loc[m, 'trend_type'] = df.loc[m, 'data']

df

            data  trend  trend_type
2016-01-02   0.0      0         0.0
2016-01-03   1.0      0         0.0
2016-01-04   1.0      1         1.0
2016-01-05   1.0      2         1.0
2016-01-06   0.0      0         0.0
2016-01-07   0.0      1         0.0
2016-01-08   1.0      0         0.0
2016-01-09   2.0      0         0.0
2016-01-10   1.0      0         0.0
2016-01-11   0.0      0         0.0

答案 1 :(得分:0)

编辑,添加列“trep_type”

df.loc[0, "trend"] = 0
df.loc[0, "trend_type"] = 0

for nrow in range(df.shape[0]-1):

    if df.loc[nrow+1, 1] == df.loc[nrow, 1]:
        df.loc[nrow+1, "trend"] = df.loc[nrow, "trend"]+1
        df.loc[nrow + 1, "trend_type"] = 1
    else:
        df.loc[nrow + 1, "trend"] = 0
        df.loc[nrow + 1, "trend_type"] = 0