jmquintana79
3/10/2017 - 6:03 AM

Fill temporal holes in df trend data

Fill temporal holes with any determined frequency.

data -- dataframe with a temporal column as a index.
sfreq -- frequency of temporal steps to be set (default 'D' = daily). 
         Possible frequencies:
         Y: yearly, M: monthly, W: weekly, D: daily, H: hourly, '30min': each 30 minutes (and so on)
return -- filled data with NaN values in the other columns.
## fill temporal holes
def fill_dtholes(data:'dataframe',sfreq:str='D')->'dataframe':
    """
    Fill temporal holes with any determined frequency.
    data -- dataframe with a temporal column as a index.
    sfreq -- frequency of temporal steps to be set (default 'D' = daily). 
             Possible frequencies:
             Y: yearly, M: monthly, W: weekly, D: daily, H: hourly, '30min': each 30 minutes (and so on)
    return -- filled data with NaN values in the other columns.
    """
    from pandas import DataFrame, concat, date_range
    from numpy import ones
    dti = data.index.tolist()[0]
    dtf = data.index.tolist()[-1]
    ldt = date_range(start=dti, end=dtf, freq=sfreq).to_pydatetime() 
    AUX = DataFrame({'datetime':ldt,'aux':ones(len(ldt))}).set_index('datetime')
    data = concat([data,AUX],axis=1).drop('aux', axis=1, inplace=False)
    # clean
    del(AUX)
    # return
    return data