Skip to content

Commit 262e8ff

Browse files
yeemeyjreback
authored andcommitted
BUG: Ignore division by 0 when merging empty dataframes (#17776) (#17846)
1 parent 4fd104a commit 262e8ff

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

doc/source/whatsnew/v0.21.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ Reshaping
120120
- Error message in ``pd.merge_asof()`` for key datatype mismatch now includes datatype of left and right key (:issue:`18068`)
121121
- Bug in ``pd.concat`` when empty and non-empty DataFrames or Series are concatenated (:issue:`18178` :issue:`18187`)
122122
- Bug in ``DataFrame.filter(...)`` when :class:`unicode` is passed as a condition in Python 2 (:issue:`13101`)
123-
-
123+
- Bug when merging empty DataFrames when ``np.seterr(divide='raise')`` is set (:issue:`17776`)
124124

125125
Numeric
126126
^^^^^^^

pandas/core/reshape/merge.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,8 @@ def _get_join_keys(llab, rlab, shape, sort):
15291529
rkey = stride * rlab[0].astype('i8', subok=False, copy=False)
15301530

15311531
for i in range(1, nlev):
1532-
stride //= shape[i]
1532+
with np.errstate(divide='ignore'):
1533+
stride //= shape[i]
15331534
lkey += llab[i] * stride
15341535
rkey += rlab[i] * stride
15351536

pandas/tests/reshape/test_merge.py

+6
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,12 @@ def test_validation(self):
864864
result = merge(left, right, on=['a', 'b'], validate='1:1')
865865
assert_frame_equal(result, expected_multi)
866866

867+
def test_merge_two_empty_df_no_division_error(self):
868+
# GH17776, PR #17846
869+
a = pd.DataFrame({'a': [], 'b': [], 'c': []})
870+
with np.errstate(divide='raise'):
871+
merge(a, a, on=('a', 'b'))
872+
867873

868874
def _check_merge(x, y):
869875
for how in ['inner', 'left', 'outer']:

0 commit comments

Comments
 (0)