-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
Split test multi #20669
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
Closed
Closed
Split test multi #20669
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
be3a486
split up multi test cases into multiple files
xchoudhury 66f4f74
removed test_multi.py
xchoudhury 650829e
removed unnecessary imports
xchoudhury 09b349a
fixed import from .common to full path
xchoudhury 2b87293
fixed pep8 issues
xchoudhury e524003
fixed pep8 issues
xchoudhury a175096
added setup_method & create_index back to tests
xchoudhury 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import re | ||
|
||
import numpy as np | ||
|
||
from pandas import (Index, MultiIndex) | ||
|
||
import pandas.util.testing as tm | ||
|
||
from pandas.tests.indexes.common import Base | ||
|
||
|
||
class TestConstructor(Base): | ||
_holder = MultiIndex | ||
_compat_props = ['shape', 'ndim', 'size', 'itemsize'] | ||
|
||
def setup_method(self, method): | ||
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]) | ||
self.index_names = ['first', 'second'] | ||
self.indices = dict(index=MultiIndex(levels=[major_axis, minor_axis], | ||
labels=[major_labels, minor_labels | ||
], names=self.index_names, | ||
verify_integrity=False)) | ||
self.setup_indices() | ||
|
||
def create_index(self): | ||
return self.index | ||
|
||
def test_copy_in_constructor(self): | ||
levels = np.array(["a", "b", "c"]) | ||
labels = np.array([1, 1, 2, 0, 0, 1, 1]) | ||
val = labels[0] | ||
mi = MultiIndex(levels=[levels, levels], labels=[labels, labels], | ||
copy=True) | ||
assert mi.labels[0][0] == val | ||
labels[0] = 15 | ||
assert mi.labels[0][0] == val | ||
val = levels[0] | ||
levels[0] = "PANDA" | ||
assert mi.levels[0][0] == val | ||
|
||
def test_constructor_single_level(self): | ||
result = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux']], | ||
labels=[[0, 1, 2, 3]], names=['first']) | ||
assert isinstance(result, MultiIndex) | ||
expected = Index(['foo', 'bar', 'baz', 'qux'], name='first') | ||
tm.assert_index_equal(result.levels[0], expected) | ||
assert result.names == ['first'] | ||
|
||
def test_constructor_no_levels(self): | ||
tm.assert_raises_regex(ValueError, "non-zero number " | ||
"of levels/labels", | ||
MultiIndex, levels=[], labels=[]) | ||
both_re = re.compile('Must pass both levels and labels') | ||
with tm.assert_raises_regex(TypeError, both_re): | ||
MultiIndex(levels=[]) | ||
with tm.assert_raises_regex(TypeError, both_re): | ||
MultiIndex(labels=[]) | ||
|
||
def test_constructor_mismatched_label_levels(self): | ||
labels = [np.array([1]), np.array([2]), np.array([3])] | ||
levels = ["a"] | ||
tm.assert_raises_regex(ValueError, "Length of levels and labels " | ||
"must be the same", MultiIndex, | ||
levels=levels, labels=labels) | ||
length_error = re.compile('>= length of level') | ||
label_error = re.compile(r'Unequal label lengths: \[4, 2\]') | ||
|
||
# important to check that it's looking at the right thing. | ||
with tm.assert_raises_regex(ValueError, length_error): | ||
MultiIndex(levels=[['a'], ['b']], | ||
labels=[[0, 1, 2, 3], [0, 3, 4, 1]]) | ||
|
||
with tm.assert_raises_regex(ValueError, label_error): | ||
MultiIndex(levels=[['a'], ['b']], labels=[[0, 0, 0, 0], [0, 0]]) | ||
|
||
# external API | ||
with tm.assert_raises_regex(ValueError, length_error): | ||
self.index.copy().set_levels([['a'], ['b']]) | ||
|
||
with tm.assert_raises_regex(ValueError, label_error): | ||
self.index.copy().set_labels([[0, 0, 0, 0], [0, 0]]) |
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,50 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import numpy as np | ||
|
||
import pandas as pd | ||
|
||
from pandas import (Index, MultiIndex) | ||
|
||
from pandas.tests.indexes.common import Base | ||
|
||
|
||
class TestContains(Base): | ||
_holder = MultiIndex | ||
_compat_props = ['shape', 'ndim', 'size', 'itemsize'] | ||
|
||
def setup_method(self, method): | ||
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]) | ||
self.index_names = ['first', 'second'] | ||
self.indices = dict(index=MultiIndex(levels=[major_axis, minor_axis], | ||
labels=[major_labels, minor_labels | ||
], names=self.index_names, | ||
verify_integrity=False)) | ||
self.setup_indices() | ||
|
||
def create_index(self): | ||
return self.index | ||
|
||
def test_contains(self): | ||
assert ('foo', 'two') in self.index | ||
assert ('bar', 'two') not in self.index | ||
assert None not in self.index | ||
|
||
def test_contains_top_level(self): | ||
midx = MultiIndex.from_product([['A', 'B'], [1, 2]]) | ||
assert 'A' in midx | ||
assert 'A' not in midx._engine | ||
|
||
def test_contains_with_nat(self): | ||
# MI with a NaT | ||
mi = MultiIndex(levels=[['C'], | ||
pd.date_range('2012-01-01', periods=5)], | ||
labels=[[0, 0, 0, 0, 0, 0], [-1, 0, 1, 2, 3, 4]], | ||
names=[None, 'B']) | ||
assert ('C', pd.Timestamp('2012-01-01')) in mi | ||
for val in mi.values: | ||
assert val in mi |
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,138 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import pytest | ||
|
||
import numpy as np | ||
|
||
import pandas as pd | ||
|
||
from pandas import (Index, MultiIndex) | ||
from pandas.compat import lrange | ||
from pandas.errors import PerformanceWarning | ||
|
||
import pandas.util.testing as tm | ||
|
||
from pandas.tests.indexes.common import Base | ||
|
||
|
||
class TestDrop(Base): | ||
_holder = MultiIndex | ||
_compat_props = ['shape', 'ndim', 'size', 'itemsize'] | ||
|
||
def setup_method(self, method): | ||
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]) | ||
self.index_names = ['first', 'second'] | ||
self.indices = dict(index=MultiIndex(levels=[major_axis, minor_axis], | ||
labels=[major_labels, minor_labels | ||
], names=self.index_names, | ||
verify_integrity=False)) | ||
self.setup_indices() | ||
|
||
def create_index(self): | ||
return self.index | ||
|
||
def test_drop(self): | ||
dropped = self.index.drop([('foo', 'two'), ('qux', 'one')]) | ||
|
||
index = MultiIndex.from_tuples([('foo', 'two'), ('qux', 'one')]) | ||
dropped2 = self.index.drop(index) | ||
|
||
expected = self.index[[0, 2, 3, 5]] | ||
tm.assert_index_equal(dropped, expected) | ||
tm.assert_index_equal(dropped2, expected) | ||
|
||
dropped = self.index.drop(['bar']) | ||
expected = self.index[[0, 1, 3, 4, 5]] | ||
tm.assert_index_equal(dropped, expected) | ||
|
||
dropped = self.index.drop('foo') | ||
expected = self.index[[2, 3, 4, 5]] | ||
tm.assert_index_equal(dropped, expected) | ||
|
||
index = MultiIndex.from_tuples([('bar', 'two')]) | ||
pytest.raises(KeyError, self.index.drop, [('bar', 'two')]) | ||
pytest.raises(KeyError, self.index.drop, index) | ||
pytest.raises(KeyError, self.index.drop, ['foo', 'two']) | ||
|
||
# partially correct argument | ||
mixed_index = MultiIndex.from_tuples([('qux', 'one'), ('bar', 'two')]) | ||
pytest.raises(KeyError, self.index.drop, mixed_index) | ||
|
||
# error='ignore' | ||
dropped = self.index.drop(index, errors='ignore') | ||
expected = self.index[[0, 1, 2, 3, 4, 5]] | ||
tm.assert_index_equal(dropped, expected) | ||
|
||
dropped = self.index.drop(mixed_index, errors='ignore') | ||
expected = self.index[[0, 1, 2, 3, 5]] | ||
tm.assert_index_equal(dropped, expected) | ||
|
||
dropped = self.index.drop(['foo', 'two'], errors='ignore') | ||
expected = self.index[[2, 3, 4, 5]] | ||
tm.assert_index_equal(dropped, expected) | ||
|
||
# mixed partial / full drop | ||
dropped = self.index.drop(['foo', ('qux', 'one')]) | ||
expected = self.index[[2, 3, 5]] | ||
tm.assert_index_equal(dropped, expected) | ||
|
||
# mixed partial / full drop / error='ignore' | ||
mixed_index = ['foo', ('qux', 'one'), 'two'] | ||
pytest.raises(KeyError, self.index.drop, mixed_index) | ||
dropped = self.index.drop(mixed_index, errors='ignore') | ||
expected = self.index[[2, 3, 5]] | ||
tm.assert_index_equal(dropped, expected) | ||
|
||
def test_droplevel_with_names(self): | ||
index = self.index[self.index.get_loc('foo')] | ||
dropped = index.droplevel(0) | ||
assert dropped.name == 'second' | ||
|
||
index = MultiIndex( | ||
levels=[Index(lrange(4)), Index(lrange(4)), Index(lrange(4))], | ||
labels=[np.array([0, 0, 1, 2, 2, 2, 3, 3]), np.array( | ||
[0, 1, 0, 0, 0, 1, 0, 1]), np.array([1, 0, 1, 1, 0, 0, 1, 0])], | ||
names=['one', 'two', 'three']) | ||
dropped = index.droplevel(0) | ||
assert dropped.names == ('two', 'three') | ||
|
||
dropped = index.droplevel('two') | ||
expected = index.droplevel(1) | ||
assert dropped.equals(expected) | ||
|
||
def test_droplevel_multiple(self): | ||
index = MultiIndex( | ||
levels=[Index(lrange(4)), Index(lrange(4)), Index(lrange(4))], | ||
labels=[np.array([0, 0, 1, 2, 2, 2, 3, 3]), np.array( | ||
[0, 1, 0, 0, 0, 1, 0, 1]), np.array([1, 0, 1, 1, 0, 0, 1, 0])], | ||
names=['one', 'two', 'three']) | ||
|
||
dropped = index[:2].droplevel(['three', 'one']) | ||
expected = index[:2].droplevel(2).droplevel(0) | ||
assert dropped.equals(expected) | ||
|
||
def test_drop_not_lexsorted(self): | ||
# GH 12078 | ||
|
||
# define the lexsorted version of the multi-index | ||
tuples = [('a', ''), ('b1', 'c1'), ('b2', 'c2')] | ||
lexsorted_mi = MultiIndex.from_tuples(tuples, names=['b', 'c']) | ||
assert lexsorted_mi.is_lexsorted() | ||
|
||
# and the not-lexsorted version | ||
df = pd.DataFrame(columns=['a', 'b', 'c', 'd'], | ||
data=[[1, 'b1', 'c1', 3], [1, 'b2', 'c2', 4]]) | ||
df = df.pivot_table(index='a', columns=['b', 'c'], values='d') | ||
df = df.reset_index() | ||
not_lexsorted_mi = df.columns | ||
assert not not_lexsorted_mi.is_lexsorted() | ||
|
||
# compare the results | ||
tm.assert_index_equal(lexsorted_mi, not_lexsorted_mi) | ||
with tm.assert_produces_warning(PerformanceWarning): | ||
tm.assert_index_equal(lexsorted_mi.drop('a'), | ||
not_lexsorted_mi.drop('a')) |
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.
so things that are not used in a particular module need not be repeated here (e.g. in the constructor) and/or properties in the class.
We want to actually remove the need for the class entirely. You could do that here as well (or in a next pass PR).
This is a little trickier because need to turn things (like self.index) into fixtures. so ok with doing this later.