-
-
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 1 commit
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,20 @@ 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 level name | ||
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. The general format is to specify the data type on the first line followed by an explanation of their semantics in a subsequent block. 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. Ok, I've changed this, also for MultiIndex. |
||
|
||
Returns | ||
------- | ||
values : Index | ||
self : 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. You aren't returning 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. Actually it is returning self. this method probably meant to make the API the same for MultiIndex and other Index types, so for Index types this really doesn't do much. The name should I still change it? 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. @topper-123 : Sorry, I wasn't able to check the actual file on my phone. Yes, you are correct that |
||
|
||
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,7 +882,7 @@ 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 | ||
---------- | ||
|
@@ -891,6 +891,24 @@ def get_level_values(self, 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: | ||
|
||
>>> i1 = pd.Index(list('abc'), name='level_1') | ||
>>> i2 = pd.CategoricalIndex(list('def'), name='level_2') | ||
>>> mi = pd.MultiIndex.from_arrays((i1, i2)) | ||
|
||
Get level values by supplying level as either integer or name: | ||
|
||
>>> mi.get_level_values(1) | ||
CategoricalIndex(['d', 'e', 'f'], categories=['d', 'e', 'f'], | ||
ordered=False, name='level_2', | ||
dtype='category') | ||
>>> mi.get_level_values('level_1') | ||
Index(['a', 'b', 'c'], dtype='object', name='level_1') | ||
""" | ||
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) | ||
|
||
# test with name | ||
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. Reference your PR here. |
||
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.