Replies: 5 comments
-
I suppose that Awkward v1 had an awkward/src/awkward/_connect/numexpr.py Lines 69 to 71 in 6b49960 Before just making this public, I wanted to see if there's any way to register Awkward with NumExpr (like the way that it's registered with NumPy's ufuncs or Numba's JIT). I think that was something that was going to be added to NumExpr v3, but it doesn't look like NumExpr v3 is going to happen. NumExpr expressions would be great to turn into ufuncs, and then they could be used uniformly on any array type that extends NumPy (NEP-13, |
Beta Was this translation helpful? Give feedback.
-
Hi Jim,
Yes, I think that's what it does (with probably slight variations for different engines).
Not well enough, tried this one yesterday ;). The main issue for me are custom functions, e.g. def func2(a, b):
return a + b
expression = "a + b + func2(a, b)" which works for pandas.eval but cannot be done with ak._connect.numexpr.evaluate nor numexpr.evaluate (although in most cases things like Anyway, not part of this issue ;).
Of course :). Let me know if you need a hand or two. Beats writing a custom parser for these expressions. |
Beta Was this translation helpful? Give feedback.
-
No, I mean it's still hidden; you can't use it (without going through a module name that starts with an underscore). The one thing we can't do is make this slick, to have I'm also assuming that this isn't what you're looking for: >>> a = ak.Array([[1, 2, 3], [5, 6]])
>>> b = ak.Array([[2, 3, 4], [6, 7]])
>>> eval("a + b")
<Array [[3, 5, 7], [11, 13]] type='2 * var * int64'> Is it about wanting to do a single pass over the data? With something like the above (execution of the string in Python), If you're okay with an interface like this, >>> evaluate("a + b", a=a, b=b)
<Array [[3, 5, 7], [11, 13]] type='2 * var * int64'> it can be done like this: def evaluate(expression, **namespace):
arrays = {}
not_arrays = {}
for k, v in namespace.items():
if isinstance(v, (ak.Array, np.array)): # maybe also pd.Series...
arrays[k] = v
else:
not_arrays[k] = v
def action(layouts, **ignore):
if all(x.is_numpy for x in layouts):
ns = dict(not_arrays)
for name, value in zip(arrays.keys(), layouts):
ns[name] = value.data
return ak.contents.NumpyArray(ne.evaluate(expression, ns))
return ak.transform(action, *arrays.values()) This is, in a sense, a reimplementation of If what you really want is to get the |
Beta Was this translation helpful? Give feedback.
-
If the part that you really want is to pick up the variable names from the string, that is possible if it can be parsed. >>> print(ast.dump(ast.parse("a + b"), indent=" "))
Module(
body=[
Expr(
value=BinOp(
left=Name(id='a', ctx=Load()),
op=Add(),
right=Name(id='b', ctx=Load())))],
type_ignores=[]) One could then write a |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot Jim!
Which is fine for single underscores: result = ak._connect.numexpr.evaluate(expression) → works (for expressions without functions)
Yes. Although I care at the moment more about completeness than efficiency. Regarding your suggestions, indeed, these are the paths I tried over the weekend - and then I remembered Anyway, you are right, a custom implementation would be best as I wanted to extract a compute graph in its final form. For
I can use this information to create a compute graph or extract & evaluate the functions to make the expression compatible with numexpr. Thanks for the pointers! |
Beta Was this translation helpful? Give feedback.
-
Version of Awkward Array
2.0.7
Description and code to reproduce
I am trying to use
pandas.eval
(pandas == 1.5.3
) with awkward arrays.The minimal example for this is
The error I am getting is:
Patching the pandas code reveals that the value
t
in is_datetime isReplacing awkward arrays with numpy arrays works (
t = <class 'numpy.int64'>
).Not sure of the exact chain of events, but before I dig deeper, I thought I post this in case someone has an "of course, it's that thing" moment ;).
If you need me to drill deeper, please let me know what you need.
PS: Same issue with awkward 1.X, so I guess nobody tried
pandas.eval
with awkward arrays so far.Note: The reason I am trying to use pandas.eval over numexpr or awkward..evaluate is the ease of using custom functions (with all its downsides).
Beta Was this translation helpful? Give feedback.
All reactions