Skip to content

Commit 49f83ca

Browse files
committed
Fix .where for sparse blocks
Discrepancy comes from: dense_frame._data.blocks[0].values # this is 2D even for 1D block sparse_frame._data.blocks[0].values # this is always 1D I'm sure this had worked before and was unneeded a month ago.
1 parent 7c5ac6a commit 49f83ca

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

pandas/core/internals.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,11 @@ def where(self, other, cond, align=True, errors='raise',
14561456
raise ValueError("where must have a condition that is ndarray "
14571457
"like")
14581458

1459+
# For SparseBlock, self.values is always 1D. If cond was a frame,
1460+
# it's 2D values would incorrectly broadcast later on.
1461+
if values.ndim == 1 and any(ax == 1 for ax in cond.shape):
1462+
cond = cond.ravel()
1463+
14591464
# our where function
14601465
def func(cond, values, other):
14611466
if cond.ravel().all():
@@ -2817,10 +2822,10 @@ def _can_hold_element(self, element):
28172822

28182823
def _try_coerce_result(self, result):
28192824
if (isinstance(result, np.ndarray) and
2820-
np.ndim(result) > 0
2821-
and not is_sparse(result)):
2825+
np.ndim(result) == 1 and
2826+
not is_sparse(result)):
28222827
result = SparseArray(result, kind=self.kind,
2823-
fill_value=self.fill_value, dtype=self.dtype)
2828+
fill_value=self.fill_value)
28242829
return result
28252830

28262831
def __len__(self):

0 commit comments

Comments
 (0)