-
-
Notifications
You must be signed in to change notification settings - Fork 141
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
Index slice #311
Conversation
There was a problem hiding this 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: ... |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@bashtage @twoertwein this is all green now, so should be good to go |
There was a problem hiding this 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
Thanks @Dr-Irv |
test_frame.py:test_indexslice_getitem()