-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
REF: collect+parametrize reorder_levels tests #32373
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
Merged
simonjayhawkins
merged 11 commits into
pandas-dev:master
from
jbrockmendel:tst-alter_axes
Mar 2, 2020
+87
−98
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f646265
test_reorder_levels
jbrockmendel 8f93384
Merge branch 'master' of https://github.com/pandas-dev/pandas into ts…
jbrockmendel 440498c
Merge branch 'master' of https://github.com/pandas-dev/pandas into ts…
jbrockmendel 84076d5
Merge branch 'master' of https://github.com/pandas-dev/pandas into ts…
jbrockmendel 29e593e
Merge branch 'master' of https://github.com/pandas-dev/pandas into ts…
jbrockmendel f1f25e4
Merge branch 'master' of https://github.com/pandas-dev/pandas into ts…
jbrockmendel 108521b
Merge branch 'master' of https://github.com/pandas-dev/pandas into ts…
jbrockmendel 777d82f
REF: collect+parametrize reorder_levels tests
jbrockmendel 9d6dff9
Merge branch 'master' of https://github.com/pandas-dev/pandas into ts…
jbrockmendel 831558e
Merge branch 'master' of https://github.com/pandas-dev/pandas into ts…
jbrockmendel 4f0975e
use fixture
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from pandas import DataFrame, MultiIndex, Series | ||
import pandas._testing as tm | ||
|
||
|
||
class TestReorderLevels: | ||
@pytest.mark.parametrize("klass", [Series, DataFrame]) | ||
def test_reorder_levels(self, klass): | ||
index = MultiIndex( | ||
levels=[["bar"], ["one", "two", "three"], [0, 1]], | ||
codes=[[0, 0, 0, 0, 0, 0], [0, 1, 2, 0, 1, 2], [0, 1, 0, 1, 0, 1]], | ||
names=["L0", "L1", "L2"], | ||
) | ||
df = DataFrame({"A": np.arange(6), "B": np.arange(6)}, index=index) | ||
obj = df if klass is DataFrame else df["A"] | ||
|
||
# no change, position | ||
result = obj.reorder_levels([0, 1, 2]) | ||
tm.assert_equal(obj, result) | ||
|
||
# no change, labels | ||
result = obj.reorder_levels(["L0", "L1", "L2"]) | ||
tm.assert_equal(obj, result) | ||
|
||
# rotate, position | ||
result = obj.reorder_levels([1, 2, 0]) | ||
e_idx = MultiIndex( | ||
levels=[["one", "two", "three"], [0, 1], ["bar"]], | ||
codes=[[0, 1, 2, 0, 1, 2], [0, 1, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0]], | ||
names=["L1", "L2", "L0"], | ||
) | ||
expected = DataFrame({"A": np.arange(6), "B": np.arange(6)}, index=e_idx) | ||
expected = expected if klass is DataFrame else expected["A"] | ||
tm.assert_equal(result, expected) | ||
|
||
result = obj.reorder_levels([0, 0, 0]) | ||
e_idx = MultiIndex( | ||
levels=[["bar"], ["bar"], ["bar"]], | ||
codes=[[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]], | ||
names=["L0", "L0", "L0"], | ||
) | ||
expected = DataFrame({"A": np.arange(6), "B": np.arange(6)}, index=e_idx) | ||
expected = expected if klass is DataFrame else expected["A"] | ||
tm.assert_equal(result, expected) | ||
|
||
result = obj.reorder_levels(["L0", "L0", "L0"]) | ||
tm.assert_equal(result, expected) | ||
|
||
def test_reorder_levels_swaplevel_equivalence( | ||
self, multiindex_year_month_day_dataframe_random_data | ||
): | ||
|
||
ymd = multiindex_year_month_day_dataframe_random_data | ||
|
||
result = ymd.reorder_levels(["month", "day", "year"]) | ||
expected = ymd.swaplevel(0, 1).swaplevel(1, 2) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
result = ymd["A"].reorder_levels(["month", "day", "year"]) | ||
expected = ymd["A"].swaplevel(0, 1).swaplevel(1, 2) | ||
tm.assert_series_equal(result, expected) | ||
|
||
result = ymd.T.reorder_levels(["month", "day", "year"], axis=1) | ||
expected = ymd.T.swaplevel(0, 1, axis=1).swaplevel(1, 2, axis=1) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
with pytest.raises(TypeError, match="hierarchical axis"): | ||
ymd.reorder_levels([1, 2], axis=1) | ||
|
||
with pytest.raises(IndexError, match="Too many levels"): | ||
ymd.index.reorder_levels([1, 2, 3]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
if this is going to happen a lot with these changes, should maybe consider a solution that can maintain the correct assertion helper.
either in the parameterisation as "klass, helper" or create a fixture something like
(code not tested!)
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.
might make sense to add as a kwarg to tm.assert_equal
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.
yeah, that sounds good as well. could be follow-up