I'm trying to make a report on daily balance fluctuations. My idea is make a folder, use glob to open all files, concat and use it to make Scatter chart. Below is my sample code:
import pandas as pd
import numpy as np
df1 = pd.DataFrame({
'BAS_DT': ['20211010','20211010','20211011','20211011','20211012','20211012'],
'REF_NO': ["VN121001211","VN121001212","VN121001211","VN121001212","VN121001212","VN121001213"],
'APPR_LIM':[1,2,1,2,2,3]})
df1.head(6)
#df1
BAS_DT REF_NO APPR_LIM
0 20211010 VN121001211 1
1 20211010 VN121001212 2
2 20211011 VN121001211 1
3 20211011 VN121001212 2
4 20211012 VN121001212 2
5 20211012 VN121001213 3
import datetime
Previous_date=datetime.datetime.today() - datetime.timedelta(days=1)
Previous_date = Previous_date.strftime('%Y%m%d')
df2=df1[df1['BAS_DT']==Previous_date]
print(df2)
#df2
BAS_DT REF_NO APPR_LIM
2 20211011 VN121001211 1
3 20211011 VN121001212 2
df3 = df1.tail(2)
print(df3)
#df3
BAS_DT REF_NO APPR_LIM
4 20211012 VN121001212 2
5 20211012 VN121001213 3
After getting data of last day and previous day, I use isin
to find out which REF_NO has been deleted:
df2['Match'] = df2['REF_NO'].isin(df3['REF_NO'])
df2 = df2[(df2['Match']==False)]
print(df2)
#df2
BAS_DT REF_NO APPR_LIM Match
2 20211011 VN121001211 1 False
But problem is if I make report on Monday and actual Previous_date
is weekend (No new balance is generated at the weekend), formula df2=df1[df1['BAS_DT']==Previous_date]
will return results with nothing.
What should I do in this case?
Thanks and best regards
CodePudding user response:
Instead of minusing datetime.timedelta(days=1)
, use BDay
offset from pandas, which calculates business date offset:
import datetime
from pandas.tseries.offsets import BDay
Previous_date=datetime.datetime.today() - BDay(1)
Previous_date = Previous_date.strftime('%Y%m%d')
Previous_date
'20211008'
Today is 2021-10-11
, Monday, the previous business day was 2021-10-08
.