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

C++ refactoring: ak.fields #1296

Merged
merged 1 commit into from
Feb 18, 2022
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
34 changes: 16 additions & 18 deletions src/awkward/_v2/operations/describe/ak_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@


def fields(array):
raise NotImplementedError


# """
# Extracts record fields or tuple slot numbers from `array` (many types
# supported, including all Awkward Arrays and Records).

# If the array contains nested records, only the outermost record is
# queried. If it contains tuples instead of records, this function outputs
# string representations of integers, such as `"0"`, `"1"`, `"2"`, etc.
# The records or tuples may be within multiple layers of nested lists.

# If the array contains neither tuples nor records, this returns an empty
# list.
# """
# layout = ak._v2.operations.convert.to_layout(
# array, allow_record=True, allow_other=False
# )
# return layout.keys()
"""
Extracts record fields or tuple slot numbers from `array` (many types
supported, including all Awkward Arrays and Records).

If the array contains nested records, only the outermost record is
queried. If it contains tuples instead of records, this function outputs
string representations of integers, such as `"0"`, `"1"`, `"2"`, etc.
The records or tuples may be within multiple layers of nested lists.

If the array contains neither tuples nor records, this returns an empty
list.
"""
layout = ak._v2.operations.convert.to_layout(
array, allow_record=True, allow_other=False
)
return layout.fields
13 changes: 4 additions & 9 deletions tests/v2/test_0273-path-for-with-field.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@


def test_one_level():

base = ak.zip({"a": ak.zip({"x": [1, 2, 3]}), "b": [1, 2, 3]}, depth_limit=1)
what = ak.Array([1.1, 2.2, 3.3], check_valid=True)
assert ak.to_list(ak.with_field(base, what, where=["a", "y"])) == [
{"b": 1, "a": {"x": 1, "y": 1.1}},
{"b": 2, "a": {"x": 2, "y": 2.2}},
{"b": 3, "a": {"x": 3, "y": 3.3}},
]
base = ak._v2.operations.structure.zip(
{"a": ak._v2.operations.structure.zip({"x": [1, 2, 3]}), "b": [1, 2, 3]},
depth_limit=1,
Expand All @@ -29,6 +21,7 @@ def test_one_level():
{"b": 2, "a": {"x": 2, "y": 2.2}},
{"b": 3, "a": {"x": 3, "y": 3.3}},
]
assert ak._v2.operations.describe.fields(base) == ["a", "b"]

base["a", "y"] = what
assert to_list(base) == [
Expand Down Expand Up @@ -56,6 +49,7 @@ def test_two_level():
{"B": 2, "A": {"b": 2, "a": {"x": 2, "y": 2.2}}},
{"B": 3, "A": {"b": 3, "a": {"x": 3, "y": 3.3}}},
]
assert ak._v2.operations.describe.fields(base) == ["A", "B"]

base["A", "a", "y"] = what
assert to_list(base) == [
Expand All @@ -77,6 +71,7 @@ def test_replace_the_only_field():
{"a": {"x": 2, "y": 2.2}},
{"a": {"x": 3, "y": 3.3}},
]
assert ak._v2.operations.describe.fields(base) == ["a"]

base["a", "y"] = what
assert to_list(base) == [
Expand Down Expand Up @@ -110,9 +105,9 @@ def test_check_no_field():
(2, 2.2),
(3, 3.3),
]

assert to_list(ak._v2.operations.structure.with_field(recordarray, what, "a")) == [
{"0": 1, "a": 1.1},
{"0": 2, "a": 2.2},
{"0": 3, "a": 3.3},
]
assert ak._v2.operations.describe.fields(recordarray) == ["0"]
7 changes: 7 additions & 0 deletions tests/v2/test_0286-broadcast-single-value-with-field.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ def test_broadcast_single_bool():
base_new1, base.x > 0.3, "sometimes_true"
)
assert to_list(base_new2.always_true) == [[True, True]]
assert ak._v2.operations.describe.fields(base_new2) == [
"x",
"y",
"z",
"always_true",
"sometimes_true",
]
1 change: 1 addition & 0 deletions tests/v2/test_0879-non-primitive-with-field.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ def test_in_place_wrapper_broadcasting():
array["unknown field"] = None

assert array["unknown field"].tolist() == [None, None, None]
assert ak._v2.operations.describe.fields(array) == ["x", "unknown field"]