Search code examples
pythonpandasdatetimereindex

Reindexing Pandas Dataframe with specific datetime indexes


I have a simple question best described with an example. I am trying to render output_df based on current_df and parent_df.

current_df: simple time series df with datetime index

Index                            Val
'10-1-2010 08:00:00'              1.23
'10-1-2010 09:00:00'              1.3
'10-1-2010 10:00:00'               1.43

parent_df: another simple time series df

Index                            Val
'10-1-2010 07:00:00'              0.23
'10-1-2010 08:00:00'              1.23
'10-1-2010 09:00:00'              1.3
'10-1-2010 10:00:00'               1.43
'10-1-2010 11:00:00'              2.23

The output_df should:

  1. contain index of parent_df
  2. contain val of 0 if index not in current_df
Index                             Val
'10-1-2010 07:00:00'              0
'10-1-2010 08:00:00'              1.23
'10-1-2010 09:00:00'              1.3
'10-1-2010 10:00:00'               1.43
'10-1-2010 11:00:00'                0

This should be an easy task - I'm just blanking.

Cheers.


Solution

  • I think it is the functionality of reindex

    output_df = current_df.reindex(parent_df.index, fill_value=0)