Skip to content

BUG: correctly handle placement of pos/neg inf when dividing by integer 0) #6359

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
Feb 15, 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/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Bug Fixes
- TimeGrouper has a more compatible API to the rest of the groupers (e.g. ``groups`` was missing) (:issue:`3881`)
- Bug in ``pd.eval`` when parsing strings with possible tokens like ``'&'``
(:issue:`6351`)
- Bug correctly handle placements of ``-inf`` in Panels when dividing by integer 0 (:issue:`6359`)

pandas 0.13.1
-------------
Expand Down
28 changes: 24 additions & 4 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1179,13 +1179,23 @@ def _lcd_dtypes(a_dtype, b_dtype):
return np.object


def _fill_zeros(result, y, fill):
""" if we have an integer value (or array in y)
def _fill_zeros(result, x, y, name, fill):
"""
if this is a reversed op, then flip x,y
if we have an integer value (or array in y)
and we have 0's, fill them with the fill,
return the result
mask the nan's from x
"""

if fill is not None:

if name.startswith('r'):
x,y = y,x


if not isinstance(y, np.ndarray):
dtype, value = _infer_dtype_from_scalar(y)
y = pa.empty(result.shape, dtype=dtype)
Expand All @@ -1196,8 +1206,18 @@ def _fill_zeros(result, y, fill):
mask = y.ravel() == 0
if mask.any():
shape = result.shape
result, changed = _maybe_upcast_putmask(
result.ravel(), mask, fill)
result = result.ravel().astype('float64')

signs = np.sign(result)
nans = np.isnan(x.ravel())
np.putmask(result, mask & ~nans, 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)

result = result.reshape(shape)

return result
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ def na_op(x, y):

result, changed = com._maybe_upcast_putmask(result, -mask, pa.NA)

result = com._fill_zeros(result, y, fill_zeros)
result = com._fill_zeros(result, x, y, name, fill_zeros)
return result

def wrapper(left, right, name=name):
Expand Down Expand Up @@ -749,7 +749,7 @@ def na_op(x, y):
result, changed = com._maybe_upcast_putmask(result, -mask, np.nan)
result = result.reshape(x.shape)

result = com._fill_zeros(result, y, fill_zeros)
result = com._fill_zeros(result, x, y, name, fill_zeros)

return result

Expand Down Expand Up @@ -913,7 +913,7 @@ def na_op(x, y):
result[mask] = op(x[mask], y)
result, changed = com._maybe_upcast_putmask(result, -mask, pa.NA)

result = com._fill_zeros(result, y, fill_zeros)
result = com._fill_zeros(result, x, y, name, fill_zeros)
return result

# work only for scalars
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,7 @@ def na_op(x, y):
# handles discrepancy between numpy and numexpr on division/mod
# by 0 though, given that these are generally (always?)
# non-scalars, I'm not sure whether it's worth it at the moment
result = com._fill_zeros(result, y, fill_zeros)
result = com._fill_zeros(result, x, y, name, fill_zeros)
return result

@Substitution(name)
Expand Down
13 changes: 5 additions & 8 deletions pandas/tests/test_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2071,23 +2071,20 @@ def test_arith_flex_panel(self):
else:
aliases = {'div': 'truediv'}
self.panel = self.panel.to_panel()
n = np.random.randint(-50, 50)
for op in ops:
try:

for n in [ np.random.randint(-50, -1), np.random.randint(1, 50), 0]:
for op in ops:
alias = aliases.get(op, op)
f = getattr(operator, alias)
result = getattr(self.panel, op)(n)
exp = f(self.panel, n)
result = getattr(self.panel, op)(n)
assert_panel_equal(result, exp, check_panel_type=True)

# rops
r_f = lambda x, y: f(y, x)
result = getattr(self.panel, 'r' + op)(n)
exp = r_f(self.panel, n)
result = getattr(self.panel, 'r' + op)(n)
assert_panel_equal(result, exp)
except:
print("Failing operation %r" % op)
raise

def test_sort(self):
def is_sorted(arr):
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2212,6 +2212,12 @@ def test_div(self):
assert_series_equal(result, p['first'].astype('float64'))
self.assertFalse(np.array_equal(result, p['second'] / p['first']))

# inf signing
s = Series([np.nan,1.,-1.])
result = s / 0
expected = Series([np.nan,np.inf,-np.inf])
assert_series_equal(result, expected)

def test_operators(self):

def _check_op(series, other, op, pos_only=False):
Expand Down