Skip to content

Commit 7b9fad2

Browse files
YHC66haoxu0
andauthored
fix: Use group_by in Polars backend aggregation (#6965)
polars removed DataFrame.groupby in 1.0 in favour of group_by, so PolarsBackend.groupby_agg raised AttributeError and any aggregation run by the local compute engine with backend=polars failed. Signed-off-by: Yihang Chen <[email protected]> Co-authored-by: Hao Xu <[email protected]>
1 parent 5e7ef3e commit 7b9fad2

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎sdk/python/feast/infra/compute_engines/backends/polars_backend.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def groupby_agg(self, df: pl.DataFrame, group_keys, agg_ops) -> pl.DataFrame:
2626
getattr(pl.col(col), self._POLARS_FUNC_MAP.get(func, func))().alias(alias)
2727
for alias, (func, col) in agg_ops.items()
2828
]
29-
return df.groupby(group_keys).agg(agg_exprs)
29+
return df.group_by(group_keys).agg(agg_exprs)
3030

3131
def filter(self, df: pl.DataFrame, expr: str) -> pl.DataFrame:
3232
return df.filter(pl.sql_expr(expr))

‎sdk/python/tests/unit/infra/compute_engines/local/test_nodes.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import pandas as pd
55
import pyarrow as pa
6+
import pytest
67

78
from feast.infra.compute_engines.backends.pandas_backend import PandasBackend
89
from feast.infra.compute_engines.dag.context import ColumnInfo, ExecutionContext
@@ -104,6 +105,34 @@ def test_local_aggregation_node():
104105
assert result_df["sum_value"].iloc[1] == 70
105106

106107

108+
def test_local_aggregation_node_polars_backend():
109+
pytest.importorskip("polars")
110+
from feast.infra.compute_engines.backends.polars_backend import PolarsBackend
111+
112+
context = create_context(
113+
node_outputs={"source": ArrowTableValue(pa.Table.from_pandas(sample_df))}
114+
)
115+
116+
agg_ops = {
117+
"sum_value": ("sum", "value"),
118+
"unique_value": ("nunique", "value"),
119+
}
120+
agg_node = LocalAggregationNode(
121+
name="agg",
122+
backend=PolarsBackend(),
123+
group_keys=["entity_id"],
124+
agg_ops=agg_ops,
125+
)
126+
agg_node.add_input(MagicMock())
127+
agg_node.inputs[0].name = "source"
128+
129+
result = agg_node.execute(context)
130+
assert isinstance(result, ArrowTableValue)
131+
result_df = result.data.to_pandas().sort_values("entity_id")
132+
assert result_df["sum_value"].tolist() == [30, 70]
133+
assert result_df["unique_value"].tolist() == [2, 2]
134+
135+
107136
def test_local_join_node():
108137
context = create_context(
109138
node_outputs={"source": ArrowTableValue(pa.Table.from_pandas(sample_df))}

0 commit comments

Comments
 (0)