Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions sdmetrics/single_table/new_row_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,11 @@ def compute_breakdown(
f'{abs(numerical_match_tolerance * row[field])}'
)
elif field in categorical_fields:
if real_data[field].dtype == 'O':
field_filter = f'`{field}` == {repr(row[field])}'
value = row[field]
if isinstance(value, str):
field_filter = f'`{field}` == {repr(value)}'
else:
field_filter = f'`{field}` == {row[field]}'
field_filter = f'`{field}` == {value}'

row_filter.append(field_filter)

Expand Down
66 changes: 66 additions & 0 deletions tests/unit/single_table/test_new_row_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,72 @@ def test_compute_breakdown_multi_line(self):
# Assert
assert score == 0.5

def test_compute_breakdown_with_category_dtype(self):
"""Test ``compute_breakdown`` when a categorical column uses pandas category dtype.

Expect that string categories are matched instead of raising UndefinedVariableError.
"""
# Setup
real_data = pd.DataFrame({
'gender': pd.Series(['F', 'M', 'F'], dtype='category'),
})
synthetic_data = pd.DataFrame({
'gender': ['F', 'X'],
})
metadata = {
'tables': {
'table': {
'columns': {
'gender': {'sdtype': 'categorical'},
},
}
}
}
metric = NewRowSynthesis()

# Run
result = metric.compute_breakdown(real_data, synthetic_data, metadata)

# Assert
assert result == {
'score': 0.5,
'num_new_rows': 1,
'num_matched_rows': 1,
}

def test_compute_breakdown_with_string_dtype(self):
"""Test ``compute_breakdown`` when a categorical column uses pandas string dtype.

Expect that string values are matched instead of raising UndefinedVariableError.
"""
# Setup
real_data = pd.DataFrame({
'gender': pd.Series(['F', 'M', 'F'], dtype='string'),
})
synthetic_data = pd.DataFrame({
'gender': ['F', 'X'],
})
metadata = {
'tables': {
'table': {
'columns': {
'gender': {'sdtype': 'categorical'},
},
}
}
}
metric = NewRowSynthesis()

# Run
result = metric.compute_breakdown(real_data, synthetic_data, metadata)

# Assert
assert result == {
'score': 0.5,
'num_new_rows': 1,
'num_matched_rows': 1,
}

def test_compute_with_sample_size(self):
"""Test the ``compute`` method with a sample size.

Expand Down