I have a monthly dataset that I want to interpolate daily. However I need to interpolate from the 15th of each month or midmonth (Jan 16th, Feb 14th, March 15th...), to the next one.
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
...
First: I need these values to be set every mid-month.
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
Second: Then daily interpolate
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
I'm able to interpolate each month from the 1st to the 31st using the following code:
### 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
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
Any help will be greatly appreciated!
Here's what i would do:
# offset the timestamp
df[0] -= pd.to_timedelta(df[0].dt.day//2, unit='d')
which gives you
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
and you can interpolate:
df.set_index(0).asfreq('D').interpolate()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.