Skip to content

Commit 9f34c0a

Browse files
committed
Fix multiple warnings
1 parent 934ba74 commit 9f34c0a

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

sdv/metadata/single_table.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,7 @@ def validate_data(self, data, sdtype_warnings=None):
12961296
A warning is being raised if ``datetime_format`` is missing from a column represented
12971297
as ``object`` in the dataframe and its sdtype is ``datetime``.
12981298
"""
1299+
_datetime_format_warning_flag = sdtype_warnings is not None
12991300
sdtype_warnings = sdtype_warnings if sdtype_warnings is not None else defaultdict(list)
13001301
if not isinstance(data, pd.DataFrame):
13011302
raise ValueError(f'Data must be a DataFrame, not a {type(data)}.')
@@ -1315,7 +1316,7 @@ def validate_data(self, data, sdtype_warnings=None):
13151316
errors += self._validate_column_data(data[column], sdtype_warnings)
13161317

13171318
errors += self._validate_primary_key(data)
1318-
if sdtype_warnings is not None and len(sdtype_warnings):
1319+
if (not _datetime_format_warning_flag) and len(sdtype_warnings):
13191320
df = pd.DataFrame(sdtype_warnings)
13201321
message = (
13211322
"No 'datetime_format' is present in the metadata for the following columns:\n"

sdv/multi_table/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ def _initialize_models(self):
9090
self._table_synthesizers[table_name] = self._synthesizer(
9191
metadata=metadata, **synthesizer_parameters
9292
)
93+
# Mark synthesizer as embedded in a multi-table setting
94+
# so it can suppres datetime_format warnings that are aggregated here
95+
self._table_synthesizers[table_name]._suppress_datetime_format_warning = True
9396
self._table_synthesizers[table_name]._data_processor.table_name = table_name
9497

9598
def _get_pbar_args(self, **kwargs):

sdv/single_table/base.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,18 @@ def validate(self, data):
561561
data (pandas.DataFrame):
562562
The data to validate.
563563
"""
564-
self._original_metadata.validate_data({self._table_name: data})
564+
# Suppress duplicate datetime_format warning only when this single-table synthesizer
565+
# is embedded inside a multi-table synthesizer
566+
if getattr(self, '_suppress_datetime_format_warning', False):
567+
with warnings.catch_warnings():
568+
warnings.filterwarnings(
569+
'ignore',
570+
message=r"No 'datetime_format' is present.*",
571+
category=UserWarning,
572+
)
573+
self._original_metadata.validate_data({self._table_name: data})
574+
else:
575+
self._original_metadata.validate_data({self._table_name: data})
565576
self._validate_transform_constraints(data, enforce_constraint_fitting=True)
566577

567578
# Retaining the logic of returning errors and raising them here to maintain consistency

sdv/utils/utils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import datetime
44
import sys
5+
import warnings
56
from copy import deepcopy
67

78
import cloudpickle
@@ -55,7 +56,14 @@ def drop_unknown_references(data, metadata, drop_missing_values=False, verbose=T
5556
})
5657
metadata.validate()
5758
try:
58-
metadata.validate_data(data)
59+
# Suppress duplicate datetime_format warnings during referential integrity validation.
60+
with warnings.catch_warnings():
61+
warnings.filterwarnings(
62+
'ignore',
63+
message=r"No 'datetime_format' is present.*",
64+
category=UserWarning,
65+
)
66+
metadata.validate_data(data)
5967
if drop_missing_values:
6068
_validate_foreign_keys_not_null(metadata, data)
6169

0 commit comments

Comments
 (0)