Skip to content

Commit

Permalink
Update _is_datetime_type to handle nanoseconds (#2231)
Browse files Browse the repository at this point in the history
  • Loading branch information
fealho authored Sep 20, 2024
1 parent b405342 commit 3dfaf36
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions sdv/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 24 additions & 0 deletions tests/unit/test__utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 3dfaf36

Please sign in to comment.