From 92f50c47c2c73c1aadbdab030e61a9ddf7c17ff1 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Wed, 29 Nov 2023 23:35:17 +0000 Subject: [PATCH 1/3] fix: do not evaluate ufunc on fill value --- src/awkward/operations/ak_full_like.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/awkward/operations/ak_full_like.py b/src/awkward/operations/ak_full_like.py index 66ce515f0a..e1e3e07ed4 100644 --- a/src/awkward/operations/ak_full_like.py +++ b/src/awkward/operations/ak_full_like.py @@ -7,7 +7,7 @@ from awkward._dispatch import high_level_function from awkward._layout import HighLevelContext, ensure_same_backend from awkward._nplikes.numpy_like import NumpyMetadata -from awkward._nplikes.typetracer import ensure_known_scalar +from awkward._nplikes.typetracer import is_unknown_scalar from awkward.operations.ak_zeros_like import _ZEROS __all__ = ("full_like",) @@ -125,12 +125,14 @@ def action(layout, backend, **kwargs): if layout.is_numpy: original = nplike.asarray(layout.data) - if fill_value is _ZEROS or ensure_known_scalar(fill_value == 0, False): + if fill_value is _ZEROS or ( + not is_unknown_scalar(fill_value) and fill_value == 0 + ): return ak.contents.NumpyArray( nplike.zeros_like(original, dtype=dtype), parameters=layout.parameters, ) - elif ensure_known_scalar(fill_value == 1, False): + elif not is_unknown_scalar(fill_value) and fill_value == 1: return ak.contents.NumpyArray( nplike.ones_like(original, dtype=dtype), parameters=layout.parameters, From c3e015ef52485d3a16a26ec9bad98ee2da3fc347 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Wed, 29 Nov 2023 23:37:32 +0000 Subject: [PATCH 2/3] fix: check fill value is scalar --- src/awkward/operations/ak_full_like.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/awkward/operations/ak_full_like.py b/src/awkward/operations/ak_full_like.py index e1e3e07ed4..8411f8ca98 100644 --- a/src/awkward/operations/ak_full_like.py +++ b/src/awkward/operations/ak_full_like.py @@ -8,6 +8,7 @@ from awkward._layout import HighLevelContext, ensure_same_backend from awkward._nplikes.numpy_like import NumpyMetadata from awkward._nplikes.typetracer import is_unknown_scalar +from awkward._regularize import is_integer_like from awkward.operations.ak_zeros_like import _ZEROS __all__ = ("full_like",) @@ -126,13 +127,19 @@ def action(layout, backend, **kwargs): original = nplike.asarray(layout.data) if fill_value is _ZEROS or ( - not is_unknown_scalar(fill_value) and fill_value == 0 + is_integer_like(fill_value) + and not is_unknown_scalar(fill_value) + and fill_value == 0 ): return ak.contents.NumpyArray( nplike.zeros_like(original, dtype=dtype), parameters=layout.parameters, ) - elif not is_unknown_scalar(fill_value) and fill_value == 1: + elif ( + is_integer_like(fill_value) + and not is_unknown_scalar(fill_value) + and fill_value == 1 + ): return ak.contents.NumpyArray( nplike.ones_like(original, dtype=dtype), parameters=layout.parameters, From 9a3cfa533cbc53db68dd8302ee695076a2d3c04d Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Wed, 29 Nov 2023 23:41:41 +0000 Subject: [PATCH 3/3] test: add simple test --- tests/test_2857_full_like_scalar.py | 36 +++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tests/test_2857_full_like_scalar.py diff --git a/tests/test_2857_full_like_scalar.py b/tests/test_2857_full_like_scalar.py new file mode 100644 index 0000000000..f34ab6d4b6 --- /dev/null +++ b/tests/test_2857_full_like_scalar.py @@ -0,0 +1,36 @@ +# BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE + +from __future__ import annotations + +import numpy as np +import pytest # noqa: F401 + +import awkward as ak + + +def test(): + arr = ak.Array([{"x": 1}, {"x": 2}]) + # Fill with + result = ak.full_like(arr, np.datetime64(20, "s"), dtype="