Skip to content

Index slice #311

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
Sep 18, 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
14 changes: 9 additions & 5 deletions pandas-stubs/core/indexing.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing import (
Generic,
TypeVar,
Union,
)
Expand All @@ -9,14 +8,19 @@ from pandas.core.indexes.api import Index

from pandas._libs.indexing import _NDFrameIndexerBase
from pandas._typing import (
MaskType,
Scalar,
StrLike,
ScalarT,
)

_IndexSliceT = TypeVar("_IndexSliceT", bound=Union[StrLike, Scalar, slice])
_IndexSliceTuple = Union[
slice, tuple[Union[Index, MaskType, Scalar, list[ScalarT], slice], ...]
]

class _IndexSlice(Generic[_IndexSliceT]):
def __getitem__(self, arg) -> tuple[_IndexSliceT, ...]: ...
_IndexSliceTupleT = TypeVar("_IndexSliceTupleT", bound=_IndexSliceTuple)

class _IndexSlice:
def __getitem__(self, arg: _IndexSliceTupleT) -> _IndexSliceTupleT: ...
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't it always a tuple, even when slicing 1d? In my experiments I always say some soft of tuple whether slicing 1 level 2 or 3.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or is slice a subclass of tuple somehow?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

_IndexSliceTuple is either a slice or a tuple. So if you do pd.IndexSlice[1:2], that returns a slice not a tuple

Copy link
Member

Choose a reason for hiding this comment

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

I must admit I have never directly used pd.IndexSlice.__getitem__ - it seems to simply be an identity function.

>>> obj = object()
>>> pd.IndexSlice[obj] is obj
True

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If you are doing slicing on a MultiIndex), it really becomes necessary. See the docs at https://pandas.pydata.org/docs/reference/api/pandas.IndexSlice.html?highlight=indexslice#pandas.IndexSlice for a good example.


IndexSlice: _IndexSlice

Expand Down
22 changes: 22 additions & 0 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,28 @@ def test_indexslice_setitem():
df.loc[pd.IndexSlice[2, :], "z"] = [200, 300]


def test_indexslice_getitem():
# GH 300
df = (
pd.DataFrame({"x": [1, 2, 2, 3, 4], "y": [10, 20, 30, 40, 10]})
.assign(z=lambda df: df.x * df.y)
.set_index(["x", "y"])
)
ind = pd.Index([2, 3])
check(assert_type(pd.IndexSlice[ind, :], "tuple[pd.Index, slice]"), tuple)
check(assert_type(df.loc[pd.IndexSlice[ind, :]], pd.DataFrame), pd.DataFrame)
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally you would have a test that would show all possible types can be generated.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Some of them are tested elsewhere, but adding more tests is a good idea. Will work on that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I added a bunch of tests in the latest commit.

check(assert_type(df.loc[pd.IndexSlice[1:2]], pd.DataFrame), pd.DataFrame)
check(
assert_type(df.loc[pd.IndexSlice[:, df["z"] > 40], :], pd.DataFrame),
pd.DataFrame,
)
check(assert_type(df.loc[pd.IndexSlice[2, 30], "z"], Scalar), np.int64)
check(
assert_type(df.loc[pd.IndexSlice[[2, 4], [20, 40]], :], pd.DataFrame),
pd.DataFrame,
)


def test_compute_values():
df = pd.DataFrame({"x": [1, 2, 3, 4]})
s: pd.Series = pd.Series([10, 20, 30, 40])
Expand Down