test_join.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. import numpy as np
  3. import pytest
  4. import pandas as pd
  5. from pandas import Index, MultiIndex
  6. import pandas.util.testing as tm
  7. @pytest.mark.parametrize('other', [
  8. Index(['three', 'one', 'two']),
  9. Index(['one']),
  10. Index(['one', 'three']),
  11. ])
  12. def test_join_level(idx, other, join_type):
  13. join_index, lidx, ridx = other.join(idx, how=join_type,
  14. level='second',
  15. return_indexers=True)
  16. exp_level = other.join(idx.levels[1], how=join_type)
  17. assert join_index.levels[0].equals(idx.levels[0])
  18. assert join_index.levels[1].equals(exp_level)
  19. # pare down levels
  20. mask = np.array(
  21. [x[1] in exp_level for x in idx], dtype=bool)
  22. exp_values = idx.values[mask]
  23. tm.assert_numpy_array_equal(join_index.values, exp_values)
  24. if join_type in ('outer', 'inner'):
  25. join_index2, ridx2, lidx2 = \
  26. idx.join(other, how=join_type, level='second',
  27. return_indexers=True)
  28. assert join_index.equals(join_index2)
  29. tm.assert_numpy_array_equal(lidx, lidx2)
  30. tm.assert_numpy_array_equal(ridx, ridx2)
  31. tm.assert_numpy_array_equal(join_index2.values, exp_values)
  32. def test_join_level_corner_case(idx):
  33. # some corner cases
  34. index = Index(['three', 'one', 'two'])
  35. result = index.join(idx, level='second')
  36. assert isinstance(result, MultiIndex)
  37. with pytest.raises(TypeError, match="Join.*MultiIndex.*ambiguous"):
  38. idx.join(idx, level=1)
  39. def test_join_self(idx, join_type):
  40. joined = idx.join(idx, how=join_type)
  41. assert idx is joined
  42. def test_join_multi():
  43. # GH 10665
  44. midx = pd.MultiIndex.from_product(
  45. [np.arange(4), np.arange(4)], names=['a', 'b'])
  46. idx = pd.Index([1, 2, 5], name='b')
  47. # inner
  48. jidx, lidx, ridx = midx.join(idx, how='inner', return_indexers=True)
  49. exp_idx = pd.MultiIndex.from_product(
  50. [np.arange(4), [1, 2]], names=['a', 'b'])
  51. exp_lidx = np.array([1, 2, 5, 6, 9, 10, 13, 14], dtype=np.intp)
  52. exp_ridx = np.array([0, 1, 0, 1, 0, 1, 0, 1], dtype=np.intp)
  53. tm.assert_index_equal(jidx, exp_idx)
  54. tm.assert_numpy_array_equal(lidx, exp_lidx)
  55. tm.assert_numpy_array_equal(ridx, exp_ridx)
  56. # flip
  57. jidx, ridx, lidx = idx.join(midx, how='inner', return_indexers=True)
  58. tm.assert_index_equal(jidx, exp_idx)
  59. tm.assert_numpy_array_equal(lidx, exp_lidx)
  60. tm.assert_numpy_array_equal(ridx, exp_ridx)
  61. # keep MultiIndex
  62. jidx, lidx, ridx = midx.join(idx, how='left', return_indexers=True)
  63. exp_ridx = np.array([-1, 0, 1, -1, -1, 0, 1, -1, -1, 0, 1, -1, -1, 0,
  64. 1, -1], dtype=np.intp)
  65. tm.assert_index_equal(jidx, midx)
  66. assert lidx is None
  67. tm.assert_numpy_array_equal(ridx, exp_ridx)
  68. # flip
  69. jidx, ridx, lidx = idx.join(midx, how='right', return_indexers=True)
  70. tm.assert_index_equal(jidx, midx)
  71. assert lidx is None
  72. tm.assert_numpy_array_equal(ridx, exp_ridx)
  73. def test_join_self_unique(idx, join_type):
  74. if idx.is_unique:
  75. joined = idx.join(idx, how=join_type)
  76. assert (idx == joined).all()