# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# +
# default_exp utils.aggregate
# -

# # Aggregate
# > Aggregation of records.

#hide
from nbdev.showdoc import *

#export
import numpy as np
import pandas as pd


#export
def simple_aggregate(data,
                     drop_duplicates=True,
                     by='AFFINITY',
                     aggregation='mean',
                     weights=None,
                     half_life=None,
                     ):
  
  if drop_duplicates:
    data = data.drop_duplicates()
  
  if by in data.columns:

    if weights:
      data = data.replace({by:weights})

    if half_life:
      t_ref = pd.to_datetime(data['TIMESTAMP']).max()
      data[by] = data.apply(lambda x: x[by] * np.power(0.5, (t_ref - pd.to_datetime(x['TIMESTAMP'])).days / half_life), axis=1)

    data = data.groupby(['USERID', 'ITEMID']).agg({by: aggregation}).reset_index()

  else:

    data[by] = 1
    data = data.groupby(['USERID', 'ITEMID']).agg({by: 'count'}).reset_index()

  return data

#hide
# !pip install -q watermark
# %reload_ext watermark
# %watermark -a "Sparsh A." -m -iv -u -t -d

StarcoderdataJupyterScriptsDedupFiltered