-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC: Added docs to clarify that MultiIndex.set_levels() interprets passed values as new components of the .levels attribute (#28294) #28797
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
e8256cc
d9130f2
46d88c1
1ef3637
f625730
c9b6261
25b1859
29e56df
acde02b
5122241
df2e081
43687c0
93183ba
71f8ab9
548f83d
5cbd82b
fcea0dc
959a760
4f97ca4
9743154
9a3e1ef
eca6079
0c4404b
de2e086
d8f9be7
230a61a
fab2c90
e1e51df
a4c92ca
3954fa7
6241b9d
0474c7f
3fba92e
894eac6
981299c
7ea5c96
a31e160
0748c91
54b1151
d6b058d
c387a28
fad037e
4ade26b
1e92886
56b2fd8
25059ee
f7d162b
625c550
0eee324
2931f02
04d7931
06a6b49
851ca1a
18a9e4c
2f80feb
5b0bf23
8c5941c
97c6567
80412cc
c65cfb6
3eca505
3b34368
125739f
6dc53bb
19c8fad
8d3fec9
74bbbb0
de67bb7
d52850f
0a108f0
c903e5e
b632ca0
79a5f7c
a0d01b8
86e187f
46e89b0
5ad908e
fdc322a
b63f829
bff90a3
30a0e2e
143eb38
9486f04
da3d0d9
9d45934
509eb14
6d35836
b0f33b3
709436d
f556a71
827440a
1e4fe0a
2683954
45dc6d3
58d34d9
e54b995
2701f52
09a9f5f
b372ac4
09fc1b4
5bdd7db
9c4a371
d186f77
a2f5ae2
cb99e24
930dd84
7c8c8c8
e4afa45
cb76dcb
6931051
04893a9
971d191
8ccc7c7
36803b9
e623f0f
d2d8785
9f03837
9026a79
4f5e38d
c00cc9d
211574b
93c84bb
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 |
---|---|---|
|
@@ -719,8 +719,16 @@ def _set_levels( | |
|
||
def set_levels(self, levels, level=None, inplace=False, verify_integrity=True): | ||
""" | ||
Set new levels on MultiIndex. Defaults to returning | ||
new index. | ||
Set levels on MultiIndex by passing a new value for each | ||
index in the level. Defaults to returning new | ||
index. | ||
|
||
It is assumed that a new value is provided for each code describing | ||
values in the level. If the number of values passed is more than | ||
the number of index values in the level, ``set_levels`` will still | ||
pass the values to the level. The passed values are stored in the | ||
MultiIndex FrozenList even though the index values may be truncated | ||
in the MultiIndex output from set_levels. | ||
|
||
Parameters | ||
---------- | ||
|
@@ -740,32 +748,51 @@ def set_levels(self, levels, level=None, inplace=False, verify_integrity=True): | |
Examples | ||
-------- | ||
>>> idx = pd.MultiIndex.from_tuples([(1, 'one'), (1, 'two'), | ||
(2, 'one'), (2, 'two')], | ||
(2, 'one'), (2, 'two'), | ||
(3, 'one'), (3, 'two')], | ||
names=['foo', 'bar']) | ||
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. Can you should 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. Sure, will apply this change. |
||
>>> idx.set_levels([['a', 'b'], [1, 2]]) | ||
>>> idx.set_levels([['a', 'b', 'c'], [1, 2]]) | ||
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. Why do you think an extra value makes this example clearer? I think a more concise example is easier to read. 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. My intention of adding an extra value at one of the levels (same for the MultiIndex above) is to further highlight the differences in passing values to the two levels in the MultiIndex, for which the number of values in each level are not equal. Using different number of values in each level is to emphasize that set_levels() passes values to indexes on 'by level' basis (by changing the levels attribute), not on 'by row / integer code' basis. Then again, if a more concise example could also explain sufficiently how set_levels() work without ambiguity, I'm okay with reverting to the original MultiIndex. Do let me know what you think, so that I can make the necessary modifications to subsequent examples. 👍 |
||
MultiIndex([('a', 1), | ||
('a', 2), | ||
('b', 1), | ||
('b', 2)], | ||
('b', 2), | ||
('c', 1), | ||
('c', 2)], | ||
names=['foo', 'bar']) | ||
>>> idx.set_levels(['a', 'b'], level=0) | ||
>>> idx.set_levels(['a', 'b', 'c'], level=0) | ||
MultiIndex([('a', 'one'), | ||
('a', 'two'), | ||
('b', 'one'), | ||
('b', 'two')], | ||
('b', 'two'), | ||
('c', 'one'), | ||
('c', 'two')], | ||
names=['foo', 'bar']) | ||
>>> idx.set_levels(['a', 'b'], level='bar') | ||
MultiIndex([(1, 'a'), | ||
(1, 'b'), | ||
(2, 'a'), | ||
(2, 'b')], | ||
(2, 'b'), | ||
(3, 'a'), | ||
(3, 'b')], | ||
names=['foo', 'bar']) | ||
>>> idx.set_levels([['a', 'b', 'c'], [1, 2]], level=[0, 1]) | ||
MultiIndex([('a', 1), | ||
('a', 2), | ||
('b', 1), | ||
('b', 2), | ||
('c', 1), | ||
('c', 2)], | ||
names=['foo', 'bar']) | ||
>>> idx.set_levels([['a', 'b'], [1, 2]], level=[0, 1]) | ||
>>> idx.set_levels([['a', 'b', 'c'], [1, 2]], level=[0, 1]).levels | ||
FrozenList([['a', 'b', 'c'], [1, 2]]) | ||
>>> idx.set_levels([['a', 'b', 'c'], [1, 2, 3, 4]], level=[0, 1]) | ||
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. Can you separate this example from the previous, and explain what are we showing? 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 intention of this example is to illustrate that set_levels() passes values into the levels attribute that is represented by a FrozenList containing list of values for each level in the MultiIndex, even when the number of values passed for a level is more than the number of indexes available in the MultiIndex itself. I'll separate this example from the previous example, and append the explanation to the example. |
||
MultiIndex([('a', 1), | ||
('a', 2), | ||
('b', 1), | ||
('b', 2)], | ||
names=['foo', 'bar']) | ||
>>> idx.set_levels([['a', 'b', 'c'], [1, 2, 3, 4]], level=[0, 1]).levels | ||
FrozenList([['a', 'b', 'c'], [1, 2, 3, 4]]) | ||
""" | ||
if is_list_like(levels) and not isinstance(levels, Index): | ||
levels = list(levels) | ||
|
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.
The short description (this first paragraph) needs to be a single liner (in other paragraphs we can have as much information as needed).
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.
Oops, just realised I kinda broke PEP 257 for docstrings. Will make the changes.