Search code examples
pythonpandasconcatenation

Python/Pandas - Concatenating 2 DataFrames with different PeriodIndex frequencies


I want to concatenate 2 DataFrames with different PeriodIndex frequencies, and using for sorting the second level index which is a position.

For example, I have following 2 DataFrames.

import pandas as pd

pr1h = pd.period_range(start='2020-01-01 08:00', end='2020-01-01 11:00', freq='1h')
pr2h = pd.period_range(start='2020-01-01 08:00', end='2020-01-01 11:00', freq='2h')

n_array_1h = [2, 2, 2, 2]
n_array_2h = [0, 1, 0, 1]

index_labels_1h = [pr1h, n_array_1h]
index_labels_2h = [[pr2h[0],pr2h[0],pr2h[1],pr2h[1]], n_array_2h]

values_1h = [[1], [2], [3], [4]]
values_2h = [[10], [20], [30], [40]]

df1h = pd.DataFrame(values_1h, index=index_labels_1h, columns=['Data'])
df1h.index.names=['Period','Position']
df2h = pd.DataFrame(values_2h, index=index_labels_2h, columns=['Data'])
df2h.index.names=['Period','Position']

df1h
                           Data
Period           Position      
2020-01-01 08:00 2            1
2020-01-01 09:00 2            2
2020-01-01 10:00 2            3
2020-01-01 11:00 2            4

df2h
                       Data
Period           Position      
2020-01-01 08:00 0           10
                 1           20
2020-01-01 10:00 0           30
                 1           40

I woudl like to obtain df1h_new, which:

  • keeps the PeriodIndex from df1h,
  • keeps data from the block in df2h having a period.start_time immediately lower or equal than current perdiod.start_time in df1h,
  • keeps data from df1h obviously

So result would be.

df1h_new
                           Data
Period           Position      
2020-01-01 08:00 0           10  # |---> data coming from df2h, block with
                 1           20  # |     start_time =< df1h.index[0].start_time
                 2            1  # ----> data from df1h.index[0]
2020-01-01 09:00 0           10  # |---> data coming from df2h, block with 
                 1           20  # |     start_time =< df1h.index[1].start_time
                 2            2  # ----> data from df1h.index[1]
2020-01-01 10:00 0           30  # and so on...
                 1           40
                 2            3
2020-01-01 11:00 0           30
                 1           40
                 2            4

Please, what would be the recommended way to achieve that? I thank you for your help and support! Bests,


Solution

  • One idea is use concat with Series.unstack and change frequency to same by Series.asfreq, then back filling misisng values and reshape back to MultiIndex:

    df = (pd.concat([df1h['Data'].unstack(),
                     df2h['Data'].unstack().asfreq('H')], axis=1)
            .bfill()
            .stack()
            .sort_index()
            .to_frame('Data'))
    print (df)
                               Data
    Period           Position      
    2020-01-01 08:00 0         10.0
                     1         20.0
                     2          1.0
    2020-01-01 09:00 0         10.0
                     1         20.0
                     2          2.0
    2020-01-01 10:00 0         30.0
                     1         40.0
                     2          3.0
    2020-01-01 11:00 0         30.0
                     1         40.0
                     2          4.0