Skip to content

Instantly share code, notes, and snippets.

@hprobotic
Last active September 12, 2022 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hprobotic/c634e5a97d25b77a59d980c811218a33 to your computer and use it in GitHub Desktop.
Save hprobotic/c634e5a97d25b77a59d980c811218a33 to your computer and use it in GitHub Desktop.
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