Skip to content

Commit

Permalink
Merge branch 'main' of github.com:MarcoGorelli/narwhals into increase…
Browse files Browse the repository at this point in the history
…-coverage
  • Loading branch information
raisa committed Mar 20, 2024
2 parents 998ebe5 + dd38333 commit 2003ea3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion narwhals/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from narwhals.series import Series
from narwhals.translate import to_native

__version__ = "0.6.4"
__version__ = "0.6.5"

__all__ = [
"is_dataframe",
Expand Down
6 changes: 4 additions & 2 deletions narwhals/pandas_like/group_by.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ def agg(
raise ValueError(msg)
output_names.extend(expr._output_names)

if implementation == "pandas" and not os.environ.get("NARWHALS_FORCE_GENERIC"):
if implementation in ("pandas", "modin") and not os.environ.get(
"NARWHALS_FORCE_GENERIC"
):
return agg_pandas(
grouped,
exprs,
Expand Down Expand Up @@ -174,7 +176,7 @@ def agg_generic( # noqa: PLR0913
to_remove: list[int] = []
for i, expr in enumerate(exprs):
if is_simple_aggregation(expr):
dfs.append(evaluate_simple_aggregation(expr, grouped))
dfs.append(evaluate_simple_aggregation(expr, grouped, group_by_keys))
to_remove.append(i)
exprs = [expr for i, expr in enumerate(exprs) if i not in to_remove]

Expand Down
11 changes: 9 additions & 2 deletions narwhals/pandas_like/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def is_simple_aggregation(expr: PandasExpr) -> bool:
)


def evaluate_simple_aggregation(expr: PandasExpr, grouped: Any) -> Any:
def evaluate_simple_aggregation(expr: PandasExpr, grouped: Any, keys: list[str]) -> Any:
"""
Use fastpath for simple aggregations if possible.
Expand All @@ -232,7 +232,14 @@ def evaluate_simple_aggregation(expr: PandasExpr, grouped: Any) -> Any:
Returns naive DataFrame.
"""
if expr._depth == 0:
return grouped.size()["size"].rename(expr._output_names[0]) # type: ignore[index]
# e.g. agg(pl.len())
df = getattr(grouped, expr._function_name.replace("len", "size"))()
df = (
df.drop(columns=keys)
if len(df.shape) > 1
else df.reset_index(drop=True).to_frame("size")
)
return df.rename(columns={"size": expr._output_names[0]}) # type: ignore[index]
if expr._root_names is None or expr._output_names is None:
msg = "Expected expr to have root_names and output_names set, but they are None. Please report a bug."
raise AssertionError(msg)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "narwhals"
version = "0.6.4"
version = "0.6.5"
authors = [
{ name="Marco Gorelli", email="[email protected]" },
]
Expand Down

0 comments on commit 2003ea3

Please sign in to comment.