Skip to content

BUG: use EA.astype in ExtensionBlock.to_native_types #28841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
16 changes: 15 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,21 @@ 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"""
try:
values = self.values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bunch of this can be build outside of the try/except right?

if slicer is not None:
values = values[slicer]
mask = isna(values)
values = values.astype(str)
values[mask] = na_rep
# we are expected to return a 2-d ndarray
return values.reshape(1, len(values))
except Exception:
# eg SparseArray does not support setitem, needs to be converted to ndarray
return super().to_native_types(slicer, na_rep, quoting, **kwargs)

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