Repository: pandas-dev/pandas-stubs (language: python)

Issue:
Wrong typing for the `values` argument of the `pandas.DataFrame.pivot_table` method
**Describe the bug**
The `values` argument of the `pandas.DataFrame.pivot_table` method has type of `str | None`:
https://github.com/pandas-dev/pandas-stubs/blob/7dd7b6a037819729da84a94bcda4d2f731e43bf6/pandas-stubs/core/frame.pyi#L1126-L1137

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`](https://pandas.pydata.org/pandas-docs/version/2.0/reference/api/pandas.DataFrame.pivot_table.html#pandas-dataframe-pivot-table)

**To Reproduce**
1. A minimal runnable `pandas` example that is not properly checked by the stubs:
```python
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'],
)
```

3. I am using `mypy`.
4. 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

Wrong typing for the `values` argument of the `pandas.DataFrame.pivot_table` method
**Describe the bug**
The `values` argument of the `pandas.DataFrame.pivot_table` method has type of `str | None`:
https://github.com/pandas-dev/pandas-stubs/blob/7dd7b6a037819729da84a94bcda4d2f731e43bf6/pandas-stubs/core/frame.pyi#L1126-L1137

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`](https://pandas.pydata.org/pandas-docs/version/2.0/reference/api/pandas.DataFrame.pivot_table.html#pandas-dataframe-pivot-table)

**To Reproduce**
1. A minimal runnable `pandas` example that is not properly checked by the stubs:
```python
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'],
)
```

3. I am using `mypy`.
4. 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



Discussion / hints:
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
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.
> 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
> > 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? 
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
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.
> 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
> > 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? 

Fix patch:
diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi
index fe3fef2f..7ce2f5d6 100644
--- a/pandas-stubs/core/frame.pyi
+++ b/pandas-stubs/core/frame.pyi
@@ -1125,7 +1125,7 @@ class DataFrame(NDFrame, OpsMixin):
     ) -> DataFrame: ...
     def pivot_table(
         self,
-        values: _str | None = ...,
+        values: _str | None | Sequence[_str] = ...,
         index: _str | Grouper | Sequence | None = ...,
         columns: _str | Grouper | Sequence | None = ...,
         aggfunc=...,


Test patch:
diff --git a/tests/test_frame.py b/tests/test_frame.py
index 0eab8191..af3183b9 100644
--- a/tests/test_frame.py
+++ b/tests/test_frame.py
@@ -932,6 +932,24 @@ def test_types_pivot() -> None:
     )
 
 
+def test_types_pivot_table() -> None:
+    df = pd.DataFrame(
+        data={
+            "col1": ["first", "second", "third", "fourth"],
+            "col2": [50, 70, 56, 111],
+            "col3": ["A", "B", "C", "D"],
+            "col4": [100, 102, 500, 600],
+        }
+    )
+    check(
+        assert_type(
+            df.pivot_table(index="col1", columns="col3", values=["col2", "col4"]),
+            pd.DataFrame,
+        ),
+        pd.DataFrame,
+    )
+
+
 def test_types_groupby() -> None:
     df = pd.DataFrame(data={"col1": [1, 1, 2], "col2": [3, 4, 5], "col3": [0, 1, 0]})
     df.index.name = "ind"