From 6189b5c54f4bdc0955a6712e046e130953b601ed Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Sun, 2 Mar 2025 11:15:09 +0000 Subject: [PATCH 1/2] type transpose --- pandas-stubs/core/arrays/sparse/array.pyi | 5 ++++- pandas-stubs/core/base.pyi | 2 +- pandas-stubs/core/frame.pyi | 2 +- pandas-stubs/core/series.pyi | 2 +- tests/test_frame.py | 7 +++++++ tests/test_indexes.py | 7 +++++++ tests/test_series.py | 7 +++++++ 7 files changed, 28 insertions(+), 4 deletions(-) diff --git a/pandas-stubs/core/arrays/sparse/array.pyi b/pandas-stubs/core/arrays/sparse/array.pyi index cff7b8a20..db1b479c7 100644 --- a/pandas-stubs/core/arrays/sparse/array.pyi +++ b/pandas-stubs/core/arrays/sparse/array.pyi @@ -1,8 +1,11 @@ +from typing import Any + import numpy as np from pandas.core.arrays import ( ExtensionArray, ExtensionOpsMixin, ) +from typing_extensions import Self class SparseArray(ExtensionArray, ExtensionOpsMixin): def __init__( @@ -53,7 +56,7 @@ class SparseArray(ExtensionArray, ExtensionOpsMixin): def sum(self, axis: int = ..., *args, **kwargs): ... def cumsum(self, axis: int = ..., *args, **kwargs): ... def mean(self, axis: int = ..., *args, **kwargs): ... - def transpose(self, *axes): ... + def transpose(self, *axes: Any) -> Self: ... @property def T(self): ... def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): ... diff --git a/pandas-stubs/core/base.pyi b/pandas-stubs/core/base.pyi index 1cd752528..d7e5a63f3 100644 --- a/pandas-stubs/core/base.pyi +++ b/pandas-stubs/core/base.pyi @@ -44,7 +44,7 @@ class SelectionMixin(Generic[NDFrameT]): class IndexOpsMixin(OpsMixin, Generic[S1]): __array_priority__: int = ... - def transpose(self, *args, **kwargs) -> Self: ... + def transpose(self, axes: None = ...) -> Self: ... @property def T(self) -> Self: ... @property diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index 344dd3a39..a11f4f8a8 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -656,7 +656,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): show_counts: bool | None = ..., ) -> None: ... def memory_usage(self, index: _bool = ..., deep: _bool = ...) -> Series: ... - def transpose(self, *args, copy: _bool = ...) -> Self: ... + def transpose(self, axes: None = ..., /, copy: _bool = ...) -> Self: ... @property def T(self) -> Self: ... def __getattr__(self, name: str) -> Series: ... diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 2ade583c7..5c10d5903 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -1539,7 +1539,7 @@ class Series(IndexOpsMixin[S1], NDFrame): bins: int | None = ..., dropna: _bool = ..., ) -> Series[float]: ... - def transpose(self, *args, **kwargs) -> Series[S1]: ... + def transpose(self, axes: None = ...) -> Series[S1]: ... @property def T(self) -> Self: ... # The rest of these were left over from the old diff --git a/tests/test_frame.py b/tests/test_frame.py index e506528e3..a5d524bc7 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -4000,3 +4000,10 @@ def test_hashable_args() -> None: # GH 906 @pd.api.extensions.register_dataframe_accessor("geo") class GeoAccessor: ... + + +def test_transpose() -> None: + df = pd.DataFrame({"a": [1, 1, 2], "b": [4, 5, 6]}) + check(assert_type(df.transpose(), pd.DataFrame), pd.DataFrame) + check(assert_type(df.transpose(None), pd.DataFrame), pd.DataFrame) + check(assert_type(df.transpose(copy=True), pd.DataFrame), pd.DataFrame) diff --git a/tests/test_indexes.py b/tests/test_indexes.py index bb54d11d0..3c6f14f5c 100644 --- a/tests/test_indexes.py +++ b/tests/test_indexes.py @@ -1272,3 +1272,10 @@ def test_datetime_index_max_min_reductions() -> None: check(assert_type(dtidx.argmin(), np.int64), np.int64) check(assert_type(dtidx.max(), pd.Timestamp), pd.Timestamp) check(assert_type(dtidx.min(), pd.Timestamp), pd.Timestamp) + + +def test_transpose() -> None: + idx: pd.Index[int] = pd.Index([1, 1, 2]) + check(assert_type(idx.transpose(), "pd.Index[int]"), pd.Index, np.int64) + check(assert_type(idx.transpose(None), "pd.Index[int]"), pd.Index, np.int64) + check(assert_type(idx.transpose(axes=None), "pd.Index[int]"), pd.Index, np.int64) diff --git a/tests/test_series.py b/tests/test_series.py index 0c700fcbd..35b2a918b 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -3672,3 +3672,10 @@ def test_info() -> None: check(assert_type(s.info(show_counts=True), None), type(None)) check(assert_type(s.info(show_counts=False), None), type(None)) check(assert_type(s.info(show_counts=None), None), type(None)) + + +def test_transpose() -> None: + s: pd.Series[int] = pd.Series([1, 1, 2]) + check(assert_type(s.transpose(), "pd.Series[int]"), pd.Series, np.int64) + check(assert_type(s.transpose(None), "pd.Series[int]"), pd.Series, np.int64) + check(assert_type(s.transpose(axes=None), "pd.Series[int]"), pd.Series, np.int64) From 7572894b26073f5ca7b7018e1ec732899dc3fbdd Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Tue, 4 Mar 2025 10:20:07 +0000 Subject: [PATCH 2/2] remove undocumented methods, use *args: Any instead of undocumented axes --- pandas-stubs/core/arrays/sparse/array.pyi | 4 ---- pandas-stubs/core/base.pyi | 1 - pandas-stubs/core/frame.pyi | 2 +- pandas-stubs/core/series.pyi | 1 - 4 files changed, 1 insertion(+), 7 deletions(-) diff --git a/pandas-stubs/core/arrays/sparse/array.pyi b/pandas-stubs/core/arrays/sparse/array.pyi index db1b479c7..71b861606 100644 --- a/pandas-stubs/core/arrays/sparse/array.pyi +++ b/pandas-stubs/core/arrays/sparse/array.pyi @@ -1,11 +1,8 @@ -from typing import Any - import numpy as np from pandas.core.arrays import ( ExtensionArray, ExtensionOpsMixin, ) -from typing_extensions import Self class SparseArray(ExtensionArray, ExtensionOpsMixin): def __init__( @@ -56,7 +53,6 @@ class SparseArray(ExtensionArray, ExtensionOpsMixin): def sum(self, axis: int = ..., *args, **kwargs): ... def cumsum(self, axis: int = ..., *args, **kwargs): ... def mean(self, axis: int = ..., *args, **kwargs): ... - def transpose(self, *axes: Any) -> Self: ... @property def T(self): ... def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): ... diff --git a/pandas-stubs/core/base.pyi b/pandas-stubs/core/base.pyi index d7e5a63f3..1eb65d784 100644 --- a/pandas-stubs/core/base.pyi +++ b/pandas-stubs/core/base.pyi @@ -44,7 +44,6 @@ class SelectionMixin(Generic[NDFrameT]): class IndexOpsMixin(OpsMixin, Generic[S1]): __array_priority__: int = ... - def transpose(self, axes: None = ...) -> Self: ... @property def T(self) -> Self: ... @property diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index a11f4f8a8..e58521142 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -656,7 +656,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): show_counts: bool | None = ..., ) -> None: ... def memory_usage(self, index: _bool = ..., deep: _bool = ...) -> Series: ... - def transpose(self, axes: None = ..., /, copy: _bool = ...) -> Self: ... + def transpose(self, *args: Any, copy: _bool = ...) -> Self: ... @property def T(self) -> Self: ... def __getattr__(self, name: str) -> Series: ... diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 5c10d5903..12c939b10 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -1539,7 +1539,6 @@ class Series(IndexOpsMixin[S1], NDFrame): bins: int | None = ..., dropna: _bool = ..., ) -> Series[float]: ... - def transpose(self, axes: None = ...) -> Series[S1]: ... @property def T(self) -> Self: ... # The rest of these were left over from the old