Skip to content
Open
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: 2 additions & 2 deletions sdk/python/feast/aggregation/tiling/tile_subtraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,6 @@ def deduplicate_keep_latest(

return (
df.sort_values(by=timestamp_col, ascending=False)
.groupby(entity_keys, as_index=False)
.first()
.drop_duplicates(subset=entity_keys, keep="first")
.reset_index(drop=True)
)
27 changes: 27 additions & 0 deletions sdk/python/tests/unit/test_aggregation_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,30 @@ def windowed(frame):
assert len(expected) == 8
assert result["event_timestamp"].tolist() == expected["event_timestamp"].tolist()
assert result[feature_name].tolist() == expected[feature_name].tolist()


def test_deduplicate_keep_latest_keeps_null_values_of_latest_row():
"""A null in the latest row is kept rather than filled from an older row."""
from feast.aggregation.tiling.tile_subtraction import deduplicate_keep_latest

start = pd.Timestamp("2024-01-01")
df = pd.DataFrame(
{
"driver_id": [1, 1, 2],
"event_timestamp": [
start,
start + timedelta(minutes=5),
start,
],
# std over a window with a single event is NaN
"std_trips_1200s": [2.5, float("nan"), 1.0],
}
)

result = deduplicate_keep_latest(df, ["driver_id"], "event_timestamp")
result = result.set_index("driver_id")

assert len(result) == 2
assert result.loc[1, "event_timestamp"] == start + timedelta(minutes=5)
assert pd.isna(result.loc[1, "std_trips_1200s"])
assert result.loc[2, "std_trips_1200s"] == 1.0