Skip to content

GroupBy.pipe() missing types in arguments and correct return type #738

Closed
@Dr-Irv

Description

@Dr-Irv

In core/generic.pyi, we have for pipe():

def pipe(
        self, func: Callable[..., T] | tuple[Callable[..., T], str], *args, **kwargs
    ) -> T: ...

But in core/groupby/groupby.pyi, we have:

    def pipe(self, func: Callable, *args, **kwargs): ...

So we should investigate the proper definition for the groupby() version to determine the return type.

Sample code to illustrate issue:

from typing import Union, cast
from typing_extensions import reveal_type
import pandas as pd
from pandas.core.groupby.generic import DataFrameGroupBy

df = pd.DataFrame({"x": ["a", "b", "b", "a", "a", "c"], "y": [1, 2, 3, 4, 5, 6]})
df.index = pd.Index(df["y"] * 10)


def foo(g: Union[pd.DataFrame, DataFrameGroupBy]) -> pd.Series:
    return cast(pd.Series, g.sum())


reveal_type(df.pipe(foo))

reveal_type(df.groupby("x").pipe(foo))

Runtime reveals:

Runtime type is 'Series'
Runtime type is 'DataFrame'

pyright output:

gpipe.py:14:13 - information: Type of "df.pipe(foo)" is "Series[Unknown]"
gpipe.py:16:13 - information: Type of "df.groupby("x").pipe(foo)" is "Unknown"

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions