-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC/TST: Add examples to MultiIndex.get_level_values + related changes #17414
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
Changes from 2 commits
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 |
---|---|---|
|
@@ -2529,15 +2529,24 @@ def set_value(self, arr, key, value): | |
def _get_level_values(self, level): | ||
""" | ||
Return an Index of values for requested level, equal to the length | ||
of the index | ||
of the index. | ||
|
||
Parameters | ||
---------- | ||
level : int | ||
level : int or str | ||
``level`` is either the integer position of the level in the | ||
MultiIndex, or the name of the level. | ||
|
||
Returns | ||
------- | ||
values : Index | ||
Because there is only one level when the index has one level, | ||
the return value is always self in this case. | ||
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 would simplify to just: "self, as there is only one level in the Index" 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. Yes, thats better |
||
|
||
See also | ||
--------- | ||
pandas.MultiIndex.get_level_values : get values for a level of a | ||
MultiIndex | ||
""" | ||
|
||
self._validate_index_level(level) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -882,15 +882,32 @@ def _get_level_values(self, level): | |
def get_level_values(self, level): | ||
""" | ||
Return vector of label values for requested level, | ||
equal to the length of the index | ||
equal to the length of the index. | ||
|
||
Parameters | ||
---------- | ||
level : int or level name | ||
level : int or str | ||
``level`` is either the integer position of the level in the | ||
MultiIndex, or the name of the level. | ||
|
||
Returns | ||
------- | ||
values : Index | ||
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. Let's explain what |
||
|
||
Examples | ||
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. Note that I've made the examples a bit simpler. No need to demonstrate CategoricalIndex etc. here... |
||
--------- | ||
|
||
Create a MultiIndex: | ||
|
||
>>> mi = pd.MultiIndex.from_arrays((list('abc'), list('def'))) | ||
>>> mi.names = ['level_1', 'level_2'] | ||
|
||
Get level values by supplying level as either integer or name: | ||
|
||
>>> mi.get_level_values(0) | ||
Index(['a', 'b', 'c'], dtype='object', name='level_1') | ||
>>> mi.get_level_values('level_2') | ||
Index(['d', 'e', 'f'], dtype='object', name='level_2') | ||
""" | ||
level = self._get_level_number(level) | ||
values = self._get_level_values(level) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1438,6 +1438,12 @@ def test_get_level_values(self): | |
result = self.strIndex.get_level_values(0) | ||
tm.assert_index_equal(result, self.strIndex) | ||
|
||
# GH 17414 | ||
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. You didn't need to necessarily remove your other note. I was suggesting that you just add a reference to the issue somewhere in the comments. |
||
index_with_name = self.strIndex.copy() | ||
index_with_name.name = 'a' | ||
result = index_with_name.get_level_values('a') | ||
tm.assert_index_equal(result, index_with_name) | ||
|
||
def test_slice_keep_name(self): | ||
idx = Index(['a', 'b'], name='asdf') | ||
assert idx.name == idx[1:].name | ||
|
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.
Not Blocking : I would really like if we could condense this into one line (doc-string convention), but it's hard to see ATM what to remove from this...
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 haven't got a better proposal either.