My data includes invoices and I have to check whether an invoice was already paid or not. For each invoice I loop through all report dates. If on one day, the invoice doen't show up, it means the customer already made a payment and of course it won't appear again on subsequent days.
You can see from the table below that invoice C was paid on 28/05.
Report Date Invoice No
2019-05-28 D
2019-05-28 A
2019-05-28 B
2019-05-27 A
2019-05-27 B
2019-05-27 C
2019-05-26 A
2019-05-26 B
2019-05-26 C
I wrote the code below, it worked but took too long because there are around 800k entries. This is very inefficient. I wonder if there is a more efficient way to solve this using pandas.
# For every Invoice
for i in range(0,len(documentNo.categories)):
# If the invoice still exists in the newest Report Date (here 28/05), means that it has not been paid yet. So we can skip to check other invoices
if (df.loc[(df['Document No'] == documentNo.categories[i]) & (df['Report Date'] == reportDates.categories[len(reportDates.categories) - 1])].all(1).any()):
continue
# Decrement from date 27/05
for j in range(len(reportDates.categories) - 2,0,-1):
# If the Invoice does not exist on this date, it has been paid
if (df.loc[(df['Document No'] == documentNo.categories[i]) & (df['Report Date'] != reportDates.categories[j])].all(1).any()):
break
As a result I want a new column which shows Open/Closed for each row.
Report Date Invoice No Open/Closed
2019-05-28 D Open
2019-05-28 A Open
2019-05-28 B Open
2019-05-27 A Open
2019-05-27 B Open
2019-05-27 C Closed
2019-05-26 A Open
2019-05-26 B Open
2019-05-26 C Closed