[英]Determine current month variable that rolls over to next month after the 15th day- python 2.7
[英]how to linearly interpolate monthly data from the 15th of each month that rolls over the 15th of the next month
我有一个每月要插值的数据集。 但是我需要从每个月的15号或每个月中旬(1月16日,2月14日,3月15日...)进行插值。
Here is my data set:
2000-01-31 0.02451
2000-02-28 0.03392
2000-03-31 0.15451
2000-04-30 0.28366
2000-05-31 0.46806
2000-06-30 0.67766
...
首先:我需要在每个月中设置这些值。
2000-01-16 0.02451
2000-02-14 0.03392
2000-03-16 0.15451
2000-04-15 0.28366
2000-05-16 0.46806
2000-06-15 0.67766
第二:然后每天插值
2000-01-16 0.02451
2000-01-17
2000-01-18
...
2000-01-31
2000-02-01
2000-02-02
...
2000-02-14 0.03392
我可以使用以下代码从1号到31号每月进行插值:
### Linear interpolation from monthly values to daily
import pandas as pd
df.set_index(pd.date_range(start='1/1/2000', end='1/1/2010', freq='M'), inplace=True, drop=True)
rng = pd.date_range(start='1/1/2000', end='1/1/2010', freq='D')
df2 = df.reindex(rng, axis=0).interpolate(axis=0)
结果
RESULTS EXPECTED RESULTS
2000-01-31 0.02451 2000-01-16 0.02451
2000-02-01 0.02485 2000-02-17 0.02485
2000-02-02 0.02518 2000-02-18 0.02518
...
2000-02-26 0.03325 2000-02-12 0.03325
2000-02-27 0.03359 2000-02-13 0.03359
2000-02-28 0.03392 2000-02-14 0.03392
任何帮助将不胜感激!
这是我会做的:
# offset the timestamp
df[0] -= pd.to_timedelta(df[0].dt.day//2, unit='d')
这给你
0 1
0 2000-01-16 0.02451
1 2000-02-14 0.03392
2 2000-03-16 0.15451
3 2000-04-15 0.28366
4 2000-05-16 0.46806
5 2000-06-15 0.67766
您可以插值:
df.set_index(0).asfreq('D').interpolate()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.