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

Index slice #311

merged 5 commits into from
Sep 18, 2022

Conversation

Dr-Irv
Copy link
Collaborator

@Dr-Irv Dr-Irv commented Sep 17, 2022

Copy link
Contributor

@bashtage bashtage left a comment

Choose a reason for hiding this comment

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

Some questions and a test that shows all types allowed in the typevar can be used.

class _IndexSlice(Generic[_IndexSliceT]):
def __getitem__(self, arg) -> tuple[_IndexSliceT, ...]: ...
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.

)
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.

@Dr-Irv
Copy link
Collaborator Author

Dr-Irv commented Sep 18, 2022

@bashtage @twoertwein this is all green now, so should be good to go

Copy link
Member

@twoertwein twoertwein left a comment

Choose a reason for hiding this comment

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

LGTM but I'm not familiar with IndexSlice

@twoertwein twoertwein merged commit a3cabb3 into pandas-dev:main Sep 18, 2022
@twoertwein
Copy link
Member

Thanks @Dr-Irv

@Dr-Irv Dr-Irv deleted the IndexSlice branch December 28, 2022 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IndexSlice typing is no longer correct
3 participants