-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fields difference #167
Merged
Merged
Fields difference #167
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e336d33
Add compare_fields
manycoding e89ac5a
Ignore * warnings
manycoding 46a6ab7
Refactor price
manycoding 6f2b100
Support nested structures
manycoding 6a24808
Merge branch 'master' into fields_difference
manycoding 4df498a
Wee update
manycoding e88618d
Merge branch 'fields_difference' of https://github.com/scrapinghub/ar…
manycoding 10897ce
Add normalization
manycoding da3b312
Add more_stats to easily access all data, replace Result class eq wit…
manycoding 22cd400
Update rules to new assert
manycoding 96e4aa6
Fix tqdm warning
manycoding 933d05e
Add line to changes, describe err_thr
manycoding faa6263
Merge with master
manycoding File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
from typing import Tuple | ||
|
||
from arche.readers.schema import TaggedFields | ||
from arche.rules.result import * | ||
|
||
|
||
MAX_MISSING_VALUES = 6 | ||
|
||
|
||
def fields( | ||
source_df: pd.DataFrame, | ||
target_df: pd.DataFrame, | ||
names: List[str], | ||
normalize: bool = False, | ||
err_thr: float = 0.25, | ||
) -> Result: | ||
"""Finds fields values difference between dataframes. | ||
|
||
Args: | ||
names - a list of field names | ||
normalize - if set, all fields converted to str and processed with lower() and strip() | ||
err_thr - sets the failure threshold for missing values | ||
|
||
Returns: | ||
Result with same, missing and new values. | ||
""" | ||
|
||
def get_difference( | ||
left: pd.Series, right: pd.Series | ||
) -> Tuple[pd.Series, pd.Series, pd.Series]: | ||
return ( | ||
left[left.isin(right)], | ||
left[~(left.isin(right))], | ||
right[~(right.isin(left))], | ||
) | ||
|
||
result = Result("Fields Difference") | ||
for field in names: | ||
source = source_df[field].dropna() | ||
target = target_df[field].dropna() | ||
if normalize: | ||
source = source.astype(str).str.lower().str.strip() | ||
target = target.astype(str).str.lower().str.strip() | ||
try: | ||
same, new, missing = get_difference(source, target) | ||
except SystemError: | ||
source = source.astype(str) | ||
target = target.astype(str) | ||
same, new, missing = get_difference(source, target) | ||
|
||
same.name, new.name, missing.name = (None, None, None) | ||
result.more_stats.update( | ||
{f"{field}": {"same": same, "new": new, "missing": missing}} | ||
) | ||
result.add_info( | ||
f"{len(source)} `non NaN {field}s` - {len(new)} new, {len(same)} same" | ||
) | ||
if len(missing) == 0: | ||
continue | ||
|
||
if len(missing) < MAX_MISSING_VALUES: | ||
msg = ", ".join(missing.unique().astype(str)) | ||
else: | ||
msg = f"{', '.join(missing.unique()[:5].astype(str))}..." | ||
msg = f"{msg} `{field}s` are missing" | ||
if len(missing) / len(target_df) >= err_thr: | ||
result.add_error( | ||
f"{len(missing)} `{field}s` are missing", | ||
errors={msg: set(missing.index)}, | ||
) | ||
else: | ||
result.add_info( | ||
f"{len(missing)} `{field}s` are missing", | ||
errors={msg: set(missing.index)}, | ||
) | ||
return result | ||
|
||
|
||
def tagged_fields( | ||
source_df: pd.DataFrame, | ||
target_df: pd.DataFrame, | ||
tagged_fields: TaggedFields, | ||
tags: List[str], | ||
) -> Result: | ||
"""Compare fields tagged with `tags` between two dataframes.""" | ||
name = f"{', '.join(tags)} Fields Difference" | ||
result = Result(name) | ||
fields_names: List[str] = list() | ||
for tag in tags: | ||
tag_fields = tagged_fields.get(tag) | ||
if tag_fields: | ||
fields_names.extend(tag_fields) | ||
if not fields_names: | ||
result.add_info(Outcome.SKIPPED) | ||
return result | ||
result = fields(source_df, target_df, fields_names) | ||
result.name = name | ||
return result |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's unclear what err_thr does. I think it would be a good idea to document what the variable does on the doctring of the function;