BUG: SparseArray numeric ops misc fixes by sinhrks · Pull Request #12910 · pandas-dev/pandas (original) (raw)

Fixed following 3 issues occurred on the current master.

1. addition ignores rhs fill_value

pd.SparseArray([1., 1.]) + pd.SparseArray([1., 0.], fill_value=0.)
# [2.0, nan]
# Fill: nan
# IntIndex
# Indices: array([0], dtype=int32)

Expected:
# [2.0, 1.0]

2. mod raises AttributeError

pd.SparseArray([1, 1]) % pd.SparseArray([1, np.nan])
# AttributeError: 'module' object has no attribute 'sparse_nanmod'

3. pow outputs incorrect result wiht 1.0 ** np.nan

pd.SparseArray([1., 1.]) ** pd.SparseArray([1., np.nan])
# [1.0, nan]
# Fill: nan
# IntIndex
# Indices: array([0], dtype=int32)

Expected:
# [1.0, 1.0]

# NumPy result
np.array([1., 1.]) ** np.array([1, np.nan])
# array([ 1.,  1.])