test_panel.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. from warnings import catch_warnings
  2. import numpy as np
  3. import pytest
  4. from pandas import DataFrame, Panel, date_range
  5. from pandas.util import testing as tm
  6. @pytest.mark.filterwarnings("ignore:\\nPanel:FutureWarning")
  7. class TestPanel(object):
  8. def test_iloc_getitem_panel(self):
  9. with catch_warnings(record=True):
  10. # GH 7189
  11. p = Panel(np.arange(4 * 3 * 2).reshape(4, 3, 2),
  12. items=['A', 'B', 'C', 'D'],
  13. major_axis=['a', 'b', 'c'],
  14. minor_axis=['one', 'two'])
  15. result = p.iloc[1]
  16. expected = p.loc['B']
  17. tm.assert_frame_equal(result, expected)
  18. result = p.iloc[1, 1]
  19. expected = p.loc['B', 'b']
  20. tm.assert_series_equal(result, expected)
  21. result = p.iloc[1, 1, 1]
  22. expected = p.loc['B', 'b', 'two']
  23. assert result == expected
  24. # slice
  25. result = p.iloc[1:3]
  26. expected = p.loc[['B', 'C']]
  27. tm.assert_panel_equal(result, expected)
  28. result = p.iloc[:, 0:2]
  29. expected = p.loc[:, ['a', 'b']]
  30. tm.assert_panel_equal(result, expected)
  31. # list of integers
  32. result = p.iloc[[0, 2]]
  33. expected = p.loc[['A', 'C']]
  34. tm.assert_panel_equal(result, expected)
  35. # neg indices
  36. result = p.iloc[[-1, 1], [-1, 1]]
  37. expected = p.loc[['D', 'B'], ['c', 'b']]
  38. tm.assert_panel_equal(result, expected)
  39. # dups indices
  40. result = p.iloc[[-1, -1, 1], [-1, 1]]
  41. expected = p.loc[['D', 'D', 'B'], ['c', 'b']]
  42. tm.assert_panel_equal(result, expected)
  43. # combined
  44. result = p.iloc[0, [True, True], [0, 1]]
  45. expected = p.loc['A', ['a', 'b'], ['one', 'two']]
  46. tm.assert_frame_equal(result, expected)
  47. # out-of-bounds exception
  48. with pytest.raises(IndexError):
  49. p.iloc[tuple([10, 5])]
  50. with pytest.raises(IndexError):
  51. p.iloc[0, [True, True], [0, 1, 2]]
  52. # trying to use a label
  53. with pytest.raises(ValueError):
  54. p.iloc[tuple(['j', 'D'])]
  55. # GH
  56. p = Panel(
  57. np.random.rand(4, 3, 2), items=['A', 'B', 'C', 'D'],
  58. major_axis=['U', 'V', 'W'], minor_axis=['X', 'Y'])
  59. expected = p['A']
  60. result = p.iloc[0, :, :]
  61. tm.assert_frame_equal(result, expected)
  62. result = p.iloc[0, [True, True, True], :]
  63. tm.assert_frame_equal(result, expected)
  64. result = p.iloc[0, [True, True, True], [0, 1]]
  65. tm.assert_frame_equal(result, expected)
  66. with pytest.raises(IndexError):
  67. p.iloc[0, [True, True, True], [0, 1, 2]]
  68. with pytest.raises(IndexError):
  69. p.iloc[0, [True, True, True], [2]]
  70. def test_iloc_panel_issue(self):
  71. with catch_warnings(record=True):
  72. # see gh-3617
  73. p = Panel(np.random.randn(4, 4, 4))
  74. assert p.iloc[:3, :3, :3].shape == (3, 3, 3)
  75. assert p.iloc[1, :3, :3].shape == (3, 3)
  76. assert p.iloc[:3, 1, :3].shape == (3, 3)
  77. assert p.iloc[:3, :3, 1].shape == (3, 3)
  78. assert p.iloc[1, 1, :3].shape == (3, )
  79. assert p.iloc[1, :3, 1].shape == (3, )
  80. assert p.iloc[:3, 1, 1].shape == (3, )
  81. @pytest.mark.filterwarnings("ignore:\\n.ix:DeprecationWarning")
  82. def test_panel_getitem(self):
  83. with catch_warnings(record=True):
  84. # GH4016, date selection returns a frame when a partial string
  85. # selection
  86. ind = date_range(start="2000", freq="D", periods=1000)
  87. df = DataFrame(
  88. np.random.randn(
  89. len(ind), 5), index=ind, columns=list('ABCDE'))
  90. panel = Panel({'frame_' + c: df for c in list('ABC')})
  91. test2 = panel.loc[:, "2002":"2002-12-31"]
  92. test1 = panel.loc[:, "2002"]
  93. tm.assert_panel_equal(test1, test2)
  94. # GH8710
  95. # multi-element getting with a list
  96. panel = tm.makePanel()
  97. expected = panel.iloc[[0, 1]]
  98. result = panel.loc[['ItemA', 'ItemB']]
  99. tm.assert_panel_equal(result, expected)
  100. result = panel.loc[['ItemA', 'ItemB'], :, :]
  101. tm.assert_panel_equal(result, expected)
  102. result = panel[['ItemA', 'ItemB']]
  103. tm.assert_panel_equal(result, expected)
  104. result = panel.loc['ItemA':'ItemB']
  105. tm.assert_panel_equal(result, expected)
  106. with catch_warnings(record=True):
  107. result = panel.ix[['ItemA', 'ItemB']]
  108. tm.assert_panel_equal(result, expected)
  109. # with an object-like
  110. # GH 9140
  111. class TestObject(object):
  112. def __str__(self):
  113. return "TestObject"
  114. obj = TestObject()
  115. p = Panel(np.random.randn(1, 5, 4), items=[obj],
  116. major_axis=date_range('1/1/2000', periods=5),
  117. minor_axis=['A', 'B', 'C', 'D'])
  118. expected = p.iloc[0]
  119. result = p[obj]
  120. tm.assert_frame_equal(result, expected)
  121. def test_panel_setitem(self):
  122. with catch_warnings(record=True):
  123. # GH 7763
  124. # loc and setitem have setting differences
  125. np.random.seed(0)
  126. index = range(3)
  127. columns = list('abc')
  128. panel = Panel({'A': DataFrame(np.random.randn(3, 3),
  129. index=index, columns=columns),
  130. 'B': DataFrame(np.random.randn(3, 3),
  131. index=index, columns=columns),
  132. 'C': DataFrame(np.random.randn(3, 3),
  133. index=index, columns=columns)})
  134. replace = DataFrame(np.eye(3, 3), index=range(3), columns=columns)
  135. expected = Panel({'A': replace, 'B': replace, 'C': replace})
  136. p = panel.copy()
  137. for idx in list('ABC'):
  138. p[idx] = replace
  139. tm.assert_panel_equal(p, expected)
  140. p = panel.copy()
  141. for idx in list('ABC'):
  142. p.loc[idx, :, :] = replace
  143. tm.assert_panel_equal(p, expected)
  144. def test_panel_assignment(self):
  145. with catch_warnings(record=True):
  146. # GH3777
  147. wp = Panel(np.random.randn(2, 5, 4), items=['Item1', 'Item2'],
  148. major_axis=date_range('1/1/2000', periods=5),
  149. minor_axis=['A', 'B', 'C', 'D'])
  150. wp2 = Panel(np.random.randn(2, 5, 4), items=['Item1', 'Item2'],
  151. major_axis=date_range('1/1/2000', periods=5),
  152. minor_axis=['A', 'B', 'C', 'D'])
  153. # TODO: unused?
  154. # expected = wp.loc[['Item1', 'Item2'], :, ['A', 'B']]
  155. with pytest.raises(NotImplementedError):
  156. wp.loc[['Item1', 'Item2'], :, ['A', 'B']] = wp2.loc[
  157. ['Item1', 'Item2'], :, ['A', 'B']]
  158. # to_assign = wp2.loc[['Item1', 'Item2'], :, ['A', 'B']]
  159. # wp.loc[['Item1', 'Item2'], :, ['A', 'B']] = to_assign
  160. # result = wp.loc[['Item1', 'Item2'], :, ['A', 'B']]
  161. # tm.assert_panel_equal(result,expected)