简体   繁体   English

使用高频数据作为低频数据的代理

[英]Using higher frequency data as a proxy for lower-frequency data

I have two times series - annual and quarterly.我有两次系列 - 年度和季度。 Annual data ends in 2018, but quarterly data goes till 2019q3.年度数据在 2018 年结束,但季度数据持续到 2019 年第三季度。 What's the best way to combine the two, where Python checks what's the latest available quarterly and annual data and fills annual time series with the latest quarterly value?将两者结合起来的最佳方法是什么,Python 检查最新的可用季度和年度数据是什么,并用最新的季度值填充年度时间序列?

This is what I have in mind:这就是我的想法:

Data_ann
2013 5.1
2014 3.2
2015 2.1
2016 2.2
2017 2.1
2018 4.2
2019 n/a

Data_qtr
...
2018q1 2.5 
2018q2 2.2
2018q3 3.7
2018q4 4.2
2019q1 1.2
2019q2 2.3
2019q3 n/a

and the result和结果

2013 5.1
2014 3.2
2015 2.1
2016 2.2
2017 2.1
2018 4.2
2019 2.3

You can organize your data to use a DatetimeIndex .您可以组织数据以使用DatetimeIndex The yearly frame is then fine (if there's one row per year) but for the quarter DataFrame we need to take the last value in each year, accomplished with resample.last .年度框架就可以了(如果每年有一行),但是对于季度 DataFrame,我们需要采用每年的最后一个值,通过resample.last完成。 combine_first gives us priority to the yearly DataFrame when we join them.当我们加入时, combine_first让我们优先考虑每年的 DataFrame。

Data Prep数据准备

df_ann = pd.read_clipboard(header=None)
df_ann.columns = ['date', 'value']
df_ann['date'] = pd.to_datetime(df_ann['date'], format='%Y')
df_ann = df_ann.set_index('date')
#            value
#date             
#2013-01-01    5.1
#2014-01-01    3.2
#2015-01-01    2.1
#2016-01-01    2.2
#2017-01-01    2.1
#2018-01-01    4.2
#2019-01-01    NaN

df_qtr = pd.read_clipboard(header=None)
df_qtr.columns = ['date', 'value']
df_qtr['date'] = pd.to_datetime(df_qtr['date'])
df_qtr = df_qtr.set_index('date')
#            value
#date             
#2018-01-01    2.5
#2018-04-01    2.2
#2018-07-01    3.7
#2018-10-01    4.2
#2019-01-01    1.2
#2019-04-01    2.3
#2019-07-01    NaN

Code代码

df_ann.to_period('Y').combine_first(df_qtr.resample('Y').last().to_period('Y'))

      value
date       
2013    5.1
2014    3.2
2015    2.1
2016    2.2
2017    2.1
2018    4.2
2019    2.3

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

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