Repository: pandas-dev/pandas

Issue:
merging two empty dataframes can incur a division by zero
#### Code Sample, a copy-pastable example if possible

```python
import numpy
import pandas

pandas.show_versions()

a = pandas.DataFrame({'a':[],'b':[],'c':[]})
numpy.seterr(divide='raise')
pandas.merge(a,a,on=('a','b')) # no problem if we only merge on 'a'.
```
#### Problem description
The call to merge triggers a division by zero.

<details>

Traceback (most recent call last):
  File "/homes/mickyl/work/bugs/pandas_merge_div_by_0.py", line 8, in <module>
    pandas.merge(a,a,on=('a','b'))
  File "/homes/mickyl/venvs/debian8/local/lib/python2.7/site-packages/pandas/core/reshape/merge.py", line 54, in merge
    return op.get_result()
  File "/homes/mickyl/venvs/debian8/local/lib/python2.7/site-packages/pandas/core/reshape/merge.py", line 569, in get_result
    join_index, left_indexer, right_indexer = self._get_join_info()
  File "/homes/mickyl/venvs/debian8/local/lib/python2.7/site-packages/pandas/core/reshape/merge.py", line 734, in _get_join_info
    right_indexer) = self._get_join_indexers()
  File "/homes/mickyl/venvs/debian8/local/lib/python2.7/site-packages/pandas/core/reshape/merge.py", line 713, in _get_join_indexers
    how=self.how)
  File "/homes/mickyl/venvs/debian8/local/lib/python2.7/site-packages/pandas/core/reshape/merge.py", line 985, in _get_join_indexers
    lkey, rkey = _get_join_keys(llab, rlab, shape, sort)
  File "/homes/mickyl/venvs/debian8/local/lib/python2.7/site-packages/pandas/core/reshape/merge.py", line 1457, in _get_join_keys
    stride //= shape[i]
FloatingPointError: divide by zero encountered in long_scalars

</details>

The expeted behaviour is for merge to return an empty dataframe without causing division by 0.

#### Expected Output
just a print-out of all the version numbers with no exception.

#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------

commit: None
python: 2.7.9.final.0
python-bits: 64
OS: Linux
OS-release: 4.7.0-0.bpo.1-amd64
machine: x86_64
processor: 
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: None.None

pandas: 0.20.3
pytest: 2.6.3
pip: 9.0.1
setuptools: 5.5.1
Cython: 0.25.2
numpy: 1.13.3
scipy: 0.19.1
xarray: None
IPython: 5.4.1
sphinx: 1.2.3
patsy: None
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.1.1
numexpr: 2.6.4
feather: None
matplotlib: 2.0.0
openpyxl: 2.4.8
xlrd: 0.9.2
xlwt: 0.7.5
xlsxwriter: 0.5.2
lxml: 3.4.0
bs4: None
html5lib: 0.999999999
sqlalchemy: 0.9.8
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None

</details>



Discussion:
you could make a patch to fix this. note that we surround pandas operations with ``np.seterr(divide='ignore')`` as a matter of course; we DO want to propagate. In this case it would be checked directly though.
I'm sorry, I don't know what the right fix would be. Please remember that I don't understand this codebase like you do.
well you would have to debug this and see where the error is occurring. then surround that with a ``np.seterr(divid='ignore')``
Hi, does "make a patch" mean edit the code directly? Sorry, I'm new to this and thought I'd try it, but now am not sure if I did the right thing.

Resolving patch:
diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py
--- a/pandas/core/reshape/merge.py
+++ b/pandas/core/reshape/merge.py
@@ -1529,7 +1529,8 @@ def _get_join_keys(llab, rlab, shape, sort):
     rkey = stride * rlab[0].astype('i8', subok=False, copy=False)
 
     for i in range(1, nlev):
-        stride //= shape[i]
+        with np.errstate(divide='ignore'):
+            stride //= shape[i]
         lkey += llab[i] * stride
         rkey += rlab[i] * stride
 


Test patch:
diff --git a/pandas/tests/reshape/test_merge.py b/pandas/tests/reshape/test_merge.py
--- a/pandas/tests/reshape/test_merge.py
+++ b/pandas/tests/reshape/test_merge.py
@@ -864,6 +864,12 @@ def test_validation(self):
         result = merge(left, right, on=['a', 'b'], validate='1:1')
         assert_frame_equal(result, expected_multi)
 
+    def test_merge_two_empty_df_no_division_error(self):
+        # GH17776, PR #17846
+        a = pd.DataFrame({'a': [], 'b': [], 'c': []})
+        with np.errstate(divide='raise'):
+            merge(a, a, on=('a', 'b'))
+
 
 def _check_merge(x, y):
     for how in ['inner', 'left', 'outer']: