Skip to content

Commit 7928aaa

Browse files
committed
BUG: Fix wrong SparseBlock initialization
1 parent 64c8a8d commit 7928aaa

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

pandas/core/internals.py

+23-8
Original file line numberDiff line numberDiff line change
@@ -442,12 +442,19 @@ def make_a_block(nv, ref_loc):
442442
nv = _block_shape(nv, ndim=self.ndim)
443443
except (AttributeError, NotImplementedError):
444444
pass
445-
block = self.make_block(values=nv,
446-
placement=ref_loc, fastpath=True)
445+
446+
if isinstance(self, SparseBlock):
447+
block = self.make_block_same_class(values=nv,
448+
placement=ref_loc,
449+
fastpath=True)
450+
else:
451+
block = self.make_block(values=nv,
452+
placement=ref_loc,
453+
fastpath=True)
447454
return block
448455

449456
# ndim == 1
450-
if self.ndim == 1:
457+
if self.ndim == 1 or isinstance(self, SparseBlock):
451458
if mask.any():
452459
nv = f(mask, new_values, None)
453460
else:
@@ -527,7 +534,7 @@ def f(m, v, i):
527534

528535
return self.split_and_operate(None, f, False)
529536

530-
def astype(self, dtype, copy=False, errors='raise', values=None, **kwargs):
537+
def astype(self, dtype, copy=True, errors='raise', values=None, **kwargs):
531538
return self._astype(dtype, copy=copy, errors=errors, values=values,
532539
**kwargs)
533540

@@ -613,7 +620,9 @@ def _can_hold_element(self, element):
613620
element = np.asarray(element)
614621
tipo = element.dtype.type
615622
return issubclass(tipo, dtype)
616-
return isinstance(element, dtype)
623+
else:
624+
element_dtype = infer_dtype_from(element, pandas_dtype=True)[0]
625+
return isinstance(element, dtype) or dtype == element_dtype
617626

618627
def _try_cast_result(self, result, dtype=None):
619628
""" try to cast the result to our original type, we may have
@@ -1394,6 +1403,8 @@ def where(self, other, cond, align=True, raise_on_error=True,
13941403
if not hasattr(cond, 'shape'):
13951404
raise ValueError("where must have a condition that is ndarray "
13961405
"like")
1406+
else:
1407+
cond = cond.reshape(values.shape)
13971408

13981409
# our where function
13991410
def func(cond, values, other):
@@ -1440,7 +1451,10 @@ def func(cond, values, other):
14401451
if try_cast:
14411452
result = self._try_cast_result(result)
14421453

1443-
return self.make_block(result)
1454+
if isinstance(self, SparseBlock):
1455+
return self.make_block_same_class(result, self.mgr_locs)
1456+
else:
1457+
return self.make_block(result)
14441458

14451459
# might need to separate out blocks
14461460
axis = cond.ndim - 1
@@ -1454,7 +1468,8 @@ def func(cond, values, other):
14541468
r = self._try_cast_result(result.take(m.nonzero()[0],
14551469
axis=axis))
14561470
result_blocks.append(
1457-
self.make_block(r.T, placement=self.mgr_locs[m]))
1471+
self.make_block_same_class(r.T,
1472+
placement=self.mgr_locs[m]))
14581473

14591474
return result_blocks
14601475

@@ -2653,7 +2668,7 @@ def sp_index(self):
26532668
def kind(self):
26542669
return self.values.kind
26552670

2656-
def _astype(self, dtype, copy=False, raise_on_error=True, values=None,
2671+
def _astype(self, dtype, copy=True, raise_on_error=True, values=None,
26572672
klass=None, mgr=None, **kwargs):
26582673
if values is None:
26592674
values = self.values

pandas/core/sparse/frame.py

+2-2
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)