Skip to content

Commit baeb1bf

Browse files
BUG: modfy(SparseArray) (#26947)
Closes #26946 (cherry picked from commit 430f664ddbb4dab542b34b2c75b6d086fdef4934)
1 parent d47947a commit baeb1bf

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@ Sparse
769769
- Bug in :class:`SparseFrame` constructor where passing ``None`` as the data would cause ``default_fill_value`` to be ignored (:issue:`16807`)
770770
- Bug in :class:`SparseDataFrame` when adding a column in which the length of values does not match length of index, ``AssertionError`` is raised instead of raising ``ValueError`` (:issue:`25484`)
771771
- Introduce a better error message in :meth:`Series.sparse.from_coo` so it returns a ``TypeError`` for inputs that are not coo matrices (:issue:`26554`)
772+
- Bug in :func:`numpy.modf` on a :class:`SparseArray`. Now a tuple of :class:`SparseArray` is returned.
772773

773774
Other
774775
^^^^^

pandas/core/arrays/sparse.py

+11
Original file line numberDiff line numberDiff line change
@@ -1697,6 +1697,17 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
16971697
# No alignment necessary.
16981698
sp_values = getattr(ufunc, method)(self.sp_values, **kwargs)
16991699
fill_value = getattr(ufunc, method)(self.fill_value, **kwargs)
1700+
1701+
if isinstance(sp_values, tuple):
1702+
# multiple outputs. e.g. modf
1703+
arrays = tuple(
1704+
self._simple_new(sp_value,
1705+
self.sp_index,
1706+
SparseDtype(sp_value.dtype, fv))
1707+
for sp_value, fv in zip(sp_values, fill_value)
1708+
)
1709+
return arrays
1710+
17001711
return self._simple_new(sp_values,
17011712
self.sp_index,
17021713
SparseDtype(sp_values.dtype, fill_value))

pandas/tests/arrays/sparse/test_array.py

+10
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,16 @@ def test_ufunc_args(self):
10711071
result = SparseArray([2, 0, 1, -1], fill_value=1)
10721072
tm.assert_sp_array_equal(np.add(sparse, 1), result)
10731073

1074+
@pytest.mark.parametrize('fill_value', [0.0, np.nan])
1075+
def test_modf(self, fill_value):
1076+
# https://github.com/pandas-dev/pandas/issues/26946
1077+
sparse = pd.SparseArray([fill_value] * 10 + [1.1, 2.2],
1078+
fill_value=fill_value)
1079+
r1, r2 = np.modf(sparse)
1080+
e1, e2 = np.modf(np.asarray(sparse))
1081+
tm.assert_sp_array_equal(r1, pd.SparseArray(e1, fill_value=fill_value))
1082+
tm.assert_sp_array_equal(r2, pd.SparseArray(e2, fill_value=fill_value))
1083+
10741084
def test_nbytes_integer(self):
10751085
arr = SparseArray([1, 0, 0, 0, 2], kind='integer')
10761086
result = arr.nbytes

0 commit comments

Comments
 (0)