-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move function
drop_unknown_references
from poc
to be directly und…
…er `utils` (#1969)
- Loading branch information
Showing
11 changed files
with
604 additions
and
501 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
"""Utils module.""" | ||
import sys | ||
from copy import deepcopy | ||
|
||
import pandas as pd | ||
|
||
from sdv._utils import _validate_foreign_keys_not_null | ||
from sdv.errors import InvalidDataError, SynthesizerInputError | ||
from sdv.multi_table.utils import _drop_rows | ||
|
||
|
||
def drop_unknown_references(data, metadata, drop_missing_values=True, verbose=True): | ||
"""Drop rows with unknown foreign keys. | ||
Args: | ||
data (dict): | ||
Dictionary that maps each table name (string) to the data for that | ||
table (pandas.DataFrame). | ||
metadata (MultiTableMetadata): | ||
Metadata of the datasets. | ||
drop_missing_values (bool): | ||
Boolean describing whether or not to also drop foreign keys with missing values | ||
If True, drop rows with missing values in the foreign keys. | ||
Defaults to True. | ||
verbose (bool): | ||
If True, print information about the rows that are dropped. | ||
Defaults to True. | ||
Returns: | ||
dict: | ||
Dictionary with the dataframes ensuring referential integrity. | ||
""" | ||
success_message = 'Success! All foreign keys have referential integrity.' | ||
table_names = sorted(metadata.tables) | ||
summary_table = pd.DataFrame({ | ||
'Table Name': table_names, | ||
'# Rows (Original)': [len(data[table]) for table in table_names], | ||
'# Invalid Rows': [0] * len(table_names), | ||
'# Rows (New)': [len(data[table]) for table in table_names] | ||
}) | ||
metadata.validate() | ||
try: | ||
metadata.validate_data(data) | ||
if drop_missing_values: | ||
_validate_foreign_keys_not_null(metadata, data) | ||
|
||
if verbose: | ||
sys.stdout.write( | ||
'\n'.join([success_message, '', summary_table.to_string(index=False)]) | ||
) | ||
|
||
return data | ||
except (InvalidDataError, SynthesizerInputError): | ||
result = deepcopy(data) | ||
_drop_rows(result, metadata, drop_missing_values) | ||
if verbose: | ||
summary_table['# Invalid Rows'] = [ | ||
len(data[table]) - len(result[table]) for table in table_names | ||
] | ||
summary_table['# Rows (New)'] = [len(result[table]) for table in table_names] | ||
sys.stdout.write('\n'.join([ | ||
success_message, '', summary_table.to_string(index=False) | ||
])) | ||
|
||
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
Oops, something went wrong.