Skip to content

Commit

Permalink
Fix .where for sparse blocks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kernc committed Nov 14, 2017
1 parent 7c5ac6a commit 49f83ca
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 49f83ca

Please sign in to comment.