diff --git a/pandas/core/internals.py b/pandas/core/internals.py index 7412351b123392..7bdf32f2e9d1ad 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -1456,6 +1456,11 @@ def where(self, other, cond, align=True, errors='raise', raise ValueError("where must have a condition that is ndarray " "like") + # For SparseBlock, self.values is always 1D. If cond was a frame, + # it's 2D values would incorrectly broadcast later on. + if values.ndim == 1 and any(ax == 1 for ax in cond.shape): + cond = cond.ravel() + # our where function def func(cond, values, other): if cond.ravel().all(): @@ -2817,10 +2822,10 @@ def _can_hold_element(self, element): def _try_coerce_result(self, result): if (isinstance(result, np.ndarray) and - np.ndim(result) > 0 - and not is_sparse(result)): + np.ndim(result) == 1 and + not is_sparse(result)): result = SparseArray(result, kind=self.kind, - fill_value=self.fill_value, dtype=self.dtype) + fill_value=self.fill_value) return result def __len__(self):