-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
REF/TST: Add more pytest idiom to mi indexing tests #24040
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
Changes from all commits
Commits
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,129 +1,123 @@ | ||
from warnings import catch_warnings | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
from pandas import DataFrame, MultiIndex, Series | ||
from pandas.util import testing as tm | ||
|
||
|
||
@pytest.mark.filterwarnings("ignore:\\n.ix:DeprecationWarning") | ||
class TestMultiIndexIloc(object): | ||
def test_iloc_getitem_multiindex2(): | ||
# TODO(wesm): fix this | ||
pytest.skip('this test was being suppressed, ' | ||
'needs to be fixed') | ||
|
||
arr = np.random.randn(3, 3) | ||
df = DataFrame(arr, columns=[[2, 2, 4], [6, 8, 10]], | ||
index=[[4, 4, 8], [8, 10, 12]]) | ||
|
||
rs = df.iloc[2] | ||
xp = Series(arr[2], index=df.columns) | ||
tm.assert_series_equal(rs, xp) | ||
|
||
rs = df.iloc[:, 2] | ||
xp = Series(arr[:, 2], index=df.index) | ||
tm.assert_series_equal(rs, xp) | ||
|
||
rs = df.iloc[2, 2] | ||
xp = df.values[2, 2] | ||
assert rs == xp | ||
|
||
# for multiple items | ||
# GH 5528 | ||
rs = df.iloc[[0, 1]] | ||
xp = df.xs(4, drop_level=False) | ||
tm.assert_frame_equal(rs, xp) | ||
|
||
def test_iloc_getitem_multiindex2(self): | ||
# TODO(wesm): fix this | ||
pytest.skip('this test was being suppressed, ' | ||
'needs to be fixed') | ||
tup = zip(*[['a', 'a', 'b', 'b'], ['x', 'y', 'x', 'y']]) | ||
index = MultiIndex.from_tuples(tup) | ||
df = DataFrame(np.random.randn(4, 4), index=index) | ||
rs = df.iloc[[2, 3]] | ||
xp = df.xs('b', drop_level=False) | ||
tm.assert_frame_equal(rs, xp) | ||
|
||
arr = np.random.randn(3, 3) | ||
df = DataFrame(arr, columns=[[2, 2, 4], [6, 8, 10]], | ||
|
||
@pytest.mark.filterwarnings("ignore:\\n.ix:DeprecationWarning") | ||
def test_iloc_getitem_multiindex(): | ||
mi_labels = DataFrame(np.random.randn(4, 3), | ||
columns=[['i', 'i', 'j'], ['A', 'A', 'B']], | ||
index=[['i', 'i', 'j', 'k'], | ||
['X', 'X', 'Y', 'Y']]) | ||
|
||
mi_int = DataFrame(np.random.randn(3, 3), | ||
columns=[[2, 2, 4], [6, 8, 10]], | ||
index=[[4, 4, 8], [8, 10, 12]]) | ||
|
||
rs = df.iloc[2] | ||
xp = Series(arr[2], index=df.columns) | ||
tm.assert_series_equal(rs, xp) | ||
|
||
rs = df.iloc[:, 2] | ||
xp = Series(arr[:, 2], index=df.index) | ||
tm.assert_series_equal(rs, xp) | ||
|
||
rs = df.iloc[2, 2] | ||
xp = df.values[2, 2] | ||
assert rs == xp | ||
|
||
# for multiple items | ||
# GH 5528 | ||
rs = df.iloc[[0, 1]] | ||
xp = df.xs(4, drop_level=False) | ||
tm.assert_frame_equal(rs, xp) | ||
|
||
tup = zip(*[['a', 'a', 'b', 'b'], ['x', 'y', 'x', 'y']]) | ||
index = MultiIndex.from_tuples(tup) | ||
df = DataFrame(np.random.randn(4, 4), index=index) | ||
rs = df.iloc[[2, 3]] | ||
xp = df.xs('b', drop_level=False) | ||
tm.assert_frame_equal(rs, xp) | ||
|
||
def test_iloc_getitem_multiindex(self): | ||
mi_labels = DataFrame(np.random.randn(4, 3), | ||
columns=[['i', 'i', 'j'], ['A', 'A', 'B']], | ||
index=[['i', 'i', 'j', 'k'], | ||
['X', 'X', 'Y', 'Y']]) | ||
|
||
mi_int = DataFrame(np.random.randn(3, 3), | ||
columns=[[2, 2, 4], [6, 8, 10]], | ||
index=[[4, 4, 8], [8, 10, 12]]) | ||
|
||
# the first row | ||
rs = mi_int.iloc[0] | ||
with catch_warnings(record=True): | ||
xp = mi_int.ix[4].ix[8] | ||
tm.assert_series_equal(rs, xp, check_names=False) | ||
assert rs.name == (4, 8) | ||
assert xp.name == 8 | ||
|
||
# 2nd (last) columns | ||
rs = mi_int.iloc[:, 2] | ||
with catch_warnings(record=True): | ||
xp = mi_int.ix[:, 2] | ||
tm.assert_series_equal(rs, xp) | ||
|
||
# corner column | ||
rs = mi_int.iloc[2, 2] | ||
with catch_warnings(record=True): | ||
# First level is int - so use .loc rather than .ix (GH 21593) | ||
xp = mi_int.loc[(8, 12), (4, 10)] | ||
assert rs == xp | ||
|
||
# this is basically regular indexing | ||
rs = mi_labels.iloc[2, 2] | ||
with catch_warnings(record=True): | ||
xp = mi_labels.ix['j'].ix[:, 'j'].ix[0, 0] | ||
assert rs == xp | ||
|
||
def test_frame_getitem_setitem_slice( | ||
self, multiindex_dataframe_random_data): | ||
frame = multiindex_dataframe_random_data | ||
# getitem | ||
result = frame.iloc[:4] | ||
expected = frame[:4] | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# setitem | ||
cp = frame.copy() | ||
cp.iloc[:4] = 0 | ||
|
||
assert (cp.values[:4] == 0).all() | ||
assert (cp.values[4:] != 0).all() | ||
|
||
def test_indexing_ambiguity_bug_1678(self): | ||
columns = MultiIndex.from_tuples([('Ohio', 'Green'), ('Ohio', 'Red'), ( | ||
'Colorado', 'Green')]) | ||
index = MultiIndex.from_tuples([('a', 1), ('a', 2), ('b', 1), ('b', 2) | ||
]) | ||
|
||
frame = DataFrame(np.arange(12).reshape((4, 3)), index=index, | ||
columns=columns) | ||
|
||
result = frame.iloc[:, 1] | ||
exp = frame.loc[:, ('Ohio', 'Red')] | ||
assert isinstance(result, Series) | ||
tm.assert_series_equal(result, exp) | ||
|
||
def test_iloc_mi(self): | ||
# GH 13797 | ||
# Test if iloc can handle integer locations in MultiIndexed DataFrame | ||
|
||
data = [['str00', 'str01'], ['str10', 'str11'], ['str20', 'srt21'], | ||
['str30', 'str31'], ['str40', 'str41']] | ||
|
||
mi = MultiIndex.from_tuples( | ||
[('CC', 'A'), ('CC', 'B'), ('CC', 'B'), ('BB', 'a'), ('BB', 'b')]) | ||
|
||
expected = DataFrame(data) | ||
df_mi = DataFrame(data, index=mi) | ||
|
||
result = DataFrame([[df_mi.iloc[r, c] for c in range(2)] | ||
for r in range(5)]) | ||
|
||
tm.assert_frame_equal(result, expected) | ||
# the first row | ||
rs = mi_int.iloc[0] | ||
xp = mi_int.ix[4].ix[8] | ||
tm.assert_series_equal(rs, xp, check_names=False) | ||
assert rs.name == (4, 8) | ||
assert xp.name == 8 | ||
|
||
# 2nd (last) columns | ||
rs = mi_int.iloc[:, 2] | ||
xp = mi_int.ix[:, 2] | ||
tm.assert_series_equal(rs, xp) | ||
|
||
# corner column | ||
rs = mi_int.iloc[2, 2] | ||
# First level is int - so use .loc rather than .ix (GH 21593) | ||
xp = mi_int.loc[(8, 12), (4, 10)] | ||
assert rs == xp | ||
|
||
# this is basically regular indexing | ||
rs = mi_labels.iloc[2, 2] | ||
xp = mi_labels.ix['j'].ix[:, 'j'].ix[0, 0] | ||
assert rs == xp | ||
|
||
|
||
def test_frame_getitem_setitem_slice(multiindex_dataframe_random_data): | ||
frame = multiindex_dataframe_random_data | ||
# getitem | ||
result = frame.iloc[:4] | ||
expected = frame[:4] | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# setitem | ||
cp = frame.copy() | ||
cp.iloc[:4] = 0 | ||
|
||
assert (cp.values[:4] == 0).all() | ||
assert (cp.values[4:] != 0).all() | ||
|
||
|
||
def test_indexing_ambiguity_bug_1678(): | ||
columns = MultiIndex.from_tuples([('Ohio', 'Green'), ('Ohio', 'Red'), ( | ||
'Colorado', 'Green')]) | ||
index = MultiIndex.from_tuples([('a', 1), ('a', 2), ('b', 1), ('b', 2)]) | ||
|
||
frame = DataFrame(np.arange(12).reshape((4, 3)), index=index, | ||
columns=columns) | ||
|
||
result = frame.iloc[:, 1] | ||
exp = frame.loc[:, ('Ohio', 'Red')] | ||
assert isinstance(result, Series) | ||
tm.assert_series_equal(result, exp) | ||
|
||
|
||
def test_iloc_mi(): | ||
# GH 13797 | ||
# Test if iloc can handle integer locations in MultiIndexed DataFrame | ||
|
||
data = [['str00', 'str01'], ['str10', 'str11'], ['str20', 'srt21'], | ||
['str30', 'str31'], ['str40', 'str41']] | ||
|
||
mi = MultiIndex.from_tuples( | ||
[('CC', 'A'), ('CC', 'B'), ('CC', 'B'), ('BB', 'a'), ('BB', 'b')]) | ||
|
||
expected = DataFrame(data) | ||
df_mi = DataFrame(data, index=mi) | ||
|
||
result = DataFrame([[df_mi.iloc[r, c] for c in range(2)] | ||
for r in range(5)]) | ||
|
||
tm.assert_frame_equal(result, expected) |
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 |
---|---|---|
@@ -1,27 +1,21 @@ | ||
from warnings import catch_warnings, simplefilter | ||
|
||
import pytest | ||
|
||
from pandas.compat import lrange | ||
|
||
|
||
@pytest.mark.filterwarnings("ignore:\\n.ix:DeprecationWarning") | ||
class TestMultiIndexIx(object): | ||
|
||
def test_frame_setitem_ix(self, multiindex_dataframe_random_data): | ||
frame = multiindex_dataframe_random_data | ||
frame.loc[('bar', 'two'), 'B'] = 5 | ||
assert frame.loc[('bar', 'two'), 'B'] == 5 | ||
def test_frame_setitem_ix(multiindex_dataframe_random_data): | ||
frame = multiindex_dataframe_random_data | ||
frame.loc[('bar', 'two'), 'B'] = 5 | ||
assert frame.loc[('bar', 'two'), 'B'] == 5 | ||
|
||
# with integer labels | ||
df = frame.copy() | ||
df.columns = lrange(3) | ||
df.loc[('bar', 'two'), 1] = 7 | ||
assert df.loc[('bar', 'two'), 1] == 7 | ||
# with integer labels | ||
df = frame.copy() | ||
df.columns = lrange(3) | ||
df.loc[('bar', 'two'), 1] = 7 | ||
assert df.loc[('bar', 'two'), 1] == 7 | ||
|
||
with catch_warnings(record=True): | ||
simplefilter("ignore", DeprecationWarning) | ||
df = frame.copy() | ||
df.columns = lrange(3) | ||
df.ix[('bar', 'two'), 1] = 7 | ||
assert df.loc[('bar', 'two'), 1] == 7 | ||
df = frame.copy() | ||
df.columns = lrange(3) | ||
df.ix[('bar', 'two'), 1] = 7 | ||
assert df.loc[('bar', 'two'), 1] == 7 |
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.
we should fix this :>
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.
xref #24272