diff --git a/doc/source/v0.14.1.txt b/doc/source/v0.14.1.txt index d4598e0aece37..608c368c79ed4 100644 --- a/doc/source/v0.14.1.txt +++ b/doc/source/v0.14.1.txt @@ -87,3 +87,4 @@ Bug Fixes (:issue:`7315`). - Bug in inferred_freq results in None for eastern hemisphere timezones (:issue:`7310`) - Bug in ``Easter`` returns incorrect date when offset is negative (:issue:`7195`) +- Bug in broadcasting with ``.div``, integer dtypes and divide-by-zero (:issue:`7325`) diff --git a/pandas/core/common.py b/pandas/core/common.py index afa376a14d4da..d993112933fa9 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -1244,20 +1244,21 @@ def _fill_zeros(result, x, y, name, fill): if is_integer_dtype(y): - mask = y.ravel() == 0 - if mask.any(): + if (y.ravel() == 0).any(): shape = result.shape result = result.ravel().astype('float64') + # GH 7325, mask and nans must be broadcastable signs = np.sign(result) - nans = np.isnan(x.ravel()) - np.putmask(result, mask & ~nans, fill) + mask = ((y == 0) & ~np.isnan(x)).ravel() + + np.putmask(result, mask, fill) # if we have a fill of inf, then sign it # correctly # GH 6178 if np.isinf(fill): - np.putmask(result,signs<0 & mask & ~nans,-fill) + np.putmask(result,signs<0 & mask, -fill) result = result.reshape(shape) diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 170a64aa58482..358d9d82403f6 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -5240,6 +5240,17 @@ def test_arith_flex_series(self): assert_frame_equal(df.div(row), df / row) assert_frame_equal(df.div(col, axis=0), (df.T / col).T) + # broadcasting issue in GH7325 + df = DataFrame(np.arange(3*2).reshape((3,2)),dtype='int64') + expected = DataFrame([[np.inf,np.inf],[1.0,1.5],[1.0,1.25]]) + result = df.div(df[0],axis='index') + assert_frame_equal(result,expected) + + df = DataFrame(np.arange(3*2).reshape((3,2)),dtype='float64') + expected = DataFrame([[np.nan,np.inf],[1.0,1.5],[1.0,1.25]]) + result = df.div(df[0],axis='index') + assert_frame_equal(result,expected) + def test_arith_non_pandas_object(self): df = self.simple