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

experimental for nested #79

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
66 changes: 66 additions & 0 deletions src/akimbo/exp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""Experimental code"""

import ast
import operator

import awkward as ak
import numba


@numba.njit
def is_arange(arr):
"""Does this look like an array made by arange

Awkward index arrays look like this if there are no nulls and it's not from
categorical/dictionaries.
"""
last = -1
for x in arr:
if x != last + 1:
return False
last = x
return True


op_map = {">": operator.gt, ">=": operator.ge, "==": operator.eq}


def query(arr: ak.Array, expr: str):
"""Perform a nested-pandas style query on inner dataframeish layer

Example:
>>> out = query(arr, "nested.t > 17")
"""
field_selector, op, value = expr.split()
value = ast.literal_eval(value)
fields = tuple(field_selector.split("."))
argument = ak.to_buffers(ak.flatten(arr.__getitem__(fields)))[-1]["node1-data"]
bools = op_map[op](argument, value) # dispatches to numpy

outer = ak.copy(arr[fields[0]])
inner0 = outer[fields[1]].layout
prev = None
inner = inner0

while True:
if len(inner) == len(bools):
break
inner, prev = inner._content, inner
if isinstance(inner, ak.contents.UnmaskedArray):
inner = inner.to_ByteMaskedArray(True)
prev._content = inner
inner._mask._data[:] = bools
elif isinstance(inner, ak.contents.ByteMaskedArray):
inner._mask._data &= bools
elif isinstance(inner, ak.contents.IndexedOptionArray):
inner._index._data = inner.index.data[bools]
return ak.with_field(arr, inner0, fields)


def rec_list_swap(arr: ak.Array, field: str | None = None) -> ak.Array:
"""Make a record-of-lists into a list-of-records, assuming the lists have the same lengths"""
record_of_lists = arr[field] if field else arr
list_of_records = ak.zip(
dict(zip(ak.fields(record_of_lists), ak.unzip(record_of_lists))), depth_limit=2
)
return ak.with_field(arr, list_of_records, field) if field else list_of_records
Loading