-
-
Notifications
You must be signed in to change notification settings - Fork 143
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
Changes from all commits
76e21ea
3519805
bc44d01
e843c96
486fdf8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe 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]) | ||
|
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 aslice
or atuple
. So if you dopd.IndexSlice[1:2]
, that returns aslice
not atuple
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.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.