Skip to content

Commit 13a033e

Browse files
committed
fixup! ENH: Allow SparseDataFrame/SparseSeries values assignment
1 parent 291a5ff commit 13a033e

File tree

1 file changed

+57
-10
lines changed

1 file changed

+57
-10
lines changed

pandas/core/internals.py

+57-10
Original file line numberDiff line numberDiff line change
@@ -1500,11 +1500,6 @@ def where(self, other, cond, align=True, errors='raise',
15001500
raise ValueError("where must have a condition that is ndarray "
15011501
"like")
15021502

1503-
# For SparseBlock, self.values is always 1D. If cond was a frame,
1504-
# it's 2D values would incorrectly broadcast later on.
1505-
if values.ndim == 1 and any(ax == 1 for ax in cond.shape):
1506-
cond = cond.ravel()
1507-
15081503
# our where function
15091504
def func(cond, values, other):
15101505
if cond.ravel().all():
@@ -1816,11 +1811,6 @@ def putmask(self, mask, new, align=True, inplace=False, axis=0,
18161811
new_values = self.values if inplace else self.copy().values
18171812
new_values, _, new, _ = self._try_coerce_args(new_values, new)
18181813

1819-
if is_sparse(new_values):
1820-
indexer = mask.to_dense().values.ravel().nonzero()[0]
1821-
block = self.setitem(indexer, new)
1822-
return [block]
1823-
18241814
if isinstance(new, np.ndarray) and len(new) == len(mask):
18251815
new = new[mask]
18261816

@@ -3176,6 +3166,63 @@ def sparse_reindex(self, new_index):
31763166
return self.make_block_same_class(values, sparse_index=new_index,
31773167
placement=self.mgr_locs)
31783168

3169+
def where(self, other, cond, align=True, errors='raise',
3170+
try_cast=False, axis=0, transpose=False, mgr=None):
3171+
"""
3172+
evaluate the block; return result block(s) from the result
3173+
3174+
Parameters
3175+
----------
3176+
other : a ndarray/object
3177+
cond : the condition to respect
3178+
align : boolean, perform alignment on other/cond
3179+
errors : str, {'raise', 'ignore'}, default 'raise'
3180+
- ``raise`` : allow exceptions to be raised
3181+
- ``ignore`` : suppress exceptions. On error return original object
3182+
3183+
axis : int
3184+
transpose : boolean
3185+
Set to True if self is stored with axes reversed
3186+
3187+
Returns
3188+
-------
3189+
a new sparse block(s), the result of the func
3190+
"""
3191+
cond = getattr(cond, 'values', cond)
3192+
# For SparseBlock, self.values is always 1D.
3193+
# If cond was a frame, its 2D values would incorrectly broadcast
3194+
# later on.
3195+
if self.values.ndim == 1 and any(ax == 1 for ax in cond.shape):
3196+
cond = cond.ravel()
3197+
3198+
return super(self, SparseBlock).where(
3199+
other, cond, align=align, errors=errors, try_cast=try_cast,
3200+
axis=axis, transpose=transpose, mgr=mgr)
3201+
3202+
def putmask(self, mask, new, align=True, inplace=False, axis=0,
3203+
transpose=False, mgr=None):
3204+
"""
3205+
putmask the data to the block; we must be a single block and not
3206+
generate other blocks
3207+
3208+
return the resulting block
3209+
3210+
Parameters
3211+
----------
3212+
mask : the condition to respect
3213+
new : a ndarray/object
3214+
align : boolean, perform alignment on other/cond, default is True
3215+
inplace : perform inplace modification, default is False
3216+
3217+
Returns
3218+
-------
3219+
a new block, the result of the putmask
3220+
"""
3221+
_, _, new, _ = self._try_coerce_args(self.values, new)
3222+
indexer = mask.to_dense().values.ravel().nonzero()[0]
3223+
block = self.setitem(indexer, new)
3224+
return [block]
3225+
31793226

31803227
def get_block_type(values, dtype=None):
31813228
"""

0 commit comments

Comments
 (0)