Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何给时间序列数据添加滞后?_Python_Pandas - Fatal编程技术网

Python 如何给时间序列数据添加滞后?

Python 如何给时间序列数据添加滞后?,python,pandas,Python,Pandas,我有一个数据帧df,带有每个产品id的tv spot数据: | start_date | end_date | id | f1 | f2 0 | 2020-01-01 | 2020-01-02 | 1 | 111 | 222 1 | 2020-01-05 | 2020-01-07 | 1 | 111 | 222 2 | 2020-01-01 | 2020-01-02 | 3 | 333 | 444 3 | 2020-01-05 | 2020-01-07 | 3 | 555 |

我有一个数据帧df,带有每个产品id的tv spot数据:

  | start_date | end_date   | id | f1  | f2
0 | 2020-01-01 | 2020-01-02 | 1  | 111 | 222
1 | 2020-01-05 | 2020-01-07 | 1  | 111 | 222
2 | 2020-01-01 | 2020-01-02 | 3  | 333 | 444
3 | 2020-01-05 | 2020-01-07 | 3  | 555 | 666
现在,我想添加0到2天的延迟,作为预测模型中的功能使用

然后,应该将日期范围开始日期+结束日期分解为一个日期列,这样我就有了一个日期列而不是日期范围

但我不知道如何才能做到这一点

最终结果应该如下所示:

  | date       | id | f1_lag_0 | f2_lag_0 | f1_lag_1 | f2_lag_1 | f1_lag_2 | f2_lag_2
0 | 2020-01-01 | 1  | 111      | 222      | 111      | 222      | 111      | 222
1 | 2020-01-02 | 1  | 111      | 222      | 111      | 222      | 111      | 222
2 | 2020-01-03 | 1  | NaN      | NaN      | 111      | 222      | 111      | 222
3 | 2020-01-04 | 1  | NaN      | NaN      | NaN      | NaN      | 111      | 222
0 | 2020-01-05 | 1  | 111      | 222      | 111      | 222      | 111      | 222
1 | 2020-01-06 | 1  | 111      | 222      | 111      | 222      | 111      | 222
2 | 2020-01-07 | 1  | 111      | 222      | 111      | 222      | 111      | 222
3 | 2020-01-08 | 1  | NaN      | NaN      | 111      | 222      | 111      | 222
4 | 2020-01-09 | 1  | NaN      | NaN      | NaN      | NaN      | 111      | 222
0 | 2020-01-01 | 3  | 333      | 444      | 333      | 444      | 333      | 444
1 | 2020-01-02 | 3  | 333      | 444      | 333      | 444      | 333      | 444
2 | 2020-01-03 | 3  | NaN      | NaN      | 333      | 444      | 333      | 444
3 | 2020-01-04 | 3  | NaN      | NaN      | NaN      | NaN      | 333      | 444
0 | 2020-01-05 | 3  | 555      | 666      | 555      | 666      | 555      | 666
1 | 2020-01-06 | 3  | 555      | 666      | 555      | 666      | 555      | 666
2 | 2020-01-07 | 3  | 555      | 666      | 555      | 666      | 555      | 666
3 | 2020-01-08 | 3  | NaN      | NaN      | 555      | 666      | 555      | 666
4 | 2020-01-09 | 3  | NaN      | NaN      | NaN      | NaN      | 555      | 666
创建虚拟df的代码:

df = pd.DataFrame(
    {
        "start_date": [
            "2020-01-01",
            "2020-01-05",
            "2020-01-01",
            "2020-01-06",
        ],
        "end_date": [
            "2020-01-02",
            "2020-01-07",
            "2020-01-02",
            "2020-01-07"
        ],
        "id": ["1", "1", "3", "3"],
        "feature1": ["111", "111", "333", "555"],
        "feature2": ["222", "222", "444", "666"],
    }
)
使用:

每组最后使用班次:

for j, i in enumerate(range(2, -1, -1)):
    df[[f'f1_lag_{j}', f'f2_lag_{j}']] = df.groupby(level=0)[cols].shift(-i)
    
df = (df.drop(cols, axis=1)
        .drop('end_date', axis=1)
        .rename(columns={'start_date':'date'})
        .reset_index(drop=True))

所以需要一些更快的东西,比如?不。我想为每个电视广告产品增加0到7天的延迟。
for j, i in enumerate(range(2, -1, -1)):
    df[[f'f1_lag_{j}', f'f2_lag_{j}']] = df.groupby(level=0)[cols].shift(-i)
    
df = (df.drop(cols, axis=1)
        .drop('end_date', axis=1)
        .rename(columns={'start_date':'date'})
        .reset_index(drop=True))
print (df)
         date id f1_lag_0 f2_lag_0 f1_lag_1 f2_lag_1 f1_lag_2 f2_lag_2
0  2020-01-01  a      111      222      111      222      111      222
1  2020-01-02  a      111      222      111      222      111      222
2  2020-01-03  a      NaN      NaN      111      222      111      222
3  2020-01-04  a      NaN      NaN      NaN      NaN      111      222
4  2020-01-05  a      111      222      111      222      111      222
5  2020-01-06  a      111      222      111      222      111      222
6  2020-01-07  a      111      222      111      222      111      222
7  2020-01-08  a      NaN      NaN      111      222      111      222
8  2020-01-09  a      NaN      NaN      NaN      NaN      111      222
9  2020-01-01  b      333      444      333      444      333      444
10 2020-01-02  b      333      444      333      444      333      444
11 2020-01-03  b      NaN      NaN      333      444      333      444
12 2020-01-04  b      NaN      NaN      NaN      NaN      333      444
13 2020-01-06  b      555      666      555      666      555      666
14 2020-01-07  b      555      666      555      666      555      666
15 2020-01-08  b      NaN      NaN      555      666      555      666
16 2020-01-09  b      NaN      NaN      NaN      NaN      555      666