Skip to content

Commit 35e8c61

Browse files
committed
fixup! ENH: Allow SparseDataFrame/SparseSeries values assignment
1 parent 8748339 commit 35e8c61

File tree

1 file changed

+57
-10
lines changed

1 file changed

+57
-10
lines changed

pandas/core/internals/blocks.py

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

1500-
# For SparseBlock, self.values is always 1D. If cond was a frame,
1501-
# it's 2D values would incorrectly broadcast later on.
1502-
if values.ndim == 1 and any(ax == 1 for ax in cond.shape):
1503-
cond = cond.ravel()
1504-
15051500
# our where function
15061501
def func(cond, values, other):
15071502
if cond.ravel().all():
@@ -1852,11 +1847,6 @@ def putmask(self, mask, new, align=True, inplace=False, axis=0,
18521847
new_values = self.values if inplace else self.copy().values
18531848
new_values, _, new, _ = self._try_coerce_args(new_values, new)
18541849

1855-
if is_sparse(new_values):
1856-
indexer = mask.to_dense().values.ravel().nonzero()[0]
1857-
block = self.setitem(indexer, new)
1858-
return [block]
1859-
18601850
if isinstance(new, np.ndarray) and len(new) == len(mask):
18611851
new = new[mask]
18621852

@@ -3270,6 +3260,63 @@ def sparse_reindex(self, new_index):
32703260
return self.make_block_same_class(values, sparse_index=new_index,
32713261
placement=self.mgr_locs)
32723262

3263+
def where(self, other, cond, align=True, errors='raise',
3264+
try_cast=False, axis=0, transpose=False, mgr=None):
3265+
"""
3266+
evaluate the block; return result block(s) from the result
3267+
3268+
Parameters
3269+
----------
3270+
other : a ndarray/object
3271+
cond : the condition to respect
3272+
align : boolean, perform alignment on other/cond
3273+
errors : str, {'raise', 'ignore'}, default 'raise'
3274+
- ``raise`` : allow exceptions to be raised
3275+
- ``ignore`` : suppress exceptions. On error return original object
3276+
3277+
axis : int
3278+
transpose : boolean
3279+
Set to True if self is stored with axes reversed
3280+
3281+
Returns
3282+
-------
3283+
a new sparse block(s), the result of the func
3284+
"""
3285+
cond = getattr(cond, 'values', cond)
3286+
# For SparseBlock, self.values is always 1D.
3287+
# If cond was a frame, its 2D values would incorrectly broadcast
3288+
# later on.
3289+
if self.values.ndim == 1 and any(ax == 1 for ax in cond.shape):
3290+
cond = cond.ravel()
3291+
3292+
return super(self, SparseBlock).where(
3293+
other, cond, align=align, errors=errors, try_cast=try_cast,
3294+
axis=axis, transpose=transpose, mgr=mgr)
3295+
3296+
def putmask(self, mask, new, align=True, inplace=False, axis=0,
3297+
transpose=False, mgr=None):
3298+
"""
3299+
putmask the data to the block; we must be a single block and not
3300+
generate other blocks
3301+
3302+
return the resulting block
3303+
3304+
Parameters
3305+
----------
3306+
mask : the condition to respect
3307+
new : a ndarray/object
3308+
align : boolean, perform alignment on other/cond, default is True
3309+
inplace : perform inplace modification, default is False
3310+
3311+
Returns
3312+
-------
3313+
a new block, the result of the putmask
3314+
"""
3315+
_, _, new, _ = self._try_coerce_args(self.values, new)
3316+
indexer = mask.to_dense().values.ravel().nonzero()[0]
3317+
block = self.setitem(indexer, new)
3318+
return [block]
3319+
32733320

32743321
# -----------------------------------------------------------------
32753322
# Constructor Helpers

0 commit comments

Comments
 (0)