import pandas as pd
import numpy as np
import datetime as dt

def to_platform_indep(data, platform):
    if platform=='android':
        data.drop(['ios_ifa', 'ios_ifv', 'google_aid'], axis=1, inplace=True)

        # rename cols
        names = data.columns.tolist()
        names[names.index('android_id')] = 'id'
        data.columns = names
    elif platform=='ios':
        data.drop(['android_id','google_aid', 'ios_ifa'], axis=1, inplace=True)

        # rename cols
        names = data.columns.tolist()
        names[names.index('ios_ifv')] = 'id'
        data.columns = names
    return data

def prepare_data(data):
    data.sort_values(by='event_datetime', inplace=True)
    data.reset_index(drop=True, inplace=True)
    data['event_datetime'] = pd.to_datetime(data['event_datetime'], format='%Y-%m-%d %H:%M:%S')
    return to_platform_indep(data, PLATFORM)
    
def round_datetime(date_time, date_delta):
    rounded = None
    if(date_delta.days > 0):
        rounded = date_time - dt.timedelta(days = date_time.day%date_delta.days)
        rounded = rounded.replace(hour = 0, minute = 0, second=0)
    else:
        rounded = date_time - dt.timedelta(hours = date_time.hour%(date_delta.seconds/3600))
        rounded = rounded.replace(minute = 0, second=0)

    return rounded

def features_on_interval(data, start, interval):
    features = pd.DataFrame(columns = cols, index = [])
    period_data = data[(start <= data['event_datetime']) &
                       (data['event_datetime'] < start + interval)]
    periods_ids = period_data['id'].unique()
    
    for id in periods_ids:
        ids_data4period = period_data[period_data['id'] == id]
        events = ids_data4period['event_name'].value_counts()   
        features.loc[len(features)] = events

    if len(features) > 0 :
        features['id'] = periods_ids                
        features['event_datetime'] = \
                round_datetime(period_data['event_datetime'].iloc[0], interval)
    return features
#%%     
###############################################################################
FILENAME = 'data/events_android.csv'
OUTFILE = 'data/android.csv'
PLATFORM = 'android'
PERIOD = dt.timedelta(days=1)

# input data
data = pd.read_csv(FILENAME)
data = prepare_data(data)

# for output data
cols = ['event_datetime', 'id']
cols.extend(data['event_name'].unique().tolist())
features = pd.DataFrame(columns = cols, index = [])

# cycle over date-time periods
first_date = round_datetime(data['event_datetime'].iloc[0], PERIOD)
end_date = round_datetime(data['event_datetime'].iloc[-1], PERIOD)
date_list = [first_date + x*PERIOD for x in range(0, (end_date-first_date+PERIOD)//PERIOD)]
#%% 
for period_starts in date_list:
    cur_features = features_on_interval(data, period_starts, PERIOD)
    features = features.append(cur_features, ignore_index=True)

# final clean
features.fillna(0, inplace=True)
features = features.set_index(['event_datetime','id'])
features = features.reindex_axis(sorted(features.columns), axis=1)
features.to_csv(OUTFILE, sep=',', header=True) 
@@ -1719,19 +1719,16 @@
 counts()
-   
 %0A       
@@ -1985,16 +1985,781 @@
 eatures%0A
+    %0Adef delay_target(cur_f, next_f):%0A    if len(cur_f) == 0 or len(next_f) == 0:%0A        return cur_f%0A        %0A    cur_f.set_index('id', inplace=True)%0A    next_f.set_index('id', inplace=True)%0A    cur_datetime = cur_f%5B'event_datetime'%5D.iloc%5B0%5D%0A    target = next_f%5B'process_orderSendSuccess_event'%5D.copy().dropna()%0A%0A    if len(target) %3E 0:%0A        new_ids = set(target.index) - set(cur_f.index)%0A%0A        # append new ids%0A        for id in new_ids:%0A            new_row = pd.Series(%5Bcur_datetime, 1%5D, %0A                          index=%5B'event_datetime', 'process_orderSendSuccess_event'%5D)%0A            cur_f.loc%5Bid%5D = new_row%0A%0A        # update target%0A        cur_f%5B'process_orderSendSuccess_event'%5D = target%0A    cur_f.reset_index(level=0, inplace=True)%0A    return cur_f%0A
 #%25%25     
@@ -2956,16 +2956,36 @@
 days=1)%0A
+DELAY = 1 # 1 PERIOD
 %0A# input
@@ -3364,16 +3364,25 @@
  PERIOD)
+ - PERIOD
 %0Adate_li
@@ -3572,16 +3572,165 @@
 PERIOD)%0A
+    next_features = features_on_interval(data, period_starts + DELAY*PERIOD, PERIOD)%0A    delayd_features = delay_target(cur_features, next_features)%0A
     feat
@@ -3752,19 +3752,22 @@
 .append(
-cur
+delayd
 _feature
@@ -3794,14 +3794,8 @@
 )%0A%0A#
- final
  cle
@@ -3829,16 +3829,23 @@
 ce=True)
+%0A%0A# out
 %0Afeature

add DELAY between features and target