From a0e75e48744b542b90bf74a6ef2a0d75aadc5c0d Mon Sep 17 00:00:00 2001 From: Iason Krommydas Date: Fri, 31 May 2024 15:22:22 +0200 Subject: [PATCH 1/2] add without_field, untested yet --- src/dask_awkward/__init__.py | 1 + src/dask_awkward/lib/__init__.py | 1 + src/dask_awkward/lib/structure.py | 41 +++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/src/dask_awkward/__init__.py b/src/dask_awkward/__init__.py index 9b205092..34b5c4f5 100644 --- a/src/dask_awkward/__init__.py +++ b/src/dask_awkward/__init__.py @@ -100,6 +100,7 @@ with_field, with_name, with_parameter, + without_field, without_parameters, zeros_like, zip, diff --git a/src/dask_awkward/lib/__init__.py b/src/dask_awkward/lib/__init__.py index 90835ff7..74d16d6c 100644 --- a/src/dask_awkward/lib/__init__.py +++ b/src/dask_awkward/lib/__init__.py @@ -86,6 +86,7 @@ with_field, with_name, with_parameter, + without_field, without_parameters, zeros_like, zip, diff --git a/src/dask_awkward/lib/structure.py b/src/dask_awkward/lib/structure.py index ae5f104f..d7e52ed1 100644 --- a/src/dask_awkward/lib/structure.py +++ b/src/dask_awkward/lib/structure.py @@ -70,6 +70,7 @@ "values_astype", "where", "with_field", + "without_field", "with_name", "with_parameter", "without_parameters", @@ -1083,6 +1084,46 @@ def with_field( ) +class _WithoutFieldFn: + def __init__( + self, + highlevel: bool, + behavior: Mapping | None = None, + attrs: Mapping[str, Any] | None = None, + ) -> None: + self.highlevel = highlevel + self.behavior = behavior + self.attrs = attrs + + def __call__(self, array: ak.Array, where: str) -> ak.Array: + return ak.without_field( + array, where=where, behavior=self.behavior, attrs=self.attrs + ) + + +@borrow_docstring(ak.without_field) +def without_field( + base: Array, + where: str, + highlevel: bool = True, + behavior: Mapping | None = None, + attrs: Mapping[str, Any] | None = None, +) -> Array: + if not highlevel: + raise ValueError("Only highlevel=True is supported") + + if not isinstance(base, Array): + raise ValueError("Base argument in without_field must be a dask_awkward.Array") + + return map_partitions( + _WithoutFieldFn(highlevel=highlevel, behavior=behavior, attrs=attrs), + base, + where, + label="without-field", + output_divisions=1, + ) + + class _WithNameFn: def __init__( self, From 3a38bf614981831ed11b3d1eb8404201318fcd46 Mon Sep 17 00:00:00 2001 From: Iason Krommydas Date: Fri, 31 May 2024 15:51:14 +0200 Subject: [PATCH 2/2] some testing --- tests/test_structure.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/tests/test_structure.py b/tests/test_structure.py index d196ee35..f602e11e 100644 --- a/tests/test_structure.py +++ b/tests/test_structure.py @@ -212,20 +212,20 @@ def test_pad_none(axis: int, target: int) -> None: def test_with_field(caa: ak.Array, daa: dak.Array) -> None: - assert_eq( - ak.with_field(caa["points"], caa["points"]["x"], where="xx"), - dak.with_field(daa["points"], daa["points"]["x"], where="xx"), - ) + new_caa = ak.with_field(caa["points"], caa["points"]["x"], where="xx") + new_daa = dak.with_field(daa["points"], daa["points"]["x"], where="xx") + assert_eq(new_caa, new_daa) + assert_eq(ak.without_field(new_caa, "xx"), ak.without_field(new_daa, "xx")) - assert_eq( - ak.with_field(caa["points"], 1, where="xx"), - dak.with_field(daa["points"], 1, where="xx"), - ) + new_caa = ak.with_field(caa["points"], 1, where="xx") + new_daa = dak.with_field(daa["points"], 1, where="xx") + assert_eq(new_caa, new_daa) + assert_eq(ak.without_field(new_caa, "xx"), ak.without_field(new_daa, "xx")) - assert_eq( - ak.with_field(caa["points"], 1.0, where="xx"), - dak.with_field(daa["points"], 1.0, where="xx"), - ) + new_caa = ak.with_field(caa["points"], 1.0, where="xx") + new_daa = dak.with_field(daa["points"], 1.0, where="xx") + assert_eq(new_caa, new_daa) + assert_eq(ak.without_field(new_caa, "xx"), ak.without_field(new_daa, "xx")) with pytest.raises( ValueError, @@ -233,6 +233,15 @@ def test_with_field(caa: ak.Array, daa: dak.Array) -> None: ): _ = dak.with_field([{"foo": 1.0}, {"foo": 2.0}], daa.points.x, where="x") + with pytest.raises( + ValueError, + match="Base argument in without_field must be a dask_awkward.Array", + ): + _ = dak.without_field( + [{"foo": [1.0, 2.0], "bar": [3.0, 4.0]}], + "bar", + ) + with pytest.raises( ValueError, match="with_field cannot accept string, bytes, list, or dict values yet",