Skip to content

BUG: Bug in broadcasting with .div, integer dtypes and divide-by-zero (GH7325) #7328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/v0.14.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
11 changes: 6 additions & 5 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

Expand Down