Skip to content

Commit a7f24cb

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 c86a189 commit a7f24cb

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
@@ -1487,6 +1487,11 @@ def where(self, other, cond, align=True, errors='raise',
14871487
raise ValueError("where must have a condition that is ndarray "
14881488
"like")
14891489

1490+
# For SparseBlock, self.values is always 1D. If cond was a frame,
1491+
# it's 2D values would incorrectly broadcast later on.
1492+
if values.ndim == 1 and any(ax == 1 for ax in cond.shape):
1493+
cond = cond.ravel()
1494+
14901495
# our where function
14911496
def func(cond, values, other):
14921497
if cond.ravel().all():
@@ -2960,10 +2965,10 @@ def _can_hold_element(self, element):
29602965

29612966
def _try_coerce_result(self, result):
29622967
if (isinstance(result, np.ndarray) and
2963-
np.ndim(result) > 0
2964-
and not is_sparse(result)):
2968+
np.ndim(result) == 1 and
2969+
not is_sparse(result)):
29652970
result = SparseArray(result, kind=self.kind,
2966-
fill_value=self.fill_value, dtype=self.dtype)
2971+
fill_value=self.fill_value)
29672972
return result
29682973

29692974
def __len__(self):

0 commit comments

Comments
 (0)