Skip to content

Commit

Permalink
BUG: Ignore division by 0 when merging empty dataframes (#17776) (#17846
Browse files Browse the repository at this point in the history
)
  • Loading branch information
yeemey authored and jreback committed Nov 27, 2017
1 parent 4fd104a commit 262e8ff
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.21.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Reshaping
- Error message in ``pd.merge_asof()`` for key datatype mismatch now includes datatype of left and right key (:issue:`18068`)
- Bug in ``pd.concat`` when empty and non-empty DataFrames or Series are concatenated (:issue:`18178` :issue:`18187`)
- Bug in ``DataFrame.filter(...)`` when :class:`unicode` is passed as a condition in Python 2 (:issue:`13101`)
-
- Bug when merging empty DataFrames when ``np.seterr(divide='raise')`` is set (:issue:`17776`)

Numeric
^^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/reshape/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']:
Expand Down

0 comments on commit 262e8ff

Please sign in to comment.