# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

# +
import sys
import numba
import swifter
import timeit
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

__author__ = '<NAME>'
__version__ = 'Python 2'
# -

data = pd.read_csv('../Data/bid_fe.csv')
data.sample(5)

len(data)

hashmap = dict()
memberkeys = sorted(list(set(data.MemberKey)))[60000:]
len(memberkeys)

start = timeit.default_timer()
for key in memberkeys:
    hashmap[key] = data.query("MemberKey == '{0}'".format(key))
stop = timeit.default_timer()
print "Hashmap generated in {0} seconds".format((stop-start)) 

data = data[data['MemberKey'].isin(memberkeys)]

len(data)


# ## Lender Winning Bids

# +
def lender_winning_bids(x):
    return hashmap[x["MemberKey"]].query("Status == 'Winning' and ListingStartDate < '{0}'".format(x["ListingStartDate"]))["Bid_Key"].nunique()

start = timeit.default_timer()    
data["LenderWinningBids"] = data[["MemberKey", "ListingStartDate"]].swifter.apply(lender_winning_bids, axis=1)
stop = timeit.default_timer()
print "Feature engineering completed in {0} seconds".format((stop-start)) 
# data[["MemberKey", "ListingKey", "ListingStartDate", "Bid_Key", "BidCreationDate", "Status", "LenderWinningBids"]].query("MemberKey=='365434772269169153CB3D3'").sort_values("ListingStartDate")
# -

# ## Lender Total Bids

# +
def lender_total_bids(x):
    return hashmap[x["MemberKey"]].query("ListingStartDate < '{0}'".format(x["ListingStartDate"]))["Bid_Key"].nunique()

start = timeit.default_timer()    
data["LenderTotalBids"] = data[["MemberKey", "ListingStartDate"]].swifter.apply(lender_total_bids, axis=1)
stop = timeit.default_timer()
print "Feature engineering completed in {0} seconds".format((stop-start)) 
# data[["MemberKey", "ListingKey", "ListingStartDate", "Bid_Key", "BidCreationDate", "Status", "LenderWinningBids", "LenderTotalBids"]].head()
# -

data[["MemberKey", "ListingKey", "ListingStartDate", "Bid_Key", "BidCreationDate", "Status", "LenderWinningBids", "LenderTotalBids"]].query("MemberKey=='{0}'".format(memberkeys[0])).sort_values("ListingStartDate")

# ## Save Data

data.to_csv("lender_bid_fe_65k.csv", index=False)



StarcoderdataJupyterScriptsDedupFiltered