Skip to content

Commit

Permalink
Add primary key auto-detection for semantic sdtypes (#1731)
Browse files Browse the repository at this point in the history
  • Loading branch information
fealho authored Jan 10, 2024
1 parent 6659835 commit 15f18fd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
10 changes: 8 additions & 2 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 @@ -407,15 +408,20 @@ def _detect_columns(self, data):
sdtype = 'unknown'

column_dict = {'sdtype': sdtype}

if sdtype in self._REFERENCE_TO_SDTYPE.values() or sdtype == 'unknown':
column_dict['pii'] = True
elif sdtype == 'datetime' and dtype == 'O':
if sdtype in self._REFERENCE_TO_SDTYPE.values() and first_pii_field is None:
first_pii_field = field
if 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 @@ -205,6 +205,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 @@ -269,6 +270,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 @@ -327,6 +327,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 15f18fd

Please sign in to comment.