Skip to content

Commit 4be3899

Browse files
committed
Merge pull request #7328 from jreback/div_error
BUG: Bug in broadcasting with .div, integer dtypes and divide-by-zero (GH7325)
2 parents 97ad707 + 8ccf0d2 commit 4be3899

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

doc/source/v0.14.1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,4 @@ Bug Fixes
8787
(:issue:`7315`).
8888
- Bug in inferred_freq results in None for eastern hemisphere timezones (:issue:`7310`)
8989
- Bug in ``Easter`` returns incorrect date when offset is negative (:issue:`7195`)
90+
- Bug in broadcasting with ``.div``, integer dtypes and divide-by-zero (:issue:`7325`)

pandas/core/common.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,20 +1244,21 @@ def _fill_zeros(result, x, y, name, fill):
12441244

12451245
if is_integer_dtype(y):
12461246

1247-
mask = y.ravel() == 0
1248-
if mask.any():
1247+
if (y.ravel() == 0).any():
12491248
shape = result.shape
12501249
result = result.ravel().astype('float64')
12511250

1251+
# GH 7325, mask and nans must be broadcastable
12521252
signs = np.sign(result)
1253-
nans = np.isnan(x.ravel())
1254-
np.putmask(result, mask & ~nans, fill)
1253+
mask = ((y == 0) & ~np.isnan(x)).ravel()
1254+
1255+
np.putmask(result, mask, fill)
12551256

12561257
# if we have a fill of inf, then sign it
12571258
# correctly
12581259
# GH 6178
12591260
if np.isinf(fill):
1260-
np.putmask(result,signs<0 & mask & ~nans,-fill)
1261+
np.putmask(result,signs<0 & mask, -fill)
12611262

12621263
result = result.reshape(shape)
12631264

pandas/tests/test_frame.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5240,6 +5240,17 @@ def test_arith_flex_series(self):
52405240
assert_frame_equal(df.div(row), df / row)
52415241
assert_frame_equal(df.div(col, axis=0), (df.T / col).T)
52425242

5243+
# broadcasting issue in GH7325
5244+
df = DataFrame(np.arange(3*2).reshape((3,2)),dtype='int64')
5245+
expected = DataFrame([[np.inf,np.inf],[1.0,1.5],[1.0,1.25]])
5246+
result = df.div(df[0],axis='index')
5247+
assert_frame_equal(result,expected)
5248+
5249+
df = DataFrame(np.arange(3*2).reshape((3,2)),dtype='float64')
5250+
expected = DataFrame([[np.nan,np.inf],[1.0,1.5],[1.0,1.25]])
5251+
result = df.div(df[0],axis='index')
5252+
assert_frame_equal(result,expected)
5253+
52435254
def test_arith_non_pandas_object(self):
52445255
df = self.simple
52455256

0 commit comments

Comments
 (0)