-
- 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
BUG: support for "level=" when reset_index() is called with a flat Index #16266
Conversation
jorisvandenbossche left a comment
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.
Looks good
doc/source/whatsnew/v0.21.0.txt Outdated
| Indexing | ||
| ^^^^^^^^ | ||
| | ||
| - Bug in ``DataFrame.reset_index(level=)`` with flat index (:issue:`16263`) |
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.
Can you move this to the 0.20.2 file?
| columns=['A', 'B', 'C', 'D']) | ||
| | ||
| # With MultiIndex | ||
| for levels in ['A', 'B', 'E'], [0, 1, 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.
Can you remove the 'E' and 2 from the lists? I founded it a bit confusing as you don't use it
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.
yep, they were refactoring leftovers
Codecov Report
@@ Coverage Diff @@ ## master #16266 +/- ## ========================================== - Coverage 90.33% 90.3% -0.04% ========================================== Files 167 167 Lines 50907 50908 +1 ========================================== - Hits 45989 45974 -15 - Misses 4918 4934 +16
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@ ## master #16266 +/- ## ========================================== - Coverage 90.32% 90.3% -0.02% ========================================== Files 167 167 Lines 50907 50908 +1 ========================================== - Hits 45982 45973 -9 - Misses 4925 4935 +10
Continue to review full report at Codecov.
|
fd83502 to f4c3121 Compare doc/source/whatsnew/v0.20.2.txt Outdated
| ^^^^^^^^ | ||
| | ||
| | ||
| - Bug in ``DataFrame.reset_index(level=)`` with flat index (:issue:`16263`) |
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.
single level index
| | ||
| def test_reset_index_level(self): | ||
| df = pd.DataFrame([[1, 2, 3, 4], [5, 6, 7, 8]], | ||
| columns=['A', 'B', 'C', 'D']) |
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
| 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
done
| | ||
| 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
done
| 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 comment
The 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is fine.
f4c3121 to 7cc21c2 Compare | with tm.assert_raises_regex(KeyError, 'Level E '): | ||
| s.reset_index(level=['A', 'E']) | ||
| | ||
| # With flat Index |
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.
don't use word flat, rather single level
| minor comment, otherwise lgtm. |
| Thanks! |
…level Index (pandas-dev#16266) (cherry picked from commit 8809b04)
git diff upstream/master --name-only -- '*.py' | flake8 --diff