Add a "path" to ak.with_field and array["outer", "inner", "new_field"] = new_field syntax to ak.Array/Record.
See original GitHub issueI got quite confused when i (wrongly) assumed that i can assign fields to arrays
>>> import numpy as np
>>> offsets = np.random.randint(0, 10, size=5)
>>> x = ak.zip({k : [np.random(off) for off in offsets] for k in ["a", "b"]})
>>> d = 2 * x.a
>>> x.d = d # wrongly assumed this is equivalent to x["d"] = d
>>> x = x[x.a > 0.9] # ups, now x.d is gone
Would it make sense/be possible to implement __setattr__ to do the same thing as __setitem__? Or if that introduces other problems, warn users they should not do that?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
No results found
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I’ve been on the fence about this because all the other attributes that attach to an
ak.Arrayhave to be attached carefully (by directly accessing the__dict__or calling the superclass’s__getattr__or__getattribute__or something; I’d have to look it up and experiment).I’ve asked this question when presenting Awkward, whether the argument for
x.d = dis strong enough to warrant the potential for issues like, “Oops, now we can’t add new features toak.Array,” or “Oops, now exceptions that happen inside the assignment (likeddoesn’t broadcast tox) are hidden, or a wrong error message is raised.” Something very much like that happened with__getattr__until we found a way to navigate around all the consequences. In that case, the argument for__getattr__access was strong enough to do it.Also something to consider: Pandas used to have this ability and now they’ve deprecated it and raise warnings if users attempt it.
(But they do allow
df.x is df["x"].)I don’t know if they hit some bad design issues or something. Maybe a warning is in order? I normally don’t like warnings because they tend to not do what they’re supposed to do—they often get buried in a script with no one watching to see them, or when they are observed, they pertain to something so many steps away from what the user is doing that the warning doesn’t make sense. But since Pandas does it, maybe we should too…
I think I’m going to label this as a “discussion” because what we have to think about goes well beyond the simple matter of implementing
__setattr__.I changed the title to be clear about what this feature request is requesting. I’m also going to add “good first issue” because this can be implemented with only a little Python meddling, not deep changes to the C++.