Skip to content
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

fix: support Content objects in writing #986

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
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
14 changes: 10 additions & 4 deletions src/uproot/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,18 @@ def ensure_numpy(array, types=(numpy.bool_, numpy.integer, numpy.floating)):
Returns an ``np.ndarray`` if ``array`` can be converted to an array of the
desired type and raises TypeError if it cannot.
"""
import uproot

awkward = uproot.extras.awkward()
with warnings.catch_warnings():
warnings.simplefilter("error", numpy.VisibleDeprecationWarning)
try:
out = numpy.asarray(array)
except (ValueError, numpy.VisibleDeprecationWarning) as err:
raise TypeError("cannot be converted to a NumPy array") from err
if isinstance(array, awkward.contents.Content):
out = awkward.to_numpy(array)
else:
try:
out = numpy.asarray(array)
except (ValueError, numpy.VisibleDeprecationWarning) as err:
raise TypeError("cannot be converted to a NumPy array") from err
if not issubclass(out.dtype.type, types):
raise TypeError(f"cannot be converted to a NumPy array of type {types}")
return out
Expand Down
4 changes: 3 additions & 1 deletion src/uproot/writing/_cascadetree.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,9 @@ def extend(self, file, sink, data):
)
)
else:
big_endian = numpy.asarray(branch_array, dtype=datum["dtype"])
big_endian = uproot._util.ensure_numpy(branch_array).astype(
datum["dtype"]
)
if big_endian.shape != (len(branch_array),) + datum["shape"]:
raise ValueError(
"'extend' must fill branches with a consistent shape: has {}, trying to fill with {}".format(
Expand Down