Skip to content

Commit

Permalink
expr: Make it hashable (#540)
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya-nambiar authored Aug 27, 2024
1 parent 1a435d5 commit f559a9f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
3 changes: 2 additions & 1 deletion docs/examples/useful-tips/debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pandas as pd

from fennel.testing import mock
from fennel.expr import lit

__owner__ = "aditya@fennel.ai"

Expand Down Expand Up @@ -41,7 +42,7 @@ def my_pipeline(cls, user: Dataset):
schema = ds.schema()
print(schema)
# docsnip-highlight end
return ds.assign("country", str, lambda df: "US")
return ds.assign(country=lit("US").astype(str))

# /docsnip

Expand Down
10 changes: 9 additions & 1 deletion fennel/expr/test_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@
from typing import Dict
from fennel.datasets import dataset

from fennel.expr import col, when
from fennel.expr import col, when, lit
from fennel.expr.visitor import ExprPrinter, FetchReferences
from fennel.expr.serializer import ExprSerializer
from google.protobuf.json_format import ParseDict # type: ignore
from fennel.gen.expr_pb2 import Expr
from fennel.testing.test_utils import error_message


def test_const_expr():
expr = lit(1, int)
assert expr.typeof({}) == int
df = pd.DataFrame({"a": [1, 2, 3, 4]})
df2 = expr.eval(df, {"a": int})
assert df2.tolist() == [1, 1, 1, 1]


def test_basic_expr1():
expr = (col("num") + col("d")).isnull()
df = pd.DataFrame({"num": [1, 2, 3, 4], "d": [5, 6, 7, 8]})
Expand Down
1 change: 1 addition & 0 deletions fennel/testing/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@ def visitAssign(self, obj):
else:
input_df = copy.deepcopy(input_ret.df)
df = copy.deepcopy(input_df)
df.reset_index(drop=True, inplace=True)
for col, typed_expr in obj.output_expressions.items():
if col in input_ret.df.columns:
raise Exception(
Expand Down
8 changes: 7 additions & 1 deletion fennel/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import sys
import textwrap
from typing import Any, cast, Callable, Dict, List, Tuple, Union

from fennel.expr.expr import TypedExpr, Expr
from pandas import DataFrame

import fennel._vendor.astunparse as astunparse
Expand Down Expand Up @@ -163,6 +163,12 @@ def _json_default(item: Any):
if isinstance(item, datetime.timedelta):
return str(item.total_seconds())

if isinstance(item, TypedExpr):
return str(item.expr) + str(item.dtype)

if isinstance(item, Expr):
return str(item)

raise TypeError(f"object of type {type(item).__name__} not hashable")


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "fennel-ai"
version = "1.5.8"
version = "1.5.9"
description = "The modern realtime feature engineering platform"
authors = ["Fennel AI <developers@fennel.ai>"]
packages = [{ include = "fennel" }]
Expand Down

0 comments on commit f559a9f

Please sign in to comment.