Skip to content

Type of "apply" is partially unknown #952

Open
@JanEricNitschke

Description

@JanEricNitschke

I am again interested in getting some pyright strict warnings fixed that i get due to a lack of type parameters. This time for df.apply. However this already has a lot of overloads and i do not yet fully understand all of them.

Firstly i do not quite understand this one:

   @overload
    def apply(
        self,
        f: Callable[..., Mapping],
        axis: AxisIndex = ...,
        raw: _bool = ...,
        result_type: None = ...,
        args=...,
        **kwargs,
    ) -> Series: ...

that would be something like

def returns_dict(x: pd.Series) -> dict[str, int]:
    return {"col4": 7, "col5": 8}

df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4], "col3": [5, 6]})
print(df.apply(returns_dict))

Which just gives me an object series.

col1    {'col4': 7, 'col5': 8}
col2    {'col4': 7, 'col5': 8}
col3    {'col4': 7, 'col5': 8}

is that really the intended thing here? In that case that one could just go from taking a Mapping -> Series to Mapping[Any, Any] -> Series[Any].

All the ones that just return a dataframe should probably also have Series[Any] (or the equivalent for other generics).

For all overloads that take a

ListLikeExceptSeriesAndStr = TypeVar(
    "ListLikeExceptSeriesAndStr", MutableSequence, np.ndarray, tuple, Index
)

and return a series i think one could probably try to parametrize that further and have the series again be Series[S1]. But bounds in a typevar cant be generic themselves.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions