Merge dataframes and fill in blank values based on start/end dates

I have pandas dataframe (df) with start and end dates for certain value (in this case 'currency'). I need to merge it with another dataframe (tbl) and fill in blank currency rows based on start/end dates from the first DF. NULL means no end date - so everything going forward. In this case everything after 01/11/2020 is USD. Data coming from SQL therefore NULL.

df = pd.DataFrame(data={
        'port': 'PortA'
        'currency': ['USD', 'CAD', 'EUR', 'USD'],
        'start_date': ['01/01/2020', '01/04/2020', '01/06/2020', '01/11/2020'],
        'end_date': ['01/04/2020', '01/06/2020', '01/11/2020', '01/15/2020']
        })
df[['start_date', 'end_date']] = df[['start_date', 'end_date']].apply(pd.to_datetime, errors='ignore')
tbl = pd.DataFrame(data={
        'port': 'PortA',
        'as_of_date': [x for x in pd.date_range(start='01/01/2020', end='01/15/2020')]
        })

This is what I need

df_merge is final look I need. Secondary question - what if I don't have second dataframe (tbl) to merge with. Is there an easy way to 'unstack' the existing df so it looks the same way as df_merge?

Thank you.



from Recent Questions - Stack Overflow https://ift.tt/2FZinnG
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)