-
- Notifications
You must be signed in to change notification settings - Fork 19.4k
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 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 |
|---|---|---|
| | @@ -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]) | ||
| Contributor 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) Member Author 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 flat 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) | ||
| | ||
| Contributor 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 Member Author 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 flat 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]) | ||
| Contributor 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) Member Author 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? Contributor 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, | ||
| | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -141,6 +141,45 @@ def test_reset_index(self): | |
| tm.assert_index_equal(rs.index, Index(index.get_level_values(1))) | ||
| assert isinstance(rs, Series) | ||
| | ||
| def test_reset_index_level(self): | ||
| df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], | ||
| columns=['A', 'B', 'C']) | ||
| | ||
| for levels in ['A', 'B'], [0, 1]: | ||
| # With MultiIndex | ||
| s = df.set_index(['A', 'B'])['C'] | ||
| | ||
| result = s.reset_index(level=levels[0]) | ||
| tm.assert_frame_equal(result, df.set_index('B')) | ||
| | ||
| result = s.reset_index(level=levels[:1]) | ||
| tm.assert_frame_equal(result, df.set_index('B')) | ||
| | ||
| result = s.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']]) | ||
| | ||
| with tm.assert_raises_regex(KeyError, 'Level E '): | ||
| s.reset_index(level=['A', 'E']) | ||
| | ||
| # With flat Index | ||
| ||
| s = df.set_index('A')['B'] | ||
| | ||
| result = s.reset_index(level=levels[0]) | ||
| tm.assert_frame_equal(result, df[['A', 'B']]) | ||
| | ||
| result = s.reset_index(level=levels[:1]) | ||
| tm.assert_frame_equal(result, df[['A', 'B']]) | ||
| | ||
| result = s.reset_index(level=levels[0], drop=True) | ||
| tm.assert_series_equal(result, df['B']) | ||
| | ||
| with tm.assert_raises_regex(IndexError, 'Too many levels'): | ||
| s.reset_index(level=[0, 1, 2]) | ||
| | ||
| def test_reset_index_range(self): | ||
| # GH 12071 | ||
| s = pd.Series(range(2), name='A', dtype='int64') | ||
| | ||
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