Skip to content

Commit

Permalink
Autodetect primary keys for pii columns
Browse files Browse the repository at this point in the history
  • Loading branch information
fealho committed Jan 5, 2024
1 parent 64e8df2 commit 20f6195
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
8 changes: 8 additions & 0 deletions sdv/metadata/single_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ def _detect_columns(self, data):
data (pandas.DataFrame):
The data to be analyzed.
"""
first_pii_field = None
for field in data:
column_data = data[field]
clean_data = column_data.dropna()
Expand Down Expand Up @@ -410,12 +411,19 @@ def _detect_columns(self, data):

if sdtype in self._REFERENCE_TO_SDTYPE.values() or sdtype == 'unknown':
column_dict['pii'] = True
if first_pii_field is None:
first_pii_field = field

elif sdtype == 'datetime' and dtype == 'O':
datetime_format = get_datetime_format(column_data.iloc[:100])
column_dict['datetime_format'] = datetime_format

self.columns[field] = deepcopy(column_dict)

# When no primary key column was set, choose the first pii field
if self.primary_key is None and first_pii_field:
self.primary_key = first_pii_field

def detect_from_dataframe(self, data):
"""Detect the metadata from a ``pd.DataFrame`` object.
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/metadata/test_multi_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ def test_detect_from_dataframes():
'billing_address': {'sdtype': 'unknown', 'pii': True},
'credit_card_number': {'sdtype': 'credit_card_number', 'pii': True}
},
'primary_key': 'guest_email'
}
},
'relationships': [
Expand Down Expand Up @@ -261,6 +262,7 @@ def test_detect_from_csvs(tmp_path):
'billing_address': {'sdtype': 'unknown', 'pii': True},
'credit_card_number': {'sdtype': 'credit_card_number', 'pii': True}
},
'primary_key': 'guest_email'
}
},
'relationships': [
Expand Down
1 change: 1 addition & 0 deletions tests/integration/metadata/test_single_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ def test_detect_from_dataframe_with_pii_names():
# Assert
expected_metadata = {
'METADATA_SPEC_VERSION': 'SINGLE_TABLE_V1',
'primary_key': 'USER PHONE NUMBER',
'columns': {
'USER PHONE NUMBER': {'sdtype': 'phone_number', 'pii': True},
'addr_line_1': {'sdtype': 'street_address', 'pii': True},
Expand Down

0 comments on commit 20f6195

Please sign in to comment.