Skip to content

BUG: SparseArray numeric ops misc fixes #12910

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions doc/source/whatsnew/v0.18.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ These changes conform sparse handling to return the correct types and work to ma
- Bug in ``SparseSeries.iloc[]`` with scalar input may raise ``IndexError`` (:issue:`10560`)
- Bug in ``SparseSeries.loc[]``, ``.iloc[]`` with ``slice`` returns ``SparseArray``, rather than ``SparseSeries`` (:issue:`10560`)
- Bug in ``SparseDataFrame.loc[]``, ``.iloc[]`` may results in dense ``Series``, rather than ``SparseSeries`` (:issue:`12787`)
- Bug in ``SparseArray`` addition ignores ``fill_value`` of right hand side (:issue:`12910`)
- Bug in ``SparseArray`` mod raises ``AttributeError (:issue:`12910`)
- Bug in ``SparseArray`` pow calculates ``1 ** np.nan`` as ``np.nan`` which must be 1 (:issue:`12910`)
- Bug in ``SparseSeries.__repr__`` raises ``TypeError`` when it is longer than ``max_rows`` (:issue:`10560`)
- Bug in ``SparseSeries.shape`` ignores ``fill_value`` (:issue:`10452`)
- Bug in ``SparseSeries.reindex`` incorrectly handle ``fill_value`` (:issue:`12797`)
Expand Down
15 changes: 2 additions & 13 deletions pandas/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ def wrapper(self, other):


def _sparse_array_op(left, right, op, name):
if np.isnan(left.fill_value):
sparse_op = lambda a, b: _sparse_nanop(a, b, name)
else:
sparse_op = lambda a, b: _sparse_fillop(a, b, name)
sparse_op = lambda a, b: _sparse_op(a, b, name)

if left.sp_index.equals(right.sp_index):
result = op(left.sp_values, right.sp_values)
Expand All @@ -79,15 +76,7 @@ def _sparse_array_op(left, right, op, name):
fill_value=fill_value)


def _sparse_nanop(this, other, name):
sparse_op = getattr(splib, 'sparse_nan%s' % name)
result, result_index = sparse_op(this.sp_values, this.sp_index,
other.sp_values, other.sp_index)

return result, result_index


def _sparse_fillop(this, other, name):
def _sparse_op(this, other, name):
sparse_op = getattr(splib, 'sparse_%s' % name)
result, result_index = sparse_op(this.sp_values, this.sp_index,
this.fill_value, other.sp_values,
Expand Down
63 changes: 63 additions & 0 deletions pandas/sparse/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,69 @@ def test_fillna_overlap(self):
tm.assert_sp_array_equal(res, exp)


class TestSparseArrayArithmetic(tm.TestCase):

_multiprocess_can_split_ = True

def _check_numeric_ops(self, a, b, a_dense, b_dense):
tm.assert_numpy_array_equal((a + b).to_dense(), a_dense + b_dense)
tm.assert_numpy_array_equal((b + a).to_dense(), b_dense + a_dense)

tm.assert_numpy_array_equal((a - b).to_dense(), a_dense - b_dense)
tm.assert_numpy_array_equal((b - a).to_dense(), b_dense - a_dense)

tm.assert_numpy_array_equal((a * b).to_dense(), a_dense * b_dense)
tm.assert_numpy_array_equal((b * a).to_dense(), b_dense * a_dense)

tm.assert_numpy_array_equal((a / b).to_dense(), a_dense / b_dense)
tm.assert_numpy_array_equal((b / a).to_dense(), b_dense / a_dense)

tm.assert_numpy_array_equal((a // b).to_dense(), a_dense // b_dense)
tm.assert_numpy_array_equal((b // a).to_dense(), b_dense // a_dense)

tm.assert_numpy_array_equal((a % b).to_dense(), a_dense % b_dense)
tm.assert_numpy_array_equal((b % a).to_dense(), b_dense % a_dense)

tm.assert_numpy_array_equal((a ** b).to_dense(), a_dense ** b_dense)
tm.assert_numpy_array_equal((b ** a).to_dense(), b_dense ** a_dense)

def test_float_scalar(self):
values = np.array([np.nan, 1, 2, 0, np.nan, 0, 1, 2, 1, np.nan])

a = SparseArray(values)
self._check_numeric_ops(a, 1, values, 1)
self._check_numeric_ops(a, 0, values, 0)

a = SparseArray(values, fill_value=0)
self._check_numeric_ops(a, 1, values, 1)
self._check_numeric_ops(a, 0, values, 0)

a = SparseArray(values, fill_value=2)
self._check_numeric_ops(a, 1, values, 1)
self._check_numeric_ops(a, 0, values, 0)

def test_float_array(self):
values = np.array([np.nan, 1, 2, 0, np.nan, 0, 1, 2, 1, np.nan])
rvalues = np.array([2, np.nan, 2, 3, np.nan, 0, 1, 5, 2, np.nan])

a = SparseArray(values)
b = SparseArray(rvalues)
self._check_numeric_ops(a, b, values, rvalues)
self._check_numeric_ops(a, b * 0, values, rvalues * 0)

a = SparseArray(values, fill_value=0)
b = SparseArray(rvalues)
self._check_numeric_ops(a, b, values, rvalues)

a = SparseArray(values, fill_value=0)
b = SparseArray(rvalues, fill_value=0)
self._check_numeric_ops(a, b, values, rvalues)

a = SparseArray(values, fill_value=1)
b = SparseArray(rvalues, fill_value=2)
self._check_numeric_ops(a, b, values, rvalues)


if __name__ == '__main__':
import nose
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
Expand Down
39 changes: 1 addition & 38 deletions pandas/sparse/tests/test_libsparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,31 +269,6 @@ def test_to_int_index(self):


class TestSparseOperators(tm.TestCase):
def _nan_op_tests(self, sparse_op, python_op):
def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
xindex = BlockIndex(TEST_LENGTH, xloc, xlen)
yindex = BlockIndex(TEST_LENGTH, yloc, ylen)

xdindex = xindex.to_int_index()
ydindex = yindex.to_int_index()

x = np.arange(xindex.npoints) * 10. + 1
y = np.arange(yindex.npoints) * 100. + 1

result_block_vals, rb_index = sparse_op(x, xindex, y, yindex)
result_int_vals, ri_index = sparse_op(x, xdindex, y, ydindex)

self.assertTrue(rb_index.to_int_index().equals(ri_index))
assert_equal(result_block_vals, result_int_vals)

# check versus Series...
xseries = Series(x, xdindex.indices)
yseries = Series(y, ydindex.indices)
series_result = python_op(xseries, yseries).valid()
assert_equal(result_block_vals, series_result.values)
assert_equal(result_int_vals, series_result.values)

check_cases(_check_case)

def _op_tests(self, sparse_op, python_op):
def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
Expand Down Expand Up @@ -337,16 +312,6 @@ def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
check_ops = ['add', 'sub', 'mul', 'truediv', 'floordiv']


def make_nanoptestf(op):
def f(self):
sparse_op = getattr(splib, 'sparse_nan%s' % op)
python_op = getattr(operator, op)
self._nan_op_tests(sparse_op, python_op)

f.__name__ = 'test_nan%s' % op
return f


def make_optestf(op):
def f(self):
sparse_op = getattr(splib, 'sparse_%s' % op)
Expand All @@ -358,13 +323,11 @@ def f(self):


for op in check_ops:
f = make_nanoptestf(op)
g = make_optestf(op)
setattr(TestSparseOperators, f.__name__, f)
setattr(TestSparseOperators, g.__name__, g)
del f
del g


if __name__ == '__main__':
import nose # noqa
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
Expand Down
Loading