test_set_ops.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from numpy.random import randn
  2. from pandas import DataFrame, MultiIndex, Series
  3. from pandas.util import testing as tm
  4. class TestMultiIndexSetOps(object):
  5. def test_multiindex_symmetric_difference(self):
  6. # GH 13490
  7. idx = MultiIndex.from_product([['a', 'b'], ['A', 'B']],
  8. names=['a', 'b'])
  9. result = idx ^ idx
  10. assert result.names == idx.names
  11. idx2 = idx.copy().rename(['A', 'B'])
  12. result = idx ^ idx2
  13. assert result.names == [None, None]
  14. def test_mixed_depth_insert(self):
  15. arrays = [['a', 'top', 'top', 'routine1', 'routine1', 'routine2'],
  16. ['', 'OD', 'OD', 'result1', 'result2', 'result1'],
  17. ['', 'wx', 'wy', '', '', '']]
  18. tuples = sorted(zip(*arrays))
  19. index = MultiIndex.from_tuples(tuples)
  20. df = DataFrame(randn(4, 6), columns=index)
  21. result = df.copy()
  22. expected = df.copy()
  23. result['b'] = [1, 2, 3, 4]
  24. expected['b', '', ''] = [1, 2, 3, 4]
  25. tm.assert_frame_equal(result, expected)
  26. def test_dataframe_insert_column_all_na(self):
  27. # GH #1534
  28. mix = MultiIndex.from_tuples([('1a', '2a'), ('1a', '2b'), ('1a', '2c')
  29. ])
  30. df = DataFrame([[1, 2], [3, 4], [5, 6]], index=mix)
  31. s = Series({(1, 1): 1, (1, 2): 2})
  32. df['new'] = s
  33. assert df['new'].isna().all()