Skip to content

type dataframe.transpose #1136

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

Merged
merged 5 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pandas-stubs/core/arrays/sparse/array.pyi
Original file line number Diff line number Diff line change
@@ -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__(
Expand Down Expand Up @@ -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: ...
Copy link
Member Author

Choose a reason for hiding this comment

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

axes is totally ignored in SparseArray.transpose

Copy link
Collaborator

Choose a reason for hiding this comment

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

We should delete this. SparseArray.transpose() is not documented. In fact, only the constructor is documented, so we should delete all the methods for SparseArray except __init__(). Can you do that here?

@property
def T(self): ...
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): ...
Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/core/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]: ...
Copy link
Member Author

Choose a reason for hiding this comment

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

The only argument that pandas actually lets us pass at runtime is axes=None

https://github.com/pandas-dev/pandas/blob/d831326e11971af61307e4fc940855556ef3cdb7/pandas/compat/numpy/function.py#L317-L320

For Series and Index, it can be passed positionally or by name

For DataFrame, it can only be passed positionally

@property
def T(self) -> Self: ...
# The rest of these were left over from the old
Expand Down
7 changes: 7 additions & 0 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
7 changes: 7 additions & 0 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
7 changes: 7 additions & 0 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)