Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions xarray/namedarray/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,21 @@ def reduce(
else:
data = func(self.data, **kwargs)

if self.dtype == object and not hasattr(data, "dtype"):
# A full reduction (e.g. .sum() with no remaining axes)
# over an object-dtype array can return a raw Python
# scalar rather than a 0-d array (e.g. np.sum() on an
# array of strings returns a plain str). Left as-is,
# this raw scalar would later be wrapped by
# from_array()'s generic np.asarray(data) fallback,
# which infers a dtype from the scalar itself (e.g.
# numpy unicode dtype for a str) instead of preserving
# the original object dtype. Explicitly wrap it in a
# 0-d object array here, while we still know the
# original dtype, using the same helper already used
# for tuple inputs in from_array(). See GH #5024.
data = to_0d_object_array(data)

if getattr(data, "shape", ()) == self.shape:
dims = self.dims
else:
Expand Down
Loading