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

feat: add dask_awkward.without_field #508

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/dask_awkward/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
with_field,
with_name,
with_parameter,
without_field,
without_parameters,
zeros_like,
zip,
Expand Down
1 change: 1 addition & 0 deletions src/dask_awkward/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
with_field,
with_name,
with_parameter,
without_field,
without_parameters,
zeros_like,
zip,
Expand Down
41 changes: 41 additions & 0 deletions src/dask_awkward/lib/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"values_astype",
"where",
"with_field",
"without_field",
"with_name",
"with_parameter",
"without_parameters",
Expand Down Expand Up @@ -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,
Expand Down
33 changes: 21 additions & 12 deletions tests/test_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,27 +212,36 @@ 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,
match="Base argument in with_field must be a dask_awkward.Array",
):
_ = 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",
Expand Down
Loading