forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_panel.py
104 lines (76 loc) · 3.26 KB
/
test_panel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from warnings import catch_warnings
import numpy as np
import pytest
from pandas import Panel, date_range
from pandas.util import testing as tm
@pytest.mark.filterwarnings("ignore:\\nPanel:FutureWarning")
class TestPanel(object):
def test_iloc_getitem_panel(self):
with catch_warnings(record=True):
# GH 7189
p = Panel(np.arange(4 * 3 * 2).reshape(4, 3, 2),
items=['A', 'B', 'C', 'D'],
major_axis=['a', 'b', 'c'],
minor_axis=['one', 'two'])
result = p.iloc[1]
expected = p.loc['B']
tm.assert_frame_equal(result, expected)
result = p.iloc[1, 1]
expected = p.loc['B', 'b']
tm.assert_series_equal(result, expected)
result = p.iloc[1, 1, 1]
expected = p.loc['B', 'b', 'two']
assert result == expected
# combined
result = p.iloc[0, [True, True], [0, 1]]
expected = p.loc['A', ['a', 'b'], ['one', 'two']]
tm.assert_frame_equal(result, expected)
# out-of-bounds exception
with pytest.raises(IndexError):
p.iloc[tuple([10, 5])]
with pytest.raises(IndexError):
p.iloc[0, [True, True], [0, 1, 2]]
# trying to use a label
with pytest.raises(ValueError):
p.iloc[tuple(['j', 'D'])]
# GH
p = Panel(
np.random.rand(4, 3, 2), items=['A', 'B', 'C', 'D'],
major_axis=['U', 'V', 'W'], minor_axis=['X', 'Y'])
expected = p['A']
result = p.iloc[0, :, :]
tm.assert_frame_equal(result, expected)
result = p.iloc[0, [True, True, True], :]
tm.assert_frame_equal(result, expected)
result = p.iloc[0, [True, True, True], [0, 1]]
tm.assert_frame_equal(result, expected)
with pytest.raises(IndexError):
p.iloc[0, [True, True, True], [0, 1, 2]]
with pytest.raises(IndexError):
p.iloc[0, [True, True, True], [2]]
def test_iloc_panel_issue(self):
with catch_warnings(record=True):
# see gh-3617
p = Panel(np.random.randn(4, 4, 4))
assert p.iloc[:3, :3, :3].shape == (3, 3, 3)
assert p.iloc[1, :3, :3].shape == (3, 3)
assert p.iloc[:3, 1, :3].shape == (3, 3)
assert p.iloc[:3, :3, 1].shape == (3, 3)
assert p.iloc[1, 1, :3].shape == (3, )
assert p.iloc[1, :3, 1].shape == (3, )
assert p.iloc[:3, 1, 1].shape == (3, )
@pytest.mark.filterwarnings("ignore:\\n.ix:DeprecationWarning")
def test_panel_getitem(self):
with catch_warnings(record=True):
# with an object-like
# GH 9140
class TestObject(object):
def __str__(self):
return "TestObject"
obj = TestObject()
p = Panel(np.random.randn(1, 5, 4), items=[obj],
major_axis=date_range('1/1/2000', periods=5),
minor_axis=['A', 'B', 'C', 'D'])
expected = p.iloc[0]
result = p[obj]
tm.assert_frame_equal(result, expected)