|
1 |
| -from warnings import catch_warnings |
2 |
| - |
3 | 1 | import numpy as np
|
4 | 2 | import pytest
|
5 | 3 |
|
6 | 4 | from pandas import DataFrame, MultiIndex, Series
|
7 | 5 | from pandas.util import testing as tm
|
8 | 6 |
|
9 | 7 |
|
10 |
| -@pytest.mark.filterwarnings("ignore:\\n.ix:DeprecationWarning") |
11 |
| -class TestMultiIndexIloc(object): |
12 |
| - |
13 |
| - def test_iloc_getitem_multiindex2(self): |
14 |
| - # TODO(wesm): fix this |
15 |
| - pytest.skip('this test was being suppressed, ' |
16 |
| - 'needs to be fixed') |
17 |
| - |
18 |
| - arr = np.random.randn(3, 3) |
19 |
| - df = DataFrame(arr, columns=[[2, 2, 4], [6, 8, 10]], |
20 |
| - index=[[4, 4, 8], [8, 10, 12]]) |
21 |
| - |
22 |
| - rs = df.iloc[2] |
23 |
| - xp = Series(arr[2], index=df.columns) |
24 |
| - tm.assert_series_equal(rs, xp) |
25 |
| - |
26 |
| - rs = df.iloc[:, 2] |
27 |
| - xp = Series(arr[:, 2], index=df.index) |
28 |
| - tm.assert_series_equal(rs, xp) |
29 |
| - |
30 |
| - rs = df.iloc[2, 2] |
31 |
| - xp = df.values[2, 2] |
32 |
| - assert rs == xp |
33 |
| - |
34 |
| - # for multiple items |
35 |
| - # GH 5528 |
36 |
| - rs = df.iloc[[0, 1]] |
37 |
| - xp = df.xs(4, drop_level=False) |
38 |
| - tm.assert_frame_equal(rs, xp) |
39 |
| - |
40 |
| - tup = zip(*[['a', 'a', 'b', 'b'], ['x', 'y', 'x', 'y']]) |
41 |
| - index = MultiIndex.from_tuples(tup) |
42 |
| - df = DataFrame(np.random.randn(4, 4), index=index) |
43 |
| - rs = df.iloc[[2, 3]] |
44 |
| - xp = df.xs('b', drop_level=False) |
45 |
| - tm.assert_frame_equal(rs, xp) |
46 |
| - |
47 |
| - def test_iloc_getitem_multiindex(self): |
48 |
| - mi_labels = DataFrame(np.random.randn(4, 3), |
49 |
| - columns=[['i', 'i', 'j'], ['A', 'A', 'B']], |
50 |
| - index=[['i', 'i', 'j', 'k'], |
51 |
| - ['X', 'X', 'Y', 'Y']]) |
52 |
| - |
53 |
| - mi_int = DataFrame(np.random.randn(3, 3), |
54 |
| - columns=[[2, 2, 4], [6, 8, 10]], |
55 |
| - index=[[4, 4, 8], [8, 10, 12]]) |
56 |
| - |
57 |
| - # the first row |
58 |
| - rs = mi_int.iloc[0] |
59 |
| - with catch_warnings(record=True): |
60 |
| - xp = mi_int.ix[4].ix[8] |
61 |
| - tm.assert_series_equal(rs, xp, check_names=False) |
62 |
| - assert rs.name == (4, 8) |
63 |
| - assert xp.name == 8 |
64 |
| - |
65 |
| - # 2nd (last) columns |
66 |
| - rs = mi_int.iloc[:, 2] |
67 |
| - with catch_warnings(record=True): |
68 |
| - xp = mi_int.ix[:, 2] |
69 |
| - tm.assert_series_equal(rs, xp) |
70 |
| - |
71 |
| - # corner column |
72 |
| - rs = mi_int.iloc[2, 2] |
73 |
| - with catch_warnings(record=True): |
74 |
| - # First level is int - so use .loc rather than .ix (GH 21593) |
75 |
| - xp = mi_int.loc[(8, 12), (4, 10)] |
76 |
| - assert rs == xp |
77 |
| - |
78 |
| - # this is basically regular indexing |
79 |
| - rs = mi_labels.iloc[2, 2] |
80 |
| - with catch_warnings(record=True): |
81 |
| - xp = mi_labels.ix['j'].ix[:, 'j'].ix[0, 0] |
82 |
| - assert rs == xp |
83 |
| - |
84 |
| - def test_frame_getitem_setitem_slice( |
85 |
| - self, multiindex_dataframe_random_data): |
86 |
| - frame = multiindex_dataframe_random_data |
87 |
| - # getitem |
88 |
| - result = frame.iloc[:4] |
89 |
| - expected = frame[:4] |
90 |
| - tm.assert_frame_equal(result, expected) |
91 |
| - |
92 |
| - # setitem |
93 |
| - cp = frame.copy() |
94 |
| - cp.iloc[:4] = 0 |
95 |
| - |
96 |
| - assert (cp.values[:4] == 0).all() |
97 |
| - assert (cp.values[4:] != 0).all() |
98 |
| - |
99 |
| - def test_indexing_ambiguity_bug_1678(self): |
100 |
| - columns = MultiIndex.from_tuples([('Ohio', 'Green'), ('Ohio', 'Red'), ( |
101 |
| - 'Colorado', 'Green')]) |
102 |
| - index = MultiIndex.from_tuples([('a', 1), ('a', 2), ('b', 1), ('b', 2) |
103 |
| - ]) |
104 |
| - |
105 |
| - frame = DataFrame(np.arange(12).reshape((4, 3)), index=index, |
106 |
| - columns=columns) |
107 |
| - |
108 |
| - result = frame.iloc[:, 1] |
109 |
| - exp = frame.loc[:, ('Ohio', 'Red')] |
110 |
| - assert isinstance(result, Series) |
111 |
| - tm.assert_series_equal(result, exp) |
112 |
| - |
113 |
| - def test_iloc_mi(self): |
114 |
| - # GH 13797 |
115 |
| - # Test if iloc can handle integer locations in MultiIndexed DataFrame |
116 |
| - |
117 |
| - data = [['str00', 'str01'], ['str10', 'str11'], ['str20', 'srt21'], |
118 |
| - ['str30', 'str31'], ['str40', 'str41']] |
119 |
| - |
120 |
| - mi = MultiIndex.from_tuples( |
121 |
| - [('CC', 'A'), ('CC', 'B'), ('CC', 'B'), ('BB', 'a'), ('BB', 'b')]) |
122 |
| - |
123 |
| - expected = DataFrame(data) |
124 |
| - df_mi = DataFrame(data, index=mi) |
125 |
| - |
126 |
| - result = DataFrame([[df_mi.iloc[r, c] for c in range(2)] |
127 |
| - for r in range(5)]) |
128 |
| - |
129 |
| - tm.assert_frame_equal(result, expected) |
| 8 | +@pytest.fixture |
| 9 | +def simple_multiindex_dataframe(): |
| 10 | + """ |
| 11 | + Factory function to create simple 3 x 3 dataframe with |
| 12 | + both columns and row MultiIndex using supplied data or |
| 13 | + random data by default. |
| 14 | + """ |
| 15 | + def _simple_multiindex_dataframe(data=None): |
| 16 | + if data is None: |
| 17 | + data = np.random.randn(3, 3) |
| 18 | + return DataFrame(data, columns=[[2, 2, 4], [6, 8, 10]], |
| 19 | + index=[[4, 4, 8], [8, 10, 12]]) |
| 20 | + return _simple_multiindex_dataframe |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize('indexer, expected', [ |
| 24 | + (lambda df: df.iloc[0], |
| 25 | + lambda arr: Series(arr[0], index=[[2, 2, 4], [6, 8, 10]], name=(4, 8))), |
| 26 | + (lambda df: df.iloc[2], |
| 27 | + lambda arr: Series(arr[2], index=[[2, 2, 4], [6, 8, 10]], name=(8, 12))), |
| 28 | + (lambda df: df.iloc[:, 2], |
| 29 | + lambda arr: Series( |
| 30 | + arr[:, 2], index=[[4, 4, 8], [8, 10, 12]], name=(4, 10))) |
| 31 | +]) |
| 32 | +def test_iloc_returns_series(indexer, expected, simple_multiindex_dataframe): |
| 33 | + arr = np.random.randn(3, 3) |
| 34 | + df = simple_multiindex_dataframe(arr) |
| 35 | + result = indexer(df) |
| 36 | + expected = expected(arr) |
| 37 | + tm.assert_series_equal(result, expected) |
| 38 | + |
| 39 | + |
| 40 | +def test_iloc_returns_dataframe(simple_multiindex_dataframe): |
| 41 | + df = simple_multiindex_dataframe() |
| 42 | + result = df.iloc[[0, 1]] |
| 43 | + expected = df.xs(4, drop_level=False) |
| 44 | + tm.assert_frame_equal(result, expected) |
| 45 | + |
| 46 | + |
| 47 | +def test_iloc_returns_scalar(simple_multiindex_dataframe): |
| 48 | + arr = np.random.randn(3, 3) |
| 49 | + df = simple_multiindex_dataframe(arr) |
| 50 | + result = df.iloc[2, 2] |
| 51 | + expected = arr[2, 2] |
| 52 | + assert result == expected |
| 53 | + |
| 54 | + |
| 55 | +def test_iloc_getitem_multiple_items(): |
| 56 | + # GH 5528 |
| 57 | + tup = zip(*[['a', 'a', 'b', 'b'], ['x', 'y', 'x', 'y']]) |
| 58 | + index = MultiIndex.from_tuples(tup) |
| 59 | + df = DataFrame(np.random.randn(4, 4), index=index) |
| 60 | + result = df.iloc[[2, 3]] |
| 61 | + expected = df.xs('b', drop_level=False) |
| 62 | + tm.assert_frame_equal(result, expected) |
| 63 | + |
| 64 | + |
| 65 | +def test_iloc_getitem_labels(): |
| 66 | + # this is basically regular indexing |
| 67 | + arr = np.random.randn(4, 3) |
| 68 | + df = DataFrame(arr, |
| 69 | + columns=[['i', 'i', 'j'], ['A', 'A', 'B']], |
| 70 | + index=[['i', 'i', 'j', 'k'], ['X', 'X', 'Y', 'Y']]) |
| 71 | + result = df.iloc[2, 2] |
| 72 | + expected = arr[2, 2] |
| 73 | + assert result == expected |
| 74 | + |
| 75 | + |
| 76 | +def test_frame_getitem_slice(multiindex_dataframe_random_data): |
| 77 | + df = multiindex_dataframe_random_data |
| 78 | + result = df.iloc[:4] |
| 79 | + expected = df[:4] |
| 80 | + tm.assert_frame_equal(result, expected) |
| 81 | + |
| 82 | + |
| 83 | +def test_frame_setitem_slice(multiindex_dataframe_random_data): |
| 84 | + df = multiindex_dataframe_random_data |
| 85 | + df.iloc[:4] = 0 |
| 86 | + |
| 87 | + assert (df.values[:4] == 0).all() |
| 88 | + assert (df.values[4:] != 0).all() |
| 89 | + |
| 90 | + |
| 91 | +def test_indexing_ambiguity_bug_1678(): |
| 92 | + # GH 1678 |
| 93 | + columns = MultiIndex.from_tuples( |
| 94 | + [('Ohio', 'Green'), ('Ohio', 'Red'), ('Colorado', 'Green')]) |
| 95 | + index = MultiIndex.from_tuples([('a', 1), ('a', 2), ('b', 1), ('b', 2)]) |
| 96 | + |
| 97 | + df = DataFrame(np.arange(12).reshape((4, 3)), index=index, columns=columns) |
| 98 | + |
| 99 | + result = df.iloc[:, 1] |
| 100 | + expected = df.loc[:, ('Ohio', 'Red')] |
| 101 | + tm.assert_series_equal(result, expected) |
| 102 | + |
| 103 | + |
| 104 | +def test_iloc_integer_locations(): |
| 105 | + # GH 13797 |
| 106 | + data = [['str00', 'str01'], ['str10', 'str11'], ['str20', 'srt21'], |
| 107 | + ['str30', 'str31'], ['str40', 'str41']] |
| 108 | + |
| 109 | + index = MultiIndex.from_tuples( |
| 110 | + [('CC', 'A'), ('CC', 'B'), ('CC', 'B'), ('BB', 'a'), ('BB', 'b')]) |
| 111 | + |
| 112 | + expected = DataFrame(data) |
| 113 | + df = DataFrame(data, index=index) |
| 114 | + |
| 115 | + result = DataFrame([[df.iloc[r, c] for c in range(2)] for r in range(5)]) |
| 116 | + |
| 117 | + tm.assert_frame_equal(result, expected) |
0 commit comments