简体   繁体   English

在 Pandas 中添加日期和零填充时间序列?

[英]Adding dates and zero padding a time series in Pandas?

I have a time series dataframe that looks like this:我有一个如下所示的时间序列数据框:

      Date     Var_1  Var_2  Var_3
------------------------------------
0   01/01/14    1      1       2  
1   01/08/14    2      1       3
2   01/15/14    2      1       3
3   01/22/14    1      0       3
4   01/29/14    3      0       2 

I want to extend the dates by a few weeks prior to the start date and zero pad the other variables, so that my dataframe will look like:我想在开始日期前几周将日期延长,并将其他变量补零,以便我的数据框看起来像:

      Date     Var_1  Var_2  Var_3
------------------------------------
0   12/11/13    0      0       0  
1   12/18/13    0      0       0
2   12/25/13    0      0       0
3   01/01/13    1      1       2  
4   01/08/14    2      1       3
5   01/15/14    2      1       3
6   01/22/14    1      0       3
7   01/29/14    3      0       2 

Are there any built in functions in Pandas to do this, or do I to create a separate dataframe with the additional date sequence and zeros in all the other columns and then concatenate the two? Pandas 中是否有任何内置函数来执行此操作,或者我是否创建一个单独的数据框,其中包含所有其他列中的附加日期序列和零,然后将两者连接起来?

There is one way有一种方法

df=df.set_index('Date').reindex(pd.date_range(end=df.Date.max(),freq='W-WED',periods=8),fill_value=0)
            Var_1  Var_2  Var_3
2013-12-11      0      0      0
2013-12-18      0      0      0
2013-12-25      0      0      0
2014-01-01      1      1      2
2014-01-08      2      1      3
2014-01-15      2      1      3
2014-01-22      1      0      3
2014-01-29      3      0      2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM