Skip to content

Commit 792c868

Browse files
yeemeyTomAugspurger
authored andcommitted
BUG: Ignore division by 0 when merging empty dataframes (pandas-dev#17776) (pandas-dev#17846)
(cherry picked from commit 262e8ff)
1 parent 80efa3e commit 792c868

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
@@ -121,7 +121,7 @@ Reshaping
121121
- Error message in ``pd.merge_asof()`` for key datatype mismatch now includes datatype of left and right key (:issue:`18068`)
122122
- Bug in ``pd.concat`` when empty and non-empty DataFrames or Series are concatenated (:issue:`18178` :issue:`18187`)
123123
- Bug in ``DataFrame.filter(...)`` when :class:`unicode` is passed as a condition in Python 2 (:issue:`13101`)
124-
-
124+
- Bug when merging empty DataFrames when ``np.seterr(divide='raise')`` is set (:issue:`17776`)
125125

126126
Numeric
127127
^^^^^^^

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
@@ -861,6 +861,12 @@ def test_validation(self):
861861
result = merge(left, right, on=['a', 'b'], validate='1:1')
862862
assert_frame_equal(result, expected_multi)
863863

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

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

0 commit comments

Comments
 (0)