-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: support for "level=" when reset_index() is called with a flat Index #16266
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 all 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 |
---|---|---|
|
@@ -641,6 +641,43 @@ def test_reset_index(self): | |
xp = xp.set_index(['B'], append=True) | ||
assert_frame_equal(rs, xp, check_names=False) | ||
|
||
def test_reset_index_level(self): | ||
df = pd.DataFrame([[1, 2, 3, 4], [5, 6, 7, 8]], | ||
columns=['A', 'B', 'C', 'D']) | ||
|
||
for levels in ['A', 'B'], [0, 1]: | ||
# With MultiIndex | ||
result = df.set_index(['A', 'B']).reset_index(level=levels[0]) | ||
tm.assert_frame_equal(result, df.set_index('B')) | ||
|
||
result = df.set_index(['A', 'B']).reset_index(level=levels[: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 u add similar tests for Series (in series test hierarchy) 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. done |
||
tm.assert_frame_equal(result, df.set_index('B')) | ||
|
||
result = df.set_index(['A', 'B']).reset_index(level=levels) | ||
tm.assert_frame_equal(result, df) | ||
|
||
result = df.set_index(['A', 'B']).reset_index(level=levels, | ||
drop=True) | ||
tm.assert_frame_equal(result, df[['C', 'D']]) | ||
|
||
# With single-level Index (GH 16263) | ||
result = df.set_index('A').reset_index(level=levels[0]) | ||
tm.assert_frame_equal(result, df) | ||
|
||
result = df.set_index('A').reset_index(level=levels[:1]) | ||
tm.assert_frame_equal(result, df) | ||
|
||
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. add with drop as well 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. done |
||
result = df.set_index(['A']).reset_index(level=levels[0], | ||
drop=True) | ||
tm.assert_frame_equal(result, df[['B', 'C', 'D']]) | ||
|
||
# Missing levels - for both MultiIndex and single-level Index: | ||
for idx_lev in ['A', 'B'], ['A']: | ||
with tm.assert_raises_regex(KeyError, 'Level E '): | ||
df.set_index(idx_lev).reset_index(level=['A', 'E']) | ||
with tm.assert_raises_regex(IndexError, 'Too many levels'): | ||
df.set_index(idx_lev).reset_index(level=[0, 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. it might be easier to parameteize these tests (snd make the errors a separate test) 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 clarify? 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 guess this is fine. |
||
|
||
def test_reset_index_right_dtype(self): | ||
time = np.arange(0.0, 10, np.sqrt(2) / 2) | ||
s1 = Series((9.81 * time ** 2) / 2, | ||
|
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.
issue number
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 number is few lines after, it only applies to the flat index