Skip to content
Merged
34 changes: 33 additions & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,6 @@ def _try_coerce_args(self, other):

def to_native_types(self, slicer=None, na_rep="nan", quoting=None, **kwargs):
""" convert to our native types format, slicing if desired """

values = self.get_values()

if slicer is not None:
Expand Down Expand Up @@ -1783,6 +1782,23 @@ def get_values(self, dtype=None):
def to_dense(self):
return np.asarray(self.values)

def to_native_types(self, slicer=None, na_rep="nan", quoting=None, **kwargs):
"""override to use ExtensionArray astype for the conversion"""
values = self.values
if slicer is not None:
values = values[slicer]
mask = isna(values)

try:
values = values.astype(str)
values[mask] = na_rep
except Exception:
# eg SparseArray does not support setitem, needs to be converted to ndarray
return super().to_native_types(slicer, na_rep, quoting, **kwargs)

# we are expected to return a 2-d ndarray
return values.reshape(1, len(values))

def take_nd(self, indexer, axis=0, new_mgr_locs=None, fill_tuple=None):
"""
Take values according to indexer and return them as a block.
Expand Down Expand Up @@ -2338,6 +2354,22 @@ def to_dense(self):
# expects that behavior.
return np.asarray(self.values, dtype=_NS_DTYPE)

def to_native_types(
self, slicer=None, na_rep=None, date_format=None, quoting=None, **kwargs
):
"""
We need to pick DatetimeBlock's version, but the inheritance structure
would use ExtensionBlock's verison
"""
return DatetimeBlock.to_native_types(
self,
slicer=slicer,
na_rep=na_rep,
date_format=date_format,
quoting=quoting,
**kwargs
)

def _slice(self, slicer):
""" return a slice of my values """
if isinstance(slicer, tuple):
Expand Down