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 scalar strings in fill_none #2793

Merged
merged 4 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions src/awkward/operations/ak_fill_none.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,23 @@ def _impl(array, value, axis, highlevel, behavior):
allow_unknown=False,
use_from_iter=True,
primitive_policy="pass-through",
string_policy="pass-through",
)

if isinstance(valuelayout, ak.record.Record):
valuelayout = valuelayout.array[valuelayout.at : valuelayout.at + 1]
elif isinstance(valuelayout, ak.contents.Content):
valuelayout = valuelayout[np.newaxis, ...]
else:
# Now we know we've got Python scalars/strings, we can promote without
agoose77 marked this conversation as resolved.
Show resolved Hide resolved
# adding a new axis
valuelayout = ak.operations.to_layout(
value,
allow_record=True,
allow_unknown=False,
use_from_iter=True,
primitive_policy="promote",
string_policy="promote",
)
valuelayout = valuelayout.to_backend(backend)

Expand Down
25 changes: 25 additions & 0 deletions tests/test_2763_to_layout_granularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,28 @@ def test_with_field():
["x"],
)
)


def test_fill_none():
array = ak.Array([None, 1, 2, None])
result = ak.fill_none(array, "hello world!")
assert result.layout.is_equal_to(
ak.contents.UnionArray(
ak.index.Index8([1, 0, 0, 1]),
ak.index.Index64([0, 0, 1, 0]),
[
ak.contents.NumpyArray(np.array([1, 2], dtype=np.int64)),
ak.contents.ListOffsetArray(
ak.index.Index64([0, 12]),
ak.contents.NumpyArray(
np.array(
[104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33],
dtype=np.uint8,
),
parameters={"__array__": "char"},
),
parameters={"__array__": "string"},
),
],
)
)
Loading