test_merge_cross.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import pytest
  2. from pandas import DataFrame
  3. import pandas._testing as tm
  4. from pandas.core.reshape.merge import (
  5. MergeError,
  6. merge,
  7. )
  8. @pytest.mark.parametrize(
  9. ("input_col", "output_cols"), [("b", ["a", "b"]), ("a", ["a_x", "a_y"])]
  10. )
  11. def test_merge_cross(input_col, output_cols):
  12. # GH#5401
  13. left = DataFrame({"a": [1, 3]})
  14. right = DataFrame({input_col: [3, 4]})
  15. left_copy = left.copy()
  16. right_copy = right.copy()
  17. result = merge(left, right, how="cross")
  18. expected = DataFrame({output_cols[0]: [1, 1, 3, 3], output_cols[1]: [3, 4, 3, 4]})
  19. tm.assert_frame_equal(result, expected)
  20. tm.assert_frame_equal(left, left_copy)
  21. tm.assert_frame_equal(right, right_copy)
  22. @pytest.mark.parametrize(
  23. "kwargs",
  24. [
  25. {"left_index": True},
  26. {"right_index": True},
  27. {"on": "a"},
  28. {"left_on": "a"},
  29. {"right_on": "b"},
  30. ],
  31. )
  32. def test_merge_cross_error_reporting(kwargs):
  33. # GH#5401
  34. left = DataFrame({"a": [1, 3]})
  35. right = DataFrame({"b": [3, 4]})
  36. msg = (
  37. "Can not pass on, right_on, left_on or set right_index=True or "
  38. "left_index=True"
  39. )
  40. with pytest.raises(MergeError, match=msg):
  41. merge(left, right, how="cross", **kwargs)
  42. def test_merge_cross_mixed_dtypes():
  43. # GH#5401
  44. left = DataFrame(["a", "b", "c"], columns=["A"])
  45. right = DataFrame(range(2), columns=["B"])
  46. result = merge(left, right, how="cross")
  47. expected = DataFrame({"A": ["a", "a", "b", "b", "c", "c"], "B": [0, 1, 0, 1, 0, 1]})
  48. tm.assert_frame_equal(result, expected)
  49. def test_merge_cross_more_than_one_column():
  50. # GH#5401
  51. left = DataFrame({"A": list("ab"), "B": [2, 1]})
  52. right = DataFrame({"C": range(2), "D": range(4, 6)})
  53. result = merge(left, right, how="cross")
  54. expected = DataFrame(
  55. {
  56. "A": ["a", "a", "b", "b"],
  57. "B": [2, 2, 1, 1],
  58. "C": [0, 1, 0, 1],
  59. "D": [4, 5, 4, 5],
  60. }
  61. )
  62. tm.assert_frame_equal(result, expected)
  63. def test_merge_cross_null_values(nulls_fixture):
  64. # GH#5401
  65. left = DataFrame({"a": [1, nulls_fixture]})
  66. right = DataFrame({"b": ["a", "b"], "c": [1.0, 2.0]})
  67. result = merge(left, right, how="cross")
  68. expected = DataFrame(
  69. {
  70. "a": [1, 1, nulls_fixture, nulls_fixture],
  71. "b": ["a", "b", "a", "b"],
  72. "c": [1.0, 2.0, 1.0, 2.0],
  73. }
  74. )
  75. tm.assert_frame_equal(result, expected)
  76. def test_join_cross_error_reporting():
  77. # GH#5401
  78. left = DataFrame({"a": [1, 3]})
  79. right = DataFrame({"a": [3, 4]})
  80. msg = (
  81. "Can not pass on, right_on, left_on or set right_index=True or "
  82. "left_index=True"
  83. )
  84. with pytest.raises(MergeError, match=msg):
  85. left.join(right, how="cross", on="a")