0

I have 2 different Dataframes. One is a list of "Values" that have a date, and a value. The second Dataframe is a dataframe of dates, that lie in between the dates of Dataframe 1. I want to merge the 2 Dataframes and interpolate the values from dataframe 1 to the values in dataframe2.

I have attempted the below:

sample_final= sample_set_1.merge(sample_set_2, how='right').set_index('Maturity').interpolate()

but it doesnt work! im going around in a loop!

Any help appreciated!

Thanks

Maturity    DF
2019-04-11  1.0
2019-04-12  0.999933
2019-04-15  0.999732
2019-04-16  0.9996649999999999
2019-04-23  0.9991969999999999
2019-04-29  0.9987959999999999
2019-05-06  0.9983290000000001
2019-05-15  0.9977290000000001
2019-06-17  0.995532
2019-07-15  0.99369
2019-08-15  0.991663
2019-09-16  0.989582
2019-10-15  0.987725
2019-11-15  0.985764
2019-12-16  0.98383
2020-01-15  0.9819899999999999
2020-02-18  0.979935
2020-03-16  0.9783209999999999
2020-04-15  0.97655
2020-07-15  0.971353
2020-10-15  0.96631
2021-04-15  0.9568610000000001
2022-04-19  0.938263
2023-04-17  0.9198
2024-04-15  0.9006190000000001

Dataframe2:

Maturity
2019-04-17
2019-04-17
2019-04-17
2019-04-17
2019-04-17
2019-04-17
2019-04-17
2019-04-17
2019-04-17
2019-04-17
2019-04-17
2019-04-17
2019-04-17
2019-04-17
2019-04-17
2019-04-17
2019-04-18
2019-04-18
2019-04-18
2019-04-18
2019-04-18
2019-04-18
2019-04-18
2019-04-18
2019-04-18
2019-04-18
2019-04-18
2019-04-18
2019-04-18
2019-04-18
2019-04-18
2019-04-18
2019-04-18
2019-04-18
2019-04-18
2019-04-18
2019-04-18
2019-04-23
2019-04-23
2019-04-23
2019-04-23
2019-04-23
2019-04-24
2019-04-24
2019-04-24
2019-04-24
2019-04-24
2019-04-24
2019-04-24
2019-04-24
2019-04-25
2019-04-25
2019-04-26
2019-04-26
2019-04-26
2019-04-26
2019-04-26
2019-04-26
2019-04-26
2019-04-26
2019-04-26
2019-04-26
2019-04-26
2019-04-26
2019-04-26
2019-04-29
2019-04-29
2019-04-30
2019-04-30
2019-04-30
2019-04-30
2019-04-30
2019-05-02
2019-05-03
2019-05-03
2019-05-03
2019-05-03
2019-05-03
2019-05-03
2019-05-03
2019-05-03
2019-05-03
2019-05-03
2019-05-03
2019-05-03
2019-05-03
2019-05-03
2019-05-07
2019-05-07
2019-05-07
2019-05-07
2019-05-07
2019-05-07
2019-05-08
2019-05-08
2019-05-08
2019-05-08
2019-05-09
2019-05-09
2019-05-09
2019-05-10
2019-05-10
2019-05-10
2019-05-10
2019-05-13
2019-05-13
2019-05-14
2019-05-14
2019-05-14
2019-05-14
2019-05-14
2019-05-15
2019-05-15
2019-05-15
2019-05-15
2019-05-15
2019-05-15
2019-05-15
2019-05-15
2019-05-15
2019-05-15
2019-05-15
2019-05-15
2019-05-15
2019-05-15
2019-05-16
2019-05-16
2019-05-16
2019-05-16
2019-05-16
2019-05-17
2019-05-17
2019-05-17
2019-05-20
2019-05-21
2019-05-21
2019-05-22
2019-05-22
2019-05-22
2019-05-22
2019-05-22
2019-05-22
2019-05-22
2019-05-22
2019-05-22
2019-05-22
2019-05-22
2019-05-22
2019-05-22
2019-05-22
2019-05-22
2019-05-22
2019-05-22
2019-05-22
2019-05-22
2019-05-22
2019-05-22
2019-05-22
2019-05-23
2019-05-23
2019-05-23
2019-05-23
2019-05-24
2019-05-24
2019-05-24
2019-05-28
2019-05-28
2019-05-29
2019-06-07
2019-06-07
2019-06-11
2019-06-13
2019-06-14
2019-06-18
2019-06-18
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-19
2019-06-20
2019-06-20
2019-06-20
2019-06-21
2019-06-25
2019-06-28
2019-06-28
2019-06-28
2019-06-28
2019-07-03
2019-07-11
2019-07-12
2019-07-12
2019-07-15
2019-07-15
2019-07-15
2019-07-15
2019-07-15
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-16
2019-07-17
2019-07-17
2019-07-17
2019-07-17
2019-07-17
2019-07-17
2019-07-17
2019-07-17
2019-07-17
2019-07-17
2019-07-17
2019-07-17
2019-07-17
2019-07-19
2019-07-22
2019-07-22
2019-07-22
2019-07-24
2019-07-25
2019-08-21
2019-08-21
2019-09-06
2019-09-17
2019-09-18
2019-09-30
2019-10-02
2019-10-07
2019-10-16

Alex Walton
  • 173
  • 3
  • 17
  • There are a lot of repeated dates in dataframe2, do they interpolate to the same value? – Quang Hoang Apr 16 '19 at 13:24
  • That is intentional, as there are repeated dates that come from another dataset. When i run the interpolate code above, they do interpolate to the same value but they dont correctly interpolate between the dates. – Alex Walton Apr 16 '19 at 13:27

1 Answers1

1

Assuming you want to interpolate linearly on day:

df.Maturity = pd.to_datetime(df.Maturity)
df2.Maturity = pd.to_datetime(df2.Maturity)

# get the common date in the two data frames
common = df.merge(df2.drop_duplicates(), how='outer').set_index('Maturity')

# interpolate the value:
common = common.resample('d').interpolate().reset_index()

# merge
common.merge(df2, how='right')
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74