From a7f24cb1fecda9fdba7235159585b549f6160624 Mon Sep 17 00:00:00 2001 From: Kernc Date: Tue, 14 Nov 2017 01:59:26 +0100 Subject: [PATCH] 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. --- pandas/core/internals.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pandas/core/internals.py b/pandas/core/internals.py index 8f699f853601ca..a2de0b20e88f0f 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -1487,6 +1487,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(): @@ -2960,10 +2965,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):