From ce5c13b210963434b212a13a891059d634e0bdc6 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Wed, 1 Nov 2023 22:32:29 +0000 Subject: [PATCH 1/3] fix: identify strings as scalars --- src/awkward/operations/ak_fill_none.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/awkward/operations/ak_fill_none.py b/src/awkward/operations/ak_fill_none.py index c1503a23a4..9d922e5dc5 100644 --- a/src/awkward/operations/ak_fill_none.py +++ b/src/awkward/operations/ak_fill_none.py @@ -81,6 +81,7 @@ 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): @@ -88,12 +89,15 @@ def _impl(array, value, axis, highlevel, behavior): elif isinstance(valuelayout, ak.contents.Content): valuelayout = valuelayout[np.newaxis, ...] else: + # Now we know we've got Python scalars/strings, we can promote 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) From bdab5149abeeaab56c5964e129fac3d38b06ff77 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Wed, 1 Nov 2023 22:32:54 +0000 Subject: [PATCH 2/3] test: add test for `fill_none` with bare strings --- tests/test_2763_to_layout_granularity.py | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/test_2763_to_layout_granularity.py b/tests/test_2763_to_layout_granularity.py index 6983fc12b0..81750ad780 100644 --- a/tests/test_2763_to_layout_granularity.py +++ b/tests/test_2763_to_layout_granularity.py @@ -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"}, + ), + ], + ) + ) From 8b7d679f0a58238014f3f2d08b08ecb28571be07 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Wed, 1 Nov 2023 22:51:35 +0000 Subject: [PATCH 3/3] Update src/awkward/operations/ak_fill_none.py --- src/awkward/operations/ak_fill_none.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/awkward/operations/ak_fill_none.py b/src/awkward/operations/ak_fill_none.py index 9d922e5dc5..dafe67ad2f 100644 --- a/src/awkward/operations/ak_fill_none.py +++ b/src/awkward/operations/ak_fill_none.py @@ -89,7 +89,8 @@ def _impl(array, value, axis, highlevel, behavior): elif isinstance(valuelayout, ak.contents.Content): valuelayout = valuelayout[np.newaxis, ...] else: - # Now we know we've got Python scalars/strings, we can promote without + # 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,