【问题标题】:Calculate year-over-year growth rate based on month-over-month's根据环比计算同比增长率
【发布时间】:2021-09-28 02:45:32
【问题描述】:

给定一个数据集如下:

[{'date': '2017-01', 'CPI': 242.839, 'MoM%': nan, 'YoY%': nan},
 {'date': '2017-02', 'CPI': 243.603, 'MoM%': 0.0031, 'YoY%': nan},
 {'date': '2017-03', 'CPI': 243.801, 'MoM%': 0.0008, 'YoY%': nan},
 {'date': '2017-04', 'CPI': 244.524, 'MoM%': 0.003, 'YoY%': nan},
 {'date': '2017-05', 'CPI': 244.733, 'MoM%': 0.0009, 'YoY%': nan},
 {'date': '2017-06', 'CPI': 244.955, 'MoM%': 0.0009, 'YoY%': nan},
 {'date': '2017-07', 'CPI': 244.786, 'MoM%': -0.0007, 'YoY%': nan},
 {'date': '2017-08', 'CPI': 245.519, 'MoM%': 0.003, 'YoY%': nan},
 {'date': '2017-09', 'CPI': 246.819, 'MoM%': 0.0053, 'YoY%': nan},
 {'date': '2017-10', 'CPI': 246.663, 'MoM%': -0.0006, 'YoY%': nan},
 {'date': '2017-11', 'CPI': 246.669, 'MoM%': 0.0, 'YoY%': nan},
 {'date': '2017-12', 'CPI': 246.524, 'MoM%': -0.0006, 'YoY%': nan},
 {'date': '2018-01', 'CPI': 247.867, 'MoM%': 0.0054, 'YoY%': 0.0207},
 {'date': '2018-02', 'CPI': 248.991, 'MoM%': 0.0045, 'YoY%': 0.0221},
 {'date': '2018-03', 'CPI': 249.554, 'MoM%': 0.0023, 'YoY%': 0.0236},
 {'date': '2018-04', 'CPI': 250.546, 'MoM%': 0.004, 'YoY%': 0.0246},
 {'date': '2018-05', 'CPI': 251.588, 'MoM%': 0.0042, 'YoY%': 0.028},
 {'date': '2018-06', 'CPI': 251.989, 'MoM%': 0.0016, 'YoY%': 0.0287},
 {'date': '2018-07', 'CPI': 252.006, 'MoM%': 0.0001, 'YoY%': 0.0295},
 {'date': '2018-08', 'CPI': 252.146, 'MoM%': 0.0006, 'YoY%': 0.027},
 {'date': '2018-09', 'CPI': 252.439, 'MoM%': 0.0012, 'YoY%': 0.0228},
 {'date': '2018-10', 'CPI': 252.885, 'MoM%': 0.0018, 'YoY%': 0.0252},
 {'date': '2018-11', 'CPI': 252.038, 'MoM%': -0.0033, 'YoY%': 0.0218},
 {'date': '2018-12', 'CPI': 251.233, 'MoM%': -0.0032, 'YoY%': 0.0191},
 {'date': '2019-01', 'CPI': 251.712, 'MoM%': 0.0019, 'YoY%': 0.0155}]

假设我们不知道CPI 列,是否可以根据逐月计算同比增长率,反之亦然?

参考:

https://www.econ.iastate.edu/ask-an-economist/cpi-and-inflation-relationship-between-mom-and-yoy-values

【问题讨论】:

    标签: python python-3.x pandas dataframe numpy


    【解决方案1】:

    是的,假设您的数据中没有缺少月份,您可以通过滚动(12 个周期)产品从 MoM 增长计算 YoY 增长。但是由于MoM 计算中已有的舍入,您可能会累积一些错误:

    df['MoM%'].add(1).rolling(12).apply(lambda x: x.prod()) - 1
    
    0          NaN
    1          NaN
    2          NaN
    3          NaN
    4          NaN
    5          NaN
    6          NaN
    7          NaN
    8          NaN
    9          NaN
    10         NaN
    11         NaN
    12    0.020667
    13    0.022091
    14    0.023623
    15    0.024644
    16    0.028022
    17    0.028741
    18    0.029564
    19    0.027101
    20    0.022912
    21    0.025368
    22    0.021985
    23    0.019326
    24    0.015777
    Name: MoM%, dtype: float64
    

    MoM 不四舍五入会更准确:

    mom_no_rounding = df['CPI'] / df['CPI'].shift() - 1
    mom_no_rounding.add(1).rolling(12).apply(lambda x: x.prod()) - 1
    
    0          NaN
    1          NaN
    2          NaN
    3          NaN
    4          NaN
    5          NaN
    6          NaN
    7          NaN
    8          NaN
    9          NaN
    10         NaN
    11         NaN
    12    0.020705
    13    0.022118
    14    0.023597
    15    0.024627
    16    0.028010
    17    0.028715
    18    0.029495
    19    0.026992
    20    0.022770
    21    0.025225
    22    0.021766
    23    0.019102
    24    0.015512
    Name: CPI, dtype: float64
    

    【讨论】:

    • 非常感谢,顺便说一句,是否可以从 YoY 计算 MoM 增长?
    • 我不这么认为。您在汇总到 YoY 时丢失了一些信息,所以我认为不可能反过来。
    • 如果我需要将 MoM 到 YoY 的转换应用到多个列,我可以使用df[['MoM1', 'MoM2', ...]].add(1).rolling(12).apply(lambda x: x.prod()) - 1吗?
    • 我没有尝试,但我认为应该可以。
    猜你喜欢
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 2023-04-07
    • 2014-04-03
    • 1970-01-01
    相关资源
    最近更新 更多