Skip to content

Commit

Permalink
fix: support scalar strings in fill_none (#2793)
Browse files Browse the repository at this point in the history
* fix: identify strings as scalars

* test: add test for `fill_none` with bare strings

* Update src/awkward/operations/ak_fill_none.py
  • Loading branch information
agoose77 authored Nov 3, 2023
1 parent 4ed2397 commit c01efd8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 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,24 @@ 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 that we know `valuelayout` isn't a low-level type, we must have scalars
# Thus, we can safely promote these scalars to a layout without
# 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"},
),
],
)
)

0 comments on commit c01efd8

Please sign in to comment.