Skip to content

Commit 7a60276

Browse files
committed
BUG: Override SparseBlock._can_hold_element
1 parent 4473e57 commit 7a60276

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

pandas/core/internals.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
is_bool_dtype,
2828
is_object_dtype,
2929
is_datetimelike_v_numeric,
30+
is_complex_dtype,
3031
is_float_dtype, is_numeric_dtype,
3132
is_numeric_v_string_like, is_extension_type,
3233
is_list_like,
@@ -1736,6 +1737,7 @@ class FloatBlock(FloatOrComplexBlock):
17361737
is_float = True
17371738
_downcast_dtype = 'int64'
17381739

1740+
@classmethod
17391741
def _can_hold_element(self, element):
17401742
if is_list_like(element):
17411743
element = np.asarray(element)
@@ -1786,6 +1788,7 @@ class ComplexBlock(FloatOrComplexBlock):
17861788
__slots__ = ()
17871789
is_complex = True
17881790

1791+
@classmethod
17891792
def _can_hold_element(self, element):
17901793
if is_list_like(element):
17911794
element = np.array(element)
@@ -1948,6 +1951,7 @@ class BoolBlock(NumericBlock):
19481951
is_bool = True
19491952
_can_hold_na = False
19501953

1954+
@classmethod
19511955
def _can_hold_element(self, element):
19521956
if is_list_like(element):
19531957
element = np.asarray(element)
@@ -2660,6 +2664,31 @@ def __init__(self, values, placement, ndim=None, fastpath=False, **kwargs):
26602664
else:
26612665
self._can_hold_na = False
26622666

2667+
def _can_hold_element(self, element):
2668+
""" require the same dtype as ourselves """
2669+
dtype = self.values.sp_values.dtype
2670+
2671+
if is_bool_dtype(dtype):
2672+
return BoolBlock._can_hold_element(element)
2673+
elif is_integer_dtype(dtype):
2674+
if is_list_like(element):
2675+
element = np.array(element)
2676+
tipo = element.dtype.type
2677+
return (issubclass(tipo, np.integer) and
2678+
not issubclass(tipo,
2679+
(np.datetime64,
2680+
np.timedelta64)) and
2681+
dtype.itemsize >= element.dtype.itemsize)
2682+
return is_integer(element)
2683+
elif is_float_dtype(dtype):
2684+
return FloatBlock._can_hold_element(element)
2685+
elif is_complex_dtype(dtype):
2686+
return ComplexBlock._can_hold_element(element)
2687+
elif is_object_dtype(dtype):
2688+
return True
2689+
else:
2690+
return False
2691+
26632692
def coerce_to_target_dtype(self, other, copy=True):
26642693
return super(SparseBlock, self).coerce_to_target_dtype(other, copy)
26652694

0 commit comments

Comments
 (0)