[英]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.