根据月度数据计算年同比增长率

2024-09-29 06:25:55 发布

您现在位置:Python中文网/ 问答频道 /正文

给定如下数据集:

[{'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列对我们来说是未知的,那么是否可以基于月环比计算年环比增长率,或者在Python中反之亦然

参考资料:

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


Tags: and数据httpsandatewwwnanask
1条回答
网友
1楼 · 发布于 2024-09-29 06:25:55

是的,您可以根据滚动(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

相关问题 更多 >