Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] Support preprocessing datetime.date date features #3534

Merged
merged 3 commits into from
Aug 16, 2023
Merged
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
4 changes: 3 additions & 1 deletion ludwig/features/date_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.
# ==============================================================================
import logging
from datetime import datetime
from datetime import date, datetime
from typing import Dict, List

import numpy as np
Expand Down Expand Up @@ -66,6 +66,8 @@ def date_to_list(date_value, datetime_format, preprocessing_parameters):
try:
if isinstance(date_value, datetime):
datetime_obj = date_value
elif isinstance(date_value, date):
datetime_obj = datetime.combine(date=date_value, time=datetime.min.time())
elif isinstance(date_value, str) and datetime_format is not None:
try:
datetime_obj = datetime.strptime(date_value, datetime_format)
Expand Down
25 changes: 24 additions & 1 deletion tests/ludwig/features/test_date_feature.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from copy import deepcopy
from datetime import datetime
from datetime import date, datetime
from typing import Any, List

import pytest
Expand Down Expand Up @@ -157,3 +157,26 @@ def test_date_to_list__UsesFillValueOnInvalidDate():
0,
0,
]


@pytest.fixture(scope="module")
def date_obj():
return date.fromisoformat("2022-06-25")


@pytest.fixture(scope="module")
def date_obj_vec():
return create_vector_from_datetime_obj(datetime.fromisoformat("2022-06-25"))


def test_date_object_to_list(date_obj, date_obj_vec, fill_value):
"""Test support for datetime.date object conversion.

Args:
date_obj: Date object to convert into a vector
date_obj_vector: Expected vector version of `date_obj`
"""
computed_date_vec = date_feature.DateInputFeature.date_to_list(
date_obj, None, preprocessing_parameters={MISSING_VALUE_STRATEGY: FILL_WITH_CONST, "fill_value": fill_value}
)
assert computed_date_vec == date_obj_vec
Loading