Repository: pandas-dev/pandas
Pull request: TST: Adding merge test for non-string columns [Ref 17962]
- [x] closes #17962
- [x] tests added / passed
- [x] passes `black pandas`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`


Problem statement:
BUG: merging with non-stringlike columns can raise
```
In [1]: pd.__version__
Out[1]: '0.21.0rc1+36.ge1dabf3'

In [2]: left = pd.DataFrame({
   ...:     0: [1, 0, 1, 0],
   ...:     1: [0, 1, 0, 0],
   ...:     2: [0, 0, 2, 0],
   ...:     3: [1, 0, 0, 0]
   ...: })
   ...: left
   ...: 
Out[2]: 
   0  1  2  3
0  1  0  0  1
1  0  1  0  0
2  1  0  2  0
3  0  0  0  0

In [3]: right = left.astype('float64')
   ...: right
   ...: 
Out[3]: 
     0    1    2    3
0  1.0  0.0  0.0  1.0
1  0.0  1.0  0.0  0.0
2  1.0  0.0  2.0  0.0
3  0.0  0.0  0.0  0.0

In [4]: pd.merge(left, right)
In [4]: pd.merge(left, right)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-ff1e14288b09> in <module>()
----> 1 pd.merge(left, right)

~/pandas-dev/pandas/core/reshape/merge.py in merge(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator, validate)
     55                          right_index=right_index, sort=sort, suffixes=suffixes,
     56                          copy=copy, indicator=indicator,
---> 57                          validate=validate)
     58     return op.get_result()
     59 

~/pandas-dev/pandas/core/reshape/merge.py in __init__(self, left, right, how, on, left_on, right_on, axis, left_index, right_index, sort, suffixes, copy, indicator, validate)
    567         # validate the merge keys dtypes. We may need to coerce
    568         # to avoid incompat dtypes
--> 569         self._maybe_coerce_merge_keys()
    570 
    571         # If argument passed to validate,

~/pandas-dev/pandas/core/reshape/merge.py in _maybe_coerce_merge_keys(self)
    928                 typ = lk.categories.dtype if lk_is_cat else object
    929                 self.left = self.left.assign(
--> 930                     **{name: self.left[name].astype(typ)})
    931             if name in self.right.columns:
    932                 typ = rk.categories.dtype if rk_is_cat else object

TypeError: assign() keywords must be strings

```

Hints from issue discussion:
still reproduced in `0.25.0`
We are also experiencing this issue with `0.24.1`
Can anyone provide an example that fails on 0.25.0? I can't reproduce the original anymore.
Can't replicate this on master. Could use a test.

```
In [13]: pd.merge(left, right)
Out[13]:
   0  1  2  3
0  1  0  0  1
1  0  1  0  0
2  1  0  2  0
3  0  0  0  0

In [14]: pd.__version__
Out[14]: '0.26.0.dev0+565.g8c5941cd5'
```

Commit message: TST: Adding merge test for non-string columns [Ref 17962]

Patch to review:
diff --git a/pandas/tests/reshape/merge/test_merge.py b/pandas/tests/reshape/merge/test_merge.py
index 37c0b57bc7581..8f8ccf1ea9092 100644
--- a/pandas/tests/reshape/merge/test_merge.py
+++ b/pandas/tests/reshape/merge/test_merge.py
@@ -134,6 +134,18 @@ def test_merge_common(self):
         exp = merge(self.df, self.df2, on=["key1", "key2"])
         tm.assert_frame_equal(joined, exp)
 
+    def test_merge_non_string_columns(self):
+        # https://github.com/pandas-dev/pandas/issues/17962
+        # Checks that method runs for non string column names
+        left = pd.DataFrame(
+            {0: [1, 0, 1, 0], 1: [0, 1, 0, 0], 2: [0, 0, 2, 0], 3: [1, 0, 0, 3]}
+        )
+
+        right = left.astype(float)
+        expected = left
+
+        tm.assert_frame_equal(expected, pd.merge(left, right))
+
     def test_merge_index_as_on_arg(self):
         # GH14355
 


Code review comments:
diff_hunk: @@ -134,6 +134,18 @@ def test_merge_common(self):
         exp = merge(self.df, self.df2, on=["key1", "key2"])
         tm.assert_frame_equal(joined, exp)
 
+    def test_merge_non_string_columns(self):
+        # https://github.com/pandas-dev/pandas/issues/17962
+        # Checks that method runs for non string column names
+        left = pd.DataFrame(
+            {0: [1, 0, 1, 0], 1: [0, 1, 0, 0], 2: [0, 0, 2, 0], 3: [1, 0, 0, 3]}
+        )
+
+        right = left.astype(float)
+        expected = left
+
+        tm.assert_frame_equal(expected, pd.merge(left, right))
path: pandas/tests/reshape/merge/test_merge.py
text: @user1:
Could you assign `result = pd.merge(left, right)` before this statement so this line can be `tm.assert_frame_equal(expected, result)`

@author:
this is now done