-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
split up pandas/tests/indexes/test_multi.py #18644 #21514
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1888a31
split up pandas/tests/indexes/test_multi.py #18644
elmq0022 5780b7e
addressing comments to pull request #21514 for GH issue #18644
elmq0022 750b8c6
Changes fixture _index to fixture idx
elmq0022 2fb312b
linting after fixes
elmq0022 e238b94
moved tests according to feedback
elmq0022 7a3753f
removing the named_index fixture
elmq0022 baaace3
split test_multi.py into separate files and address the comments from…
elmq0022 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
Empty file.
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,43 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import numpy as np | ||
import pytest | ||
from pandas import Index, MultiIndex | ||
|
||
|
||
@pytest.fixture | ||
def idx(): | ||
# a MultiIndex used to test the general functionality of the | ||
# general functionality of this object | ||
major_axis = Index(['foo', 'bar', 'baz', 'qux']) | ||
minor_axis = Index(['one', 'two']) | ||
|
||
major_labels = np.array([0, 0, 1, 2, 3, 3]) | ||
minor_labels = np.array([0, 1, 0, 1, 0, 1]) | ||
index_names = ['first', 'second'] | ||
index = MultiIndex( | ||
levels=[major_axis, minor_axis], | ||
labels=[major_labels, minor_labels], | ||
names=index_names, | ||
verify_integrity=False | ||
) | ||
return index | ||
|
||
|
||
@pytest.fixture | ||
def index_names(): | ||
# names that match those in the idx fixture for testing equality of | ||
# names assigned to the idx | ||
return ['first', 'second'] | ||
|
||
|
||
@pytest.fixture | ||
def holder(): | ||
# the MultiIndex constructor used to base compatibility with pickle | ||
return MultiIndex | ||
|
||
|
||
@pytest.fixture | ||
def compat_props(): | ||
# a MultiIndex must have these properties associated with it | ||
return ['shape', 'ndim', 'size'] |
File renamed without changes.
File renamed without changes.
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,8 @@ | ||
import pytest | ||
|
||
|
||
def test_shift(idx): | ||
|
||
# GH8083 test the base class for shift | ||
pytest.raises(NotImplementedError, idx.shift, 1) | ||
pytest.raises(NotImplementedError, idx.shift, 1, 2) |
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,122 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
|
||
import numpy as np | ||
import pandas.util.testing as tm | ||
import pytest | ||
from pandas import MultiIndex | ||
from pandas.compat import PY3, long | ||
|
||
|
||
def test_numeric_compat(idx): | ||
tm.assert_raises_regex(TypeError, "cannot perform __mul__", | ||
lambda: idx * 1) | ||
tm.assert_raises_regex(TypeError, "cannot perform __rmul__", | ||
lambda: 1 * idx) | ||
|
||
div_err = "cannot perform __truediv__" if PY3 \ | ||
else "cannot perform __div__" | ||
tm.assert_raises_regex(TypeError, div_err, lambda: idx / 1) | ||
div_err = div_err.replace(' __', ' __r') | ||
tm.assert_raises_regex(TypeError, div_err, lambda: 1 / idx) | ||
tm.assert_raises_regex(TypeError, "cannot perform __floordiv__", | ||
lambda: idx // 1) | ||
tm.assert_raises_regex(TypeError, "cannot perform __rfloordiv__", | ||
lambda: 1 // idx) | ||
|
||
|
||
def test_logical_compat(idx): | ||
tm.assert_raises_regex(TypeError, 'cannot perform all', | ||
lambda: idx.all()) | ||
tm.assert_raises_regex(TypeError, 'cannot perform any', | ||
lambda: idx.any()) | ||
|
||
|
||
def test_boolean_context_compat(idx): | ||
|
||
with pytest.raises(ValueError): | ||
bool(idx) | ||
|
||
|
||
def test_boolean_context_compat2(): | ||
|
||
# boolean context compat | ||
# GH7897 | ||
i1 = MultiIndex.from_tuples([('A', 1), ('A', 2)]) | ||
i2 = MultiIndex.from_tuples([('A', 1), ('A', 3)]) | ||
common = i1.intersection(i2) | ||
|
||
with pytest.raises(ValueError): | ||
bool(common) | ||
|
||
|
||
def test_inplace_mutation_resets_values(): | ||
levels = [['a', 'b', 'c'], [4]] | ||
levels2 = [[1, 2, 3], ['a']] | ||
labels = [[0, 1, 0, 2, 2, 0], [0, 0, 0, 0, 0, 0]] | ||
|
||
mi1 = MultiIndex(levels=levels, labels=labels) | ||
mi2 = MultiIndex(levels=levels2, labels=labels) | ||
vals = mi1.values.copy() | ||
vals2 = mi2.values.copy() | ||
|
||
assert mi1._tuples is not None | ||
|
||
# Make sure level setting works | ||
new_vals = mi1.set_levels(levels2).values | ||
tm.assert_almost_equal(vals2, new_vals) | ||
|
||
# Non-inplace doesn't kill _tuples [implementation detail] | ||
tm.assert_almost_equal(mi1._tuples, vals) | ||
|
||
# ...and values is still same too | ||
tm.assert_almost_equal(mi1.values, vals) | ||
|
||
# Inplace should kill _tuples | ||
mi1.set_levels(levels2, inplace=True) | ||
tm.assert_almost_equal(mi1.values, vals2) | ||
|
||
# Make sure label setting works too | ||
labels2 = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] | ||
exp_values = np.empty((6,), dtype=object) | ||
exp_values[:] = [(long(1), 'a')] * 6 | ||
|
||
# Must be 1d array of tuples | ||
assert exp_values.shape == (6,) | ||
new_values = mi2.set_labels(labels2).values | ||
|
||
# Not inplace shouldn't change | ||
tm.assert_almost_equal(mi2._tuples, vals2) | ||
|
||
# Should have correct values | ||
tm.assert_almost_equal(exp_values, new_values) | ||
|
||
# ...and again setting inplace should kill _tuples, etc | ||
mi2.set_labels(labels2, inplace=True) | ||
tm.assert_almost_equal(mi2.values, new_values) | ||
|
||
|
||
def test_ndarray_compat_properties(idx, compat_props): | ||
assert idx.T.equals(idx) | ||
assert idx.transpose().equals(idx) | ||
|
||
values = idx.values | ||
for prop in compat_props: | ||
assert getattr(idx, prop) == getattr(values, prop) | ||
|
||
# test for validity | ||
idx.nbytes | ||
idx.values.nbytes | ||
|
||
|
||
def test_compat(indices): | ||
assert indices.tolist() == list(indices) | ||
|
||
|
||
def test_pickle_compat_construction(holder): | ||
# this is testing for pickle compat | ||
if holder is None: | ||
return | ||
|
||
# need an object to create with | ||
pytest.raises(TypeError, holder) |
Oops, something went wrong.
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.
I see that you are iterating over this in a number of tests. why is that?
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.
That's more or less a holdover from how the inherited base test class was implemented. I was thinking I'd leave it in case anyone wanted to extend some of these cases with another test multi-index, but yes it looks kind of sloppy. I'll refactor it and remove the duplicate fixture.
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.
Okay, done with that, but some tests are not relevant to the MultiIndex class or will need to be refactored. I marked those tests with a TODO. The tests that I could refactor have been. 😅
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 that's ok. i expect after your refactor will need to go over this again and clean a bit