Home > database >  How to copy datetime index of a bigger pandas Dataframe to a smaller Dataframe with unequal length?
How to copy datetime index of a bigger pandas Dataframe to a smaller Dataframe with unequal length?

Time:01-21

I have a dataframe with datetime index

2021-05-05    1923.275024
2021-05-06    1920.799988
2021-05-07    1940.825012
2021-05-10    1933.875000
2021-05-11    1924.275024
2021-05-12    1919.950012
2021-05-14    1924.000000

I have another dataframe with Integer index

0      0.342828
1      0.791374
2      1.261493
3      1.725175
4      2.114718

I want the index of my 2nd dataframe to be similar to first but off set by 2 dates.

Desired Result : `

    2021-05-07      0.342828
    2021-05-10      0.791374
    2021-05-11      1.261493
    2021-05-12      1.725175
    2021-05-14      2.114718

I don't think I can use datetime.timedeltaand shift as the dates are not continuous. Thanks in advance

CodePudding user response:

Use set_index:

df2 = df2.set_index(df1[-len(df2):].index)
print(df2)

# Output
2021-05-07  0.342828
2021-05-10  0.791374
2021-05-11  1.261493
2021-05-12  1.725175
2021-05-14  2.114718
  •  Tags:  
  • Related