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

Wrong typing for the values argument of the pandas.DataFrame.pivot_table method #885

Closed
airatk opened this issue Mar 4, 2024 · 5 comments · Fixed by #893
Closed

Wrong typing for the values argument of the pandas.DataFrame.pivot_table method #885

airatk opened this issue Mar 4, 2024 · 5 comments · Fixed by #893
Assignees
Labels
good first issue Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@airatk
Copy link

airatk commented Mar 4, 2024

Describe the bug
The values argument of the pandas.DataFrame.pivot_table method has type of str | None:

def pivot_table(
self,
values: _str | None = ...,
index: _str | Grouper | Sequence | None = ...,
columns: _str | Grouper | Sequence | None = ...,
aggfunc=...,
fill_value: Scalar | None = ...,
margins: _bool = ...,
dropna: _bool = ...,
margins_name: _str = ...,
observed: _bool = ...,
) -> DataFrame: ...

But the Pandas documentation says that values may have not only a string value, but also a list-like value:
Docs of pandas.DataFrame.pivot_table

To Reproduce

  1. A minimal runnable pandas example that is not properly checked by the stubs:
from pandas import DataFrame

data_frame = DataFrame({
    'a': [1, 2],
    'b': [1, 2],
    'c': [1, 2],
    'd': [1, 2],
})
data_frame.pivot_table(
    values=['a', 'b'],
    index=['c', 'd'],
)
  1. I am using mypy.
  2. The error message received from mypy:
error: Argument "values" to "pivot_table" of "DataFrame" has incompatible type "list[str]"; expected "str | None"  [arg-type]
Found 1 error in 1 file (checked 80 source files)

Please complete the following information:

  • OS: Ubuntu
  • OS version: 23.10
  • python version: 3.10.11
  • mypy version: 1.8.0
  • pandas-stubs version: 2.2.0.240218
@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Mar 4, 2024

Thanks for the report. As you pointed out, it's a simple fix to add a Sequence[_str] to the values argument in the stub

PR with test welcome

@Dr-Irv Dr-Irv added Reshaping Concat, Merge/Join, Stack/Unstack, Explode good first issue labels Mar 4, 2024
@Aftabby
Copy link
Contributor

Aftabby commented Mar 17, 2024

Hi @Dr-Irv ,

I noticed that this issue is currently unassigned, and I'm interested in working on it. Could you please assign it to me? I believe I can contribute to fix the problem (as per you mentioned).

Thanks.

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Mar 18, 2024

I noticed that this issue is currently unassigned, and I'm interested in working on it. Could you please assign it to me? I believe I can contribute to fix the problem (as per you mentioned).

Assigned to you. Be sure to update the stub and include a test

@Aftabby
Copy link
Contributor

Aftabby commented Mar 19, 2024

I noticed that this issue is currently unassigned, and I'm interested in working on it. Could you please assign it to me? I believe I can contribute to fix the problem (as per you mentioned).

Assigned to you. Be sure to update the stub and include a test

It seems there is already a test exists regarding this issue #885

    check(
        assert_type(
            df.pivot(index="col1", columns="col3", values=["col2", "col4"]),
            pd.DataFrame,
        ),
        pd.DataFrame,
    )

Should I include another one of the same kind?

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Mar 20, 2024

Should I include another one of the same kind?

Possibly, but the check you included tests DataFrame.pivot(), and for this issue, it needs to be a check on DataFrame.pivot_table()

Dr-Irv pushed a commit that referenced this issue Mar 21, 2024
* #885 Added Sequence[_str] to the values argument in the stub

* Added the test case test_types_pivot_table() for the changed TYP of values argument in pivot_table

* Changed the argument values for the test case test_types_pivot_table

* Fixed formatting with black
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants