-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7d9900
commit ffdf10e
Showing
17 changed files
with
498 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
title: Now | ||
order: 0 | ||
status: published | ||
--- | ||
### Now | ||
|
||
Function to get current timestamp, similar to what `datetime.now` does in Python. | ||
|
||
<pre snippet="api-reference/expressions/basic#expr_now" | ||
message="Using now to get age of a person" status="success"> | ||
</pre> | ||
|
||
#### Returns | ||
<Expandable type="Any"> | ||
Returns an expression object denoting a reference to the column. The type of | ||
the resulting expression is datetime. | ||
</Expandable> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
from datetime import datetime, timezone, timedelta | ||
from typing import Optional | ||
|
||
import pandas as pd | ||
import pytest | ||
|
||
from fennel._vendor import requests | ||
from fennel.connectors import source, Webhook | ||
from fennel.datasets import dataset, field | ||
from fennel.expr import col | ||
from fennel.featuresets import featureset, feature as F | ||
from fennel.testing import mock | ||
|
||
webhook = Webhook(name="fennel_webhook") | ||
__owner__ = "[email protected]" | ||
|
||
|
||
@source(webhook.endpoint("UserInfoDataset"), disorder="14d", cdc="upsert") | ||
@dataset(index=True) | ||
class UserInfoDataset: | ||
user_id: int = field(key=True) | ||
name: str | ||
birthdate: datetime | ||
country: str | ||
ts: datetime = field(timestamp=True) | ||
|
||
|
||
@pytest.mark.integration | ||
@mock | ||
def test_now(client): | ||
from fennel.expr import now | ||
|
||
@featureset | ||
class UserInfoFeatures: | ||
user_id: int | ||
name: Optional[str] = F(UserInfoDataset.name) | ||
birthdate: Optional[datetime] = F(UserInfoDataset.birthdate) | ||
age: Optional[int] = F(now().dt.since(col("birthdate"), unit="year")) | ||
country: Optional[str] = F(UserInfoDataset.country) | ||
|
||
# Sync the dataset | ||
response = client.commit( | ||
message="msg", | ||
datasets=[UserInfoDataset], | ||
featuresets=[UserInfoFeatures], | ||
) | ||
assert response.status_code == requests.codes.OK, response.json() | ||
|
||
now = datetime.now(timezone.utc) | ||
now_1y = now - timedelta(days=365) | ||
df = pd.DataFrame( | ||
{ | ||
"user_id": [1, 2, 3, 4, 5], | ||
"name": ["Ross", "Monica", "Chandler", "Joey", "Rachel"], | ||
"birthdate": [ | ||
datetime(1970, 1, 1, tzinfo=timezone.utc), | ||
datetime(1980, 3, 12, tzinfo=timezone.utc), | ||
datetime(1990, 5, 15, tzinfo=timezone.utc), | ||
datetime(1997, 12, 24, tzinfo=timezone.utc), | ||
datetime(2001, 1, 21, tzinfo=timezone.utc), | ||
], | ||
"country": ["India", "USA", "Africa", "UK", "Chile"], | ||
"ts": [now_1y, now_1y, now_1y, now_1y, now_1y], | ||
} | ||
) | ||
response = client.log("fennel_webhook", "UserInfoDataset", df) | ||
assert response.status_code == requests.codes.OK, response.json() | ||
|
||
client.sleep() | ||
|
||
# Querying UserInfoFeatures | ||
df = client.query( | ||
inputs=[UserInfoFeatures.user_id], | ||
outputs=[ | ||
UserInfoFeatures.name, | ||
UserInfoFeatures.age, | ||
UserInfoFeatures.country, | ||
], | ||
input_dataframe=pd.DataFrame( | ||
{"UserInfoFeatures.user_id": [1, 2, 3, 4, 5, 6]} | ||
), | ||
) | ||
assert df.shape == (6, 3) | ||
assert df["UserInfoFeatures.name"].tolist() == [ | ||
"Ross", | ||
"Monica", | ||
"Chandler", | ||
"Joey", | ||
"Rachel", | ||
pd.NA, | ||
] | ||
assert df["UserInfoFeatures.age"].tolist() == [54, 44, 34, 26, 23, pd.NA] | ||
assert df["UserInfoFeatures.country"].tolist() == [ | ||
"India", | ||
"USA", | ||
"Africa", | ||
"UK", | ||
"Chile", | ||
pd.NA, | ||
] | ||
|
||
if not client.is_integration_client(): | ||
df = client.query_offline( | ||
inputs=[UserInfoFeatures.user_id], | ||
outputs=[ | ||
UserInfoFeatures.name, | ||
UserInfoFeatures.age, | ||
UserInfoFeatures.country, | ||
], | ||
input_dataframe=pd.DataFrame( | ||
{ | ||
"UserInfoFeatures.user_id": [1, 2, 3, 4, 5, 6], | ||
"timestamp": [ | ||
now_1y, | ||
now_1y, | ||
now_1y, | ||
now_1y, | ||
now_1y, | ||
now_1y, | ||
], | ||
} | ||
), | ||
timestamp_column="timestamp", | ||
) | ||
assert df.shape == (6, 4) | ||
assert df["UserInfoFeatures.name"].tolist() == [ | ||
"Ross", | ||
"Monica", | ||
"Chandler", | ||
"Joey", | ||
"Rachel", | ||
pd.NA, | ||
] | ||
assert df["UserInfoFeatures.age"].tolist() == [ | ||
53, | ||
43, | ||
33, | ||
25, | ||
22, | ||
pd.NA, | ||
] | ||
assert df["UserInfoFeatures.country"].tolist() == [ | ||
"India", | ||
"USA", | ||
"Africa", | ||
"UK", | ||
"Chile", | ||
pd.NA, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
var, | ||
datetime, | ||
from_epoch, | ||
now, | ||
Expr, | ||
InvalidExprException, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.