Skip to content

Commit

Permalink
fix: Retire the datetime.utcnow(). (#4352)
Browse files Browse the repository at this point in the history
* fix: Retire the datetime.utcnow().

Signed-off-by: Shuchu Han <shuchu.han@gmail.com>

* fix: Remove unnecessary unit test.

Signed-off-by: Shuchu Han <shuchu.han@gmail.com>

---------

Signed-off-by: Shuchu Han <shuchu.han@gmail.com>
  • Loading branch information
shuchu committed Jul 16, 2024
1 parent ce4f09b commit a8bc696
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 24 deletions.
43 changes: 31 additions & 12 deletions sdk/python/feast/driver_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ def create_orders_df(
df["order_is_success"] = np.random.randint(0, 2, size=order_count).astype(np.int32)
df[DEFAULT_ENTITY_DF_EVENT_TIMESTAMP_COL] = [
_convert_event_timestamp(
pd.Timestamp(dt, unit="ms", tz="UTC").round("ms"),
pd.Timestamp(dt, unit="ms").round("ms"),
EventTimestampType(idx % 4),
)
for idx, dt in enumerate(
pd.date_range(start=start_date, end=end_date, periods=order_count)
pd.date_range(start=start_date, end=end_date, periods=order_count, tz="UTC")
)
]
df.sort_values(
Expand Down Expand Up @@ -101,9 +101,13 @@ def create_driver_hourly_stats_df(drivers, start_date, end_date) -> pd.DataFrame
df_hourly = pd.DataFrame(
{
"event_timestamp": [
pd.Timestamp(dt, unit="ms", tz="UTC").round("ms")
pd.Timestamp(dt, unit="ms").round("ms")
for dt in pd.date_range(
start=start_date, end=end_date, freq="1h", inclusive="left"
start=start_date,
end=end_date,
freq="1h",
inclusive="left",
tz="UTC",
)
]
# include a fixed timestamp for get_historical_features in the quickstart
Expand Down Expand Up @@ -162,9 +166,13 @@ def create_customer_daily_profile_df(customers, start_date, end_date) -> pd.Data
df_daily = pd.DataFrame(
{
"event_timestamp": [
pd.Timestamp(dt, unit="ms", tz="UTC").round("ms")
pd.Timestamp(dt, unit="ms").round("ms")
for dt in pd.date_range(
start=start_date, end=end_date, freq="1D", inclusive="left"
start=start_date,
end=end_date,
freq="1D",
inclusive="left",
tz="UTC",
)
]
}
Expand Down Expand Up @@ -207,9 +215,13 @@ def create_location_stats_df(locations, start_date, end_date) -> pd.DataFrame:
df_hourly = pd.DataFrame(
{
"event_timestamp": [
pd.Timestamp(dt, unit="ms", tz="UTC").round("ms")
pd.Timestamp(dt, unit="ms").round("ms")
for dt in pd.date_range(
start=start_date, end=end_date, freq="1h", inclusive="left"
start=start_date,
end=end_date,
freq="1h",
inclusive="left",
tz="UTC",
)
]
}
Expand Down Expand Up @@ -254,9 +266,16 @@ def create_global_daily_stats_df(start_date, end_date) -> pd.DataFrame:
df_daily = pd.DataFrame(
{
"event_timestamp": [
pd.Timestamp(dt, unit="ms", tz="UTC").round("ms")
pd.Timestamp(
dt,
unit="ms",
).round("ms")
for dt in pd.date_range(
start=start_date, end=end_date, freq="1D", inclusive="left"
start=start_date,
end=end_date,
freq="1D",
inclusive="left",
tz="UTC",
)
]
}
Expand Down Expand Up @@ -286,11 +305,11 @@ def create_field_mapping_df(start_date, end_date) -> pd.DataFrame:
df["column_name"] = np.random.randint(1, 100, size=size).astype(np.int32)
df[DEFAULT_ENTITY_DF_EVENT_TIMESTAMP_COL] = [
_convert_event_timestamp(
pd.Timestamp(dt, unit="ms", tz="UTC").round("ms"),
pd.Timestamp(dt, unit="ms").round("ms"),
EventTimestampType(idx % 4),
)
for idx, dt in enumerate(
pd.date_range(start=start_date, end=end_date, periods=size)
pd.date_range(start=start_date, end=end_date, periods=size, tz="UTC")
)
]
df["created"] = pd.to_datetime(pd.Timestamp.now(tz=None).round("ms"))
Expand Down
3 changes: 2 additions & 1 deletion sdk/python/feast/type_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ def python_type_to_feast_value_type(
"timestamp": ValueType.UNIX_TIMESTAMP,
"datetime": ValueType.UNIX_TIMESTAMP,
"datetime64[ns]": ValueType.UNIX_TIMESTAMP,
"datetime64[ns, tz]": ValueType.UNIX_TIMESTAMP,
"datetime64[ns, tz]": ValueType.UNIX_TIMESTAMP, # special dtype of pandas
"datetime64[ns, utc]": ValueType.UNIX_TIMESTAMP,
"category": ValueType.STRING,
}

Expand Down
4 changes: 2 additions & 2 deletions sdk/python/feast/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import typing
import warnings
from collections import Counter, defaultdict
from datetime import datetime
from datetime import datetime, timezone
from pathlib import Path
from typing import (
Any,
Expand Down Expand Up @@ -1055,4 +1055,4 @@ def tags_str_to_dict(tags: str = "") -> dict[str, str]:


def _utc_now() -> datetime:
return datetime.utcnow()
return datetime.now(tz=timezone.utc)
6 changes: 0 additions & 6 deletions sdk/python/tests/unit/test_datetime.py

This file was deleted.

4 changes: 2 additions & 2 deletions sdk/python/tests/utils/feature_records.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from typing import Any, Dict, List, Optional

import numpy as np
Expand Down Expand Up @@ -520,7 +520,7 @@ def get_last_feature_row(df: pd.DataFrame, driver_id, max_date: datetime):
"""Manually extract last feature value from a dataframe for a given driver_id with up to `max_date` date"""
filtered = df[
(df["driver_id"] == driver_id)
& (df["event_timestamp"] < max_date.replace(tzinfo=utc))
& (df["event_timestamp"] < max_date.replace(tzinfo=timezone.utc))
]
max_ts = filtered.loc[filtered["event_timestamp"].idxmax()]["event_timestamp"]
filtered_by_ts = filtered[filtered["event_timestamp"] == max_ts]
Expand Down
2 changes: 1 addition & 1 deletion sdk/python/tests/utils/test_log_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def prepare_logs(
f"{destination_field}__status"
].mask(
logs_df[f"{destination_field}__timestamp"]
< (_utc_now() - view.ttl),
< (_utc_now() - view.ttl).replace(tzinfo=None),
FieldStatus.OUTSIDE_MAX_AGE,
)

Expand Down

0 comments on commit a8bc696

Please sign in to comment.