From e4f56372b127321f39e7e73a179dc49eac9b952e Mon Sep 17 00:00:00 2001 From: Felipe Date: Wed, 18 Sep 2024 08:46:43 -0700 Subject: [PATCH] Fix validation --- sdv/_utils.py | 1 + tests/unit/test__utils.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/sdv/_utils.py b/sdv/_utils.py index 3ec466537..1e71da475 100644 --- a/sdv/_utils.py +++ b/sdv/_utils.py @@ -81,6 +81,7 @@ def _is_datetime_type(value): bool(_get_datetime_format([value])) or isinstance(value, pd.Timestamp) or isinstance(value, datetime) + or (isinstance(value, str) and pd.notna(pd.to_datetime(value, errors='coerce'))) ): return False diff --git a/tests/unit/test__utils.py b/tests/unit/test__utils.py index 1cbcf3416..47fad1c1e 100644 --- a/tests/unit/test__utils.py +++ b/tests/unit/test__utils.py @@ -240,6 +240,30 @@ def test__is_datetime_type_with_datetime_str(): assert is_datetime +def test__is_datetime_type_with_datetime_str_nanoseconds(): + """Test it for a datetime string with nanoseconds.""" + # Setup + value = '2011-10-15 20:11:03.498707' + + # Run + is_datetime = _is_datetime_type(value) + + # Assert + assert is_datetime + + +def test__is_datetime_type_with_str_int(): + """Test it for a string with an integer.""" + # Setup + value = '123' + + # Run + is_datetime = _is_datetime_type(value) + + # Assert + assert is_datetime is False + + def test__is_datetime_type_with_invalid_str(): """Test the ``_is_datetime_type`` function when an invalid string is passed.