"""
Will calculate the weighted (by day) time average of the group passed.
It will not operate on the day it is evaulating but the previous days_back.
Should be used with the apply() function in Pandas with groupby function
Args:
    group (pandas.DataFrame): Will be passed by pandas
    value_col (str): Name of column with value to be averaged by weight
    time_col (str): Name of column of with times in them
    new_col_name (str): Name of new column to place time weighted average into, default: time_weighted_average
    days_back (str): Time delta description as described in panda time deltas documentation, default: -90 days
    fill_value (any): The value to fill rows which do not have data in days_back period, default: np.nan
Returns:
    (pandas.DataFrame): The modified DataFrame with time weighted average added to columns, np.nan if no
    time weight average exist
"""
for idx, row in group.iterrows():
    days_back_fil = (group[time_col] < row[time_col]) & (group[time_col] >=
        row[time_col] + pd.Timedelta(days_back))
df = group[days_back_fil]
df['days-back'] = (row[time_col] - df[time_col]) / np.timedelta64(1, 'D')
df['weight'] = df[value_col] * df['days-back']
Pandas: взвешенное по времени скользящее среднее с Groupby