@@ -1630,24 +1630,50 @@ def test_merge_incompat_dtypes_error(self, df1_vals, df2_vals):
1630
1630
df2 = DataFrame ({"A" : df2_vals })
1631
1631
1632
1632
msg = (
1633
- f"You are trying to merge on { df1 ['A' ].dtype } and "
1634
- f"{ df2 ['A' ].dtype } columns. If you wish to proceed "
1635
- "you should use pd.concat"
1633
+ f"You are trying to merge on { df1 ['A' ].dtype } and { df2 ['A' ].dtype } "
1634
+ "columns for key 'A'. If you wish to proceed you should use pd.concat"
1636
1635
)
1637
1636
msg = re .escape (msg )
1638
1637
with pytest .raises (ValueError , match = msg ):
1639
1638
merge (df1 , df2 , on = ["A" ])
1640
1639
1641
1640
# Check that error still raised when swapping order of dataframes
1642
1641
msg = (
1643
- f"You are trying to merge on { df2 ['A' ].dtype } and "
1644
- f"{ df1 ['A' ].dtype } columns. If you wish to proceed "
1645
- "you should use pd.concat"
1642
+ f"You are trying to merge on { df2 ['A' ].dtype } and { df1 ['A' ].dtype } "
1643
+ "columns for key 'A'. If you wish to proceed you should use pd.concat"
1646
1644
)
1647
1645
msg = re .escape (msg )
1648
1646
with pytest .raises (ValueError , match = msg ):
1649
1647
merge (df2 , df1 , on = ["A" ])
1650
1648
1649
+ # Check that error still raised when merging on multiple columns
1650
+ # The error message should mention the first incompatible column
1651
+ if len (df1_vals ) == len (df2_vals ):
1652
+ # Column A in df1 and df2 is of compatible (the same) dtype
1653
+ # Columns B and C in df1 and df2 are of incompatible dtypes
1654
+ df3 = DataFrame ({"A" : df2_vals , "B" : df1_vals , "C" : df1_vals })
1655
+ df4 = DataFrame ({"A" : df2_vals , "B" : df2_vals , "C" : df2_vals })
1656
+
1657
+ # Check that error raised correctly when merging all columns A, B, and C
1658
+ # The error message should mention key 'B'
1659
+ msg = (
1660
+ f"You are trying to merge on { df3 ['B' ].dtype } and { df4 ['B' ].dtype } "
1661
+ "columns for key 'B'. If you wish to proceed you should use pd.concat"
1662
+ )
1663
+ msg = re .escape (msg )
1664
+ with pytest .raises (ValueError , match = msg ):
1665
+ merge (df3 , df4 )
1666
+
1667
+ # Check that error raised correctly when merging columns A and C
1668
+ # The error message should mention key 'C'
1669
+ msg = (
1670
+ f"You are trying to merge on { df3 ['C' ].dtype } and { df4 ['C' ].dtype } "
1671
+ "columns for key 'C'. If you wish to proceed you should use pd.concat"
1672
+ )
1673
+ msg = re .escape (msg )
1674
+ with pytest .raises (ValueError , match = msg ):
1675
+ merge (df3 , df4 , on = ["A" , "C" ])
1676
+
1651
1677
@pytest .mark .parametrize (
1652
1678
"expected_data, how" ,
1653
1679
[
0 commit comments