Last active
September 12, 2022 04:04
-
-
Save hprobotic/c634e5a97d25b77a59d980c811218a33 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
selected_columns = ["Amount", "Date", "Where"] | |
def find_diff_only(origin_df, target_df, identity="DBS"): | |
shared_with_origin = pd.merge_asof( | |
origin_df, | |
target_df, | |
on="Date", | |
suffixes=("_origin", "_target"), | |
direction="nearest", | |
tolerance=pd.Timedelta(minutes=2), | |
) | |
origin_only = shared_with_origin[ | |
shared_with_origin["Amount_origin"] != shared_with_origin["Amount_target"] | |
] | |
origin_only["Amount"] = origin_only["Amount_origin"] | |
origin_only["Date"] = origin_only["Date"] | |
origin_only["Where"] = identity | |
origin_only = origin_only[selected_columns] | |
for i in origin_only.index: | |
origin_df.loc[i, "is_diff"] = 1 | |
return origin_only | |
def find_diff(dash_df, pos_df): | |
dbs_only = find_diff_only(dash_df, pos_df, identity="DBS") | |
pos_only = find_diff_only(pos_df, dash_df, identity="POS") | |
final_diff = dbs_only.append(pos_only) | |
return final_diff, len(final_diff), dbs_only.index, pos_only.index | |
dash_df = pd.DataFrame( | |
{ | |
"Date": [ | |
pd.Timestamp("2020-03-25 13:30:00.023"), # match | |
pd.Timestamp("2020-03-26 13:30:00.023"), # dash_only | |
pd.Timestamp("2020-03-27 13:31:00.023"), # match | |
pd.Timestamp("2020-03-27 13:35:00.023"), # dash_only | |
pd.Timestamp("2020-03-27 13:35:00.023"), # dash_only | |
], | |
"Amount": [100, 200, 300, 400, 400], | |
} | |
) | |
pos_df = pd.DataFrame( | |
{ | |
"Date": [ | |
pd.Timestamp("2020-03-25 13:30:00.023"), # match | |
pd.Timestamp("2020-03-26 13:31:00.023"), # pos_only | |
pd.Timestamp("2020-03-27 13:32:00.023"), # match | |
pd.Timestamp("2020-03-27 13:38:00.023"), # pos_only | |
], | |
"Amount": [100, 210, 300, 400], | |
} | |
) | |
find_diff(dash_df, pos_df) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment