Skip to content

Commit 40a4d05

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 b5b57c9 commit 40a4d05

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

pandas/core/internals.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,11 @@ def where(self, other, cond, align=True, errors='raise',
15011501
raise ValueError("where must have a condition that is ndarray "
15021502
"like")
15031503

1504+
# For SparseBlock, self.values is always 1D. If cond was a frame,
1505+
# it's 2D values would incorrectly broadcast later on.
1506+
if values.ndim == 1 and any(ax == 1 for ax in cond.shape):
1507+
cond = cond.ravel()
1508+
15041509
# our where function
15051510
def func(cond, values, other):
15061511
if cond.ravel().all():
@@ -3074,10 +3079,10 @@ def _can_hold_element(self, element):
30743079

30753080
def _try_coerce_result(self, result):
30763081
if (isinstance(result, np.ndarray) and
3077-
np.ndim(result) > 0
3078-
and not is_sparse(result)):
3082+
np.ndim(result) == 1 and
3083+
not is_sparse(result)):
30793084
result = SparseArray(result, kind=self.kind,
3080-
fill_value=self.fill_value, dtype=self.dtype)
3085+
fill_value=self.fill_value)
30813086
return result
30823087

30833088
def __len__(self):

0 commit comments

Comments
 (0)