Skip to content

Commit 767217a

Browse files
committed
BUG: Fix wrong SparseBlock initialization in where method
1 parent 64c8a8d commit 767217a

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

pandas/core/internals.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
find_common_type)
4747
from pandas.core.dtypes.missing import (
4848
isna, notna, array_equivalent,
49+
na_value_for_dtype,
4950
_isna_compat,
5051
is_null_datelike_scalar)
5152
import pandas.core.dtypes.concat as _concat
@@ -442,12 +443,19 @@ def make_a_block(nv, ref_loc):
442443
nv = _block_shape(nv, ndim=self.ndim)
443444
except (AttributeError, NotImplementedError):
444445
pass
445-
block = self.make_block(values=nv,
446-
placement=ref_loc, fastpath=True)
446+
447+
if isinstance(self, SparseBlock):
448+
block = self.make_block_same_class(values=nv,
449+
placement=ref_loc,
450+
fastpath=True)
451+
else:
452+
block = self.make_block(values=nv,
453+
placement=ref_loc,
454+
fastpath=True)
447455
return block
448456

449457
# ndim == 1
450-
if self.ndim == 1:
458+
if self.ndim == 1 or isinstance(self, SparseBlock):
451459
if mask.any():
452460
nv = f(mask, new_values, None)
453461
else:
@@ -527,7 +535,7 @@ def f(m, v, i):
527535

528536
return self.split_and_operate(None, f, False)
529537

530-
def astype(self, dtype, copy=False, errors='raise', values=None, **kwargs):
538+
def astype(self, dtype, copy=True, errors='raise', values=None, **kwargs):
531539
return self._astype(dtype, copy=copy, errors=errors, values=values,
532540
**kwargs)
533541

@@ -613,7 +621,9 @@ def _can_hold_element(self, element):
613621
element = np.asarray(element)
614622
tipo = element.dtype.type
615623
return issubclass(tipo, dtype)
616-
return isinstance(element, dtype)
624+
else:
625+
element_dtype = infer_dtype_from(element, pandas_dtype=True)[0]
626+
return isinstance(element, dtype) or dtype == element_dtype
617627

618628
def _try_cast_result(self, result, dtype=None):
619629
""" try to cast the result to our original type, we may have
@@ -1394,6 +1404,8 @@ def where(self, other, cond, align=True, raise_on_error=True,
13941404
if not hasattr(cond, 'shape'):
13951405
raise ValueError("where must have a condition that is ndarray "
13961406
"like")
1407+
else:
1408+
cond = cond.reshape(values.shape)
13971409

13981410
# our where function
13991411
def func(cond, values, other):
@@ -1440,7 +1452,13 @@ def func(cond, values, other):
14401452
if try_cast:
14411453
result = self._try_cast_result(result)
14421454

1443-
return self.make_block(result)
1455+
if isinstance(self, SparseBlock):
1456+
fill_value = na_value_for_dtype(result.dtype)
1457+
return self.make_block_same_class(result,
1458+
self.mgr_locs,
1459+
fill_value=fill_value)
1460+
else:
1461+
return self.make_block(result)
14441462

14451463
# might need to separate out blocks
14461464
axis = cond.ndim - 1
@@ -1454,7 +1472,8 @@ def func(cond, values, other):
14541472
r = self._try_cast_result(result.take(m.nonzero()[0],
14551473
axis=axis))
14561474
result_blocks.append(
1457-
self.make_block(r.T, placement=self.mgr_locs[m]))
1475+
self.make_block_same_class(r.T,
1476+
placement=self.mgr_locs[m]))
14581477

14591478
return result_blocks
14601479

@@ -2653,7 +2672,7 @@ def sp_index(self):
26532672
def kind(self):
26542673
return self.values.kind
26552674

2656-
def _astype(self, dtype, copy=False, raise_on_error=True, values=None,
2675+
def _astype(self, dtype, copy=True, raise_on_error=True, values=None,
26572676
klass=None, mgr=None, **kwargs):
26582677
if values is None:
26592678
values = self.values

pandas/core/sparse/frame.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,8 @@ def _apply_columns(self, func):
321321
data=new_data, index=self.index, columns=self.columns,
322322
default_fill_value=self.default_fill_value).__finalize__(self)
323323

324-
def astype(self, dtype):
325-
return self._apply_columns(lambda x: x.astype(dtype))
324+
def astype(self, dtype, copy=True, errors='raise'):
325+
return self._apply_columns(lambda x: x.astype(dtype, copy, errors))
326326

327327
def copy(self, deep=True):
328328
"""

0 commit comments

Comments
 (0)