简体   繁体   English

如何将不规则时间序列重新采样到每日频率,并将其延伸到今天?

[英]How to resample irregular time series to daily frequency and have it span to today?

I have an irregularly spaced (with respect to time frequency) pandas data frame. 我有一个不规则间隔(相对于时间频率)的pandas数据帧。 I can successfully up-sample the data frame to a daily frequency using the resample command, however my problem is that the resampling ends at the last (pre-resampled) data observation. 我可以使用resample命令成功地将数据帧上采样到每日频率,但我的问题是重采样在最后一次(预重采样)数据观察结束。 I would like the resampling to span all the way to today's date. 我希望重新采样一直延伸到今天的日期。

For example, here is the irregular dataframe: 例如,这是不规则的数据帧:

data
Out[1]: 
            Var 1     Var 2   Var 3     Var 4
Dates                                        

2017-09-20   16.0  1.328125   1.375  0.135976
2017-12-13   16.0  1.343750   1.375  0.085391
2018-03-21   15.0  2.191667   2.125  0.274946
2018-06-13   15.0  2.241667   2.375  0.208452
2018-09-26   16.0  4.312500   2.375  0.111803
2018-12-19   17.0  4.279412   2.375  0.083026
2019-03-20   17.0  3.507353   2.375  0.179358

I used 我用了

dset = data.resample('D', convention = 'end').ffill()

which results (the tail end) in 结果(尾端)

dset.tail()
Out[2]: 
            Var 1     Var 2   Var 3     Var 4
Dates                                        
2019-03-16   17.0  4.279412   2.375  0.083026
2019-03-17   17.0  4.279412   2.375  0.083026
2019-03-18   17.0  4.279412   2.375  0.083026
2019-03-19   17.0  4.279412   2.375  0.083026
2019-03-20   17.0  3.507353   2.375  0.179358

which is great, except that the last "upsampling" ended on 3/20/2019, but I would like for it to end on 4/13/2019 (today's date). 这是伟大的,除了最后的“上采样”在3/20/2019结束,但我希望它在4/13/2019(今天的日期)结束。 As you can see, the type of resampling I am after is to simply take the data from the irregular series and repeat it daily until the next (irregular) data point, from which the new observation is repeated until the next (irregular) data point, etc. 正如您所看到的,我所采用的重新采样类型是简单地从不规则系列中获取数据并每天重复这一数据直到下一个(不规则的)数据点,从中重复新的观察直到下一个(不规则的)数据点等

I am sure I am doing something stupid/not adding a simple addendum to the command. 我确信我正在做一些愚蠢/不添加命令的简单附录。 I would prefer to stay within pandas, if possible. 如果可能的话,我宁愿呆在熊猫里面。

I would like the finished data to look like: 完成的数据是这样的:

dset.tail()
Out[2]: 
            Var 1     Var 2   Var 3     Var 4
Dates                                        
2019-03-20   17.0  3.507353   2.375  0.179358
2019-03-21   17.0  3.507353   2.375  0.179358
2019-03-22   17.0  3.507353   2.375  0.179358

more days, repeated

2019-04-11   17.0  3.507353   2.375  0.179358
2019-04-12   17.0  3.507353   2.375  0.179358
2019-04-13   17.0  3.507353   2.375  0.179358

Thank you all either way for any help/hints provided. 非常感谢您提供任何帮助/提示。

Use DataFrame.reindex with pandas.date_range method: DataFrame.reindexpandas.date_range方法一起使用:

dset = data.reindex(
           pd.date_range(start=data.index.min(),
                         end=pd.datetime.today(),
                         freq='D'),
           method='ffill')

[output] [输出]

            Var 1     Var 2  Var 3     Var 4
2017-09-20   16.0  1.328125  1.375  0.135976
2017-09-21   16.0  1.328125  1.375  0.135976
2017-09-22   16.0  1.328125  1.375  0.135976
2017-09-23   16.0  1.328125  1.375  0.135976
2017-09-24   16.0  1.328125  1.375  0.135976
2017-09-25   16.0  1.328125  1.375  0.135976
2017-09-26   16.0  1.328125  1.375  0.135976
2017-09-27   16.0  1.328125  1.375  0.135976
2017-09-28   16.0  1.328125  1.375  0.135976
2017-09-29   16.0  1.328125  1.375  0.135976
...
2019-04-04   17.0  3.507353  2.375  0.179358
2019-04-05   17.0  3.507353  2.375  0.179358
2019-04-06   17.0  3.507353  2.375  0.179358
2019-04-07   17.0  3.507353  2.375  0.179358
2019-04-08   17.0  3.507353  2.375  0.179358
2019-04-09   17.0  3.507353  2.375  0.179358
2019-04-10   17.0  3.507353  2.375  0.179358
2019-04-11   17.0  3.507353  2.375  0.179358
2019-04-12   17.0  3.507353  2.375  0.179358
2019-04-13   17.0  3.507353  2.375  0.179358

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

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