Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explicit type parameters to arguments of df.apply #953

Merged
merged 2 commits into from
Jun 29, 2024

Conversation

JanEricNitschke
Copy link
Contributor

@JanEricNitschke JanEricNitschke commented Jun 29, 2024

  • Closes #xxxx (Replace xxxx with the Github issue number)
  • Tests added: Please use assert_type() to assert the type of any return value

Adresses: #952

Currently just added the explicit Any annotations everywhere, moved ListLikeExceptSeriesAndStr from a TypeVar to a Union and verified that more tests pass pyright strict mode.

However i still have a confusion about the overloads with Mapping. Isnt that just any generic object?

Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dr-Irv Dr-Irv merged commit 63c4567 into pandas-dev:main Jun 29, 2024
13 checks passed
@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Jun 29, 2024

However i still have a confusion about the overloads with Mapping. Isnt that just any generic object?

I don't understand your question here.

@JanEricNitschke
Copy link
Contributor Author

Here for example:

    @overload
    def apply(
        self,
        f: Callable[..., ListLikeExceptSeriesAndStr | Series[Any] | Mapping[Any, Any]],
        axis: Axis = ...,
        raw: _bool = ...,
        args: Any = ...,
        *,
        result_type: Literal["expand"],
        **kwargs: Any,

If i do

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))

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

I just get a series where each entry is this dict object. The mappings there just made me feel like it should be something like Mapping[S1, S1] -> Series[S2] or somethinglike that, but it Mapping -> Series[Mapping]

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Jun 29, 2024

I just get a series where each entry is this dict object. The mappings there just made me feel like it should be something like Mapping[S1, S1] -> Series[S2] or somethinglike that, but it Mapping -> Series[Mapping]

You could do Mapping[Any, S1] and have it return Series[S1], but you'd need to then have to separate the overloads for Mapping from the existing ones, and also you'd have to add another overload of Mapping[Any, Any] -> Series[Any] as well.

The key idea in the stubs is that we define S1 to be the "regular" types of things that we see in Series, and where it is useful to make a differentiation. While it is possible to put any object into a Series, we didn't include object in S1. That might be a change to make for cases like these. You'd possibly end up with overlapping overloads that mypy won't be happy with, but we can just #type: ignore statements for that.

@JanEricNitschke JanEricNitschke deleted the fix-frame-apply branch June 29, 2024 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants