0

I got 2 Dataframes. The first has a Timeindex and a Timecolumn. The second has a Timeindex, but its columns are irrelevant.

Both timeindex are sorted in timeorder, but dont have a regularity inside. (e.g. holidays or specific dates are left out)

Now I need to reset the Timecolumn of the first Dataframe to "one position back in thesecond Df's-TimeIndex".

I also need to reset the TimeIndex of the first Dataframe to "one position back in thesecond Df's-TimeIndex".

first df
                                     t1
2014-10-13 00:00:00 2014-10-13 00:00:00
2014-10-14 00:00:00 2014-10-14 00:00:00
2014-10-15 00:00:00 2014-10-15 00:00:00
2014-10-16 00:00:00 2014-10-16 00:00:00
2014-10-17 00:00:00 2014-10-17 00:00:00
                                ...
2016-02-22 16:00:00 2016-02-23 14:00:00 # in this later samples u can see
2016-02-22 17:00:00 2016-02-23 14:00:00 # that there is no common pattern
2016-02-22 18:00:00 2016-02-23 14:00:00
2016-02-22 19:00:00 2016-02-22 19:00:00
2016-02-22 20:00:00 2016-02-22 20:00:00

[604 rows x 1 columns]
second df:
                          Val       
Time                                                          
2014-10-10 00:00:00       ...  
2014-10-13 00:00:00       ...
2014-10-14 00:00:00       ...  
2014-10-15 00:00:00       ...
2014-10-16 00:00:00       ...  
                      ...       ...  ...            ...            ...
2016-02-23 16:00:00       ...
2016-02-23 17:00:00       ...   
2016-02-23 18:00:00       ... 
2016-02-23 19:00:00       ...   
2016-02-23 20:00:00       ...

expected output:

It is like I want to get the numberLocation of the ValueColumn from the first Dataframe -> in the TimeIndex of the second Dataframe , then subtract 1 of this numberIndex, so that it will be "shifted back" in terms of the Second df's index. And then get the TimeIndex-values of the second df which are located at the new numberIndex, to fill the ValueColumn of the first Index with them.


                                     t1
2014-10-13 00:00:00 2014-10-10 00:00:00
2014-10-14 00:00:00 2014-10-13 00:00:00
2014-10-15 00:00:00 2014-10-14 00:00:00
2014-10-16 00:00:00 2014-10-15 00:00:00
2014-10-17 00:00:00 2014-10-16 00:00:00
                                ... 
Benoid
  • 209
  • 1
  • 4
  • 11

1 Answers1

0

You can use the shift operator:

df1 = df1.shift(1)

and then map the index

df2.index = df2.index.map(df1.t1)

The first row in t1 will have a NAN value, so you want to keep this out.

Chris
  • 387
  • 1
  • 8
  • No. Maybe my description was unclear, but I need its index in X. Why that? Well the VALUES in t1 are NOT the index of X in shifted. (This is the case for the Index(!) of t1 and the index of X). I need the numberlocations of "t1"s Values in X's Index. – Benoid May 26 '20 at 08:46