Skip to content

Extend Scalar sub-type of Index to the __iter__() method #367

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 1 commit into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 5 additions & 6 deletions pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ from typing import (
Mapping,
Pattern,
Sequence,
TypeVar,
overload,
)

Expand Down Expand Up @@ -115,6 +116,7 @@ from pandas.plotting import PlotAccessor

_str = str
_bool = bool
_ScalarOrTupleT = TypeVar("_ScalarOrTupleT", bound=Scalar | tuple[Hashable, ...])

class _iLocIndexerFrame(_iLocIndexer):
@overload
Expand Down Expand Up @@ -491,21 +493,18 @@ class DataFrame(NDFrame, OpsMixin):
def T(self) -> DataFrame: ...
def __getattr__(self, name: str) -> Series: ...
@overload
def __getitem__(self, idx: Scalar) -> Series: ...
def __getitem__(self, idx: Scalar | tuple[Hashable, ...]) -> Series: ...
@overload
def __getitem__(self, rows: slice) -> DataFrame: ...
@overload
def __getitem__(
self,
idx: tuple
| Series[_bool]
idx: Series[_bool]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

x = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=["a", "b", "c"])

   0  1  2
a  1  2  3
b  4  5  6
c  7  8  9

x[[0, 1]] works

x[(0, 1)] does not, because that is interpreted as a column label

| DataFrame
| list[_str]
| list[ScalarT]
| Index
| np_ndarray_str
| np_ndarray_bool
| Sequence[tuple[Scalar, ...]],
| list[_ScalarOrTupleT],
) -> DataFrame: ...
def isetitem(
self, loc: int | Sequence[int], value: Scalar | ArrayLike | list[Any]
Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class Index(IndexOpsMixin, PandasObject):
def shape(self) -> tuple[int, ...]: ...
# Extra methods from old stubs
def __eq__(self, other: object) -> np_ndarray_bool: ... # type: ignore[override]
def __iter__(self) -> Iterator: ...
def __iter__(self) -> Iterator[Scalar | tuple[Hashable, ...]]: ...
def __ne__(self, other: object) -> np_ndarray_bool: ... # type: ignore[override]
def __le__(self, other: Index | Scalar) -> np_ndarray_bool: ...
def __ge__(self, other: Index | Scalar) -> np_ndarray_bool: ...
Expand Down
4 changes: 3 additions & 1 deletion tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1465,7 +1465,9 @@ def test_getmultiindex_columns() -> None:
res3: pd.DataFrame = df[
[(i, s) for i in [1] for s in df.columns.get_level_values(1)]
]
ndf: pd.DataFrame = df[[df.columns[0]]]
res4: pd.DataFrame = df[[df.columns[0]]]
check(assert_type(df[df.columns[0]], pd.Series), pd.Series)
check(assert_type(df[li[0]], pd.Series), pd.Series)


def test_frame_getitem_isin() -> None:
Expand Down
16 changes: 15 additions & 1 deletion tests/test_indexes.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
from __future__ import annotations

import datetime as dt
from typing import Union
from typing import (
Hashable,
List,
Tuple,
Union,
)

import numpy as np
from numpy import typing as npt
import pandas as pd
from pandas.core.indexes.numeric import NumericIndex
from typing_extensions import assert_type

from pandas._typing import Scalar

from tests import (
check,
pytest_warns_bounded,
Expand Down Expand Up @@ -67,6 +74,7 @@ def test_column_getitem() -> None:
df = pd.DataFrame([[1, 2, 3]], columns=["a", "b", "c"])

column = df.columns[0]
check(assert_type(column, Scalar), str)
check(assert_type(df[column], pd.Series), pd.Series, int)


Expand All @@ -81,6 +89,12 @@ def test_column_contains() -> None:
length = len(df.columns[df.columns.str.contains("A|B")])


def test_column_sequence() -> None:
df = pd.DataFrame([1, 2, 3])
col_list = list(df.columns)
check(assert_type(col_list, List[Union[Scalar, Tuple[Hashable, ...]]]), list, int)


def test_difference_none() -> None:
# https://github.com/pandas-dev/pandas-stubs/issues/17
ind = pd.Index([1, 2, 3])
Expand Down