def inactive_customer_cohorts(startDate, endDate):
try:
print("startDate : ", startDate)
print("endDate : ", endDate)
df = data_util.get_customer_scans_df(startDate, endDate)
columns = ['userId', 'createdAt']
df = df[columns]
df['scanQuater'] = ('Q' + df['createdAt'].dt.quarter.astype(str) + '/' + df['createdAt'].dt.year.astype(str))
quartersMap = dict(zip(df['scanQuater'].unique(), range(len(df['scanQuater'].unique()))))
scanQuarterUsers = df.groupby('scanQuater')['userId'].apply(list).reset_index()
scanQuarterUsers['length'] = scanQuarterUsers['userId'].apply(len)
quarterUsers = {row['scanQuater']: row['userId'] for _, row in scanQuarterUsers.iterrows()}
quarters = list(quartersMap.keys())
cumulativePreviousUserIds = set()
previousQuarterUserMap = {}
data = []
for i in range(len(quarters)):
currentQuarter = quarters[i]
if currentQuarter in quarterUsers:
nonUniquecurrentUserIds = quarterUsers[currentQuarter]
currentUserIds = set(quarterUsers[currentQuarter])
currentCount = len(currentUserIds)
nonUniqueCurrentCount = len(nonUniquecurrentUserIds)
if cumulativePreviousUserIds:
uniqueCommonUserIds = set()
quarterCommonCounts = {}
for prevQuarter, prevUserIds in previousQuarterUserMap.items():
specificCommonUserIds = currentUserIds.intersection(prevUserIds)
for userId in specificCommonUserIds:
if userId not in uniqueCommonUserIds:
uniqueCommonUserIds.add(userId)
if prevQuarter in quarterCommonCounts:
quarterCommonCounts[prevQuarter] += 1
else:
quarterCommonCounts[prevQuarter] = 1
previousQuartersSummary = ", ".join([f"{count} from {quarter}" for quarter, count in quarterCommonCounts.items()])
totalSpecificCommonCount = len(uniqueCommonUserIds)
newUsers = currentCount - totalSpecificCommonCount
data.append({
"quarter": currentQuarter,
"scanCount": nonUniqueCurrentCount,
"uniqueCounts": currentCount,
"common": totalSpecificCommonCount,
"new": newUsers,
"common_breakdown": previousQuartersSummary
})
else:
data.append({
"quarter": currentQuarter,
"scanCount": nonUniqueCurrentCount,
"uniqueCounts": currentCount,
"common": 0,
"new": currentCount,
"common_breakdown": ""
})
cumulativePreviousUserIds.update(currentUserIds)
previousQuarterUserMap[currentQuarter] = currentUserIds
df = pd.DataFrame(data)
print(df)
return df
except Exception as e:
common_util.error_logs(e, 'inactive_customer_cohorts')
raise e
This is the code I’m trying to run, whenever I run this code normally without any flask or postman, I get a proper output. But whenever I try to run it through postman, I get the following error :
{
"status": 500,
"message": "maximum recursion depth exceeded",
"data": {}
}
Please let me know what should I do to avoid getting this error. Let me know if more info is needed. Thank you.