test_panel.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import numpy as np
  2. import pytest
  3. from pandas import DataFrame, MultiIndex, Panel, Series
  4. from pandas.util import testing as tm
  5. @pytest.mark.filterwarnings('ignore:\\nPanel:FutureWarning')
  6. class TestMultiIndexPanel(object):
  7. def test_iloc_getitem_panel_multiindex(self):
  8. # GH 7199
  9. # Panel with multi-index
  10. multi_index = MultiIndex.from_tuples([('ONE', 'one'),
  11. ('TWO', 'two'),
  12. ('THREE', 'three')],
  13. names=['UPPER', 'lower'])
  14. simple_index = [x[0] for x in multi_index]
  15. wd1 = Panel(items=['First', 'Second'],
  16. major_axis=['a', 'b', 'c', 'd'],
  17. minor_axis=multi_index)
  18. wd2 = Panel(items=['First', 'Second'],
  19. major_axis=['a', 'b', 'c', 'd'],
  20. minor_axis=simple_index)
  21. expected1 = wd1['First'].iloc[[True, True, True, False], [0, 2]]
  22. result1 = wd1.iloc[0, [True, True, True, False], [0, 2]] # WRONG
  23. tm.assert_frame_equal(result1, expected1)
  24. expected2 = wd2['First'].iloc[[True, True, True, False], [0, 2]]
  25. result2 = wd2.iloc[0, [True, True, True, False], [0, 2]]
  26. tm.assert_frame_equal(result2, expected2)
  27. expected1 = DataFrame(index=['a'], columns=multi_index,
  28. dtype='float64')
  29. result1 = wd1.iloc[0, [0], [0, 1, 2]]
  30. tm.assert_frame_equal(result1, expected1)
  31. expected2 = DataFrame(index=['a'], columns=simple_index,
  32. dtype='float64')
  33. result2 = wd2.iloc[0, [0], [0, 1, 2]]
  34. tm.assert_frame_equal(result2, expected2)
  35. # GH 7516
  36. mi = MultiIndex.from_tuples([(0, 'x'), (1, 'y'), (2, 'z')])
  37. p = Panel(np.arange(3 * 3 * 3, dtype='int64').reshape(3, 3, 3),
  38. items=['a', 'b', 'c'], major_axis=mi,
  39. minor_axis=['u', 'v', 'w'])
  40. result = p.iloc[:, 1, 0]
  41. expected = Series([3, 12, 21], index=['a', 'b', 'c'], name='u')
  42. tm.assert_series_equal(result, expected)
  43. result = p.loc[:, (1, 'y'), 'u']
  44. tm.assert_series_equal(result, expected)
  45. def test_panel_setitem_with_multiindex(self):
  46. # 10360
  47. # failing with a multi-index
  48. arr = np.array([[[1, 2, 3], [0, 0, 0]],
  49. [[0, 0, 0], [0, 0, 0]]],
  50. dtype=np.float64)
  51. # reg index
  52. axes = dict(items=['A', 'B'], major_axis=[0, 1],
  53. minor_axis=['X', 'Y', 'Z'])
  54. p1 = Panel(0., **axes)
  55. p1.iloc[0, 0, :] = [1, 2, 3]
  56. expected = Panel(arr, **axes)
  57. tm.assert_panel_equal(p1, expected)
  58. # multi-indexes
  59. axes['items'] = MultiIndex.from_tuples(
  60. [('A', 'a'), ('B', 'b')])
  61. p2 = Panel(0., **axes)
  62. p2.iloc[0, 0, :] = [1, 2, 3]
  63. expected = Panel(arr, **axes)
  64. tm.assert_panel_equal(p2, expected)
  65. axes['major_axis'] = MultiIndex.from_tuples(
  66. [('A', 1), ('A', 2)])
  67. p3 = Panel(0., **axes)
  68. p3.iloc[0, 0, :] = [1, 2, 3]
  69. expected = Panel(arr, **axes)
  70. tm.assert_panel_equal(p3, expected)
  71. axes['minor_axis'] = MultiIndex.from_product(
  72. [['X'], range(3)])
  73. p4 = Panel(0., **axes)
  74. p4.iloc[0, 0, :] = [1, 2, 3]
  75. expected = Panel(arr, **axes)
  76. tm.assert_panel_equal(p4, expected)
  77. arr = np.array(
  78. [[[1, 0, 0], [2, 0, 0]], [[0, 0, 0], [0, 0, 0]]],
  79. dtype=np.float64)
  80. p5 = Panel(0., **axes)
  81. p5.iloc[0, :, 0] = [1, 2]
  82. expected = Panel(arr, **axes)
  83. tm.assert_panel_equal(p5, expected)