-
Notifications
You must be signed in to change notification settings - Fork 89
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
ak.Array constructor from {"key": whole_array} #156
Comments
Actually, I was just thinking about getting to work on #77, the The signature ak.Array({'a': [1,2,3], 'b': [4,5,6,7,8]}) should be forbidden, since the data between parentheses is actually a Record. Let's call that a bug-fix: it shouldn't turn a dict into an array of its keys. The error message should probably point out that if you want a record, you can build a record: ak.Record({'a': [1,2,3], 'b': [4,5,6,7,8]}) but that constructor doesn't invoke |
I'm marking this as a duplicate because it's very similar to what the |
Yes I see, thanks for the feedback! |
I'm going to open a PR to handle these constructor issues. I'm not sure how much it will try to address, but there will be a place to look.
|
I really should have linked these issues to the PR. The signature you were asking for is available in >>> import awkward1 as ak
>>> array = ak.zip({"x": ak.Array([[1, 2], [], [3]]), "y": ak.Array([1.1, 2.2, 3.3])})
>>> print(array)
[{x: [1, 2], y: 1.1}, {x: [], y: 2.2}, {x: [3], y: 3.3}]
>>> ak.typeof(array)
3 * {"x": var * int64, "y": float64} Since Python lists are "array-like," they can be used for small examples: >>> array = ak.zip({"x": [[1, 2], [], [3]], "y": [1.1, 2.2, 3.3]})
>>> print(array)
[{x: [1, 2], y: 1.1}, {x: [], y: 2.2}, {x: [3], y: 3.3}]
>>> ak.typeof(array)
3 * {"x": var * int64, "y": float64} It would be confusing to allow this syntax in the >>> not_array = ak.Record({"x": [[1, 2], [], [3]], "y": [1.1, 2.2, 3.3]})
>>> print(not_array)
{x: [[1, 2], [], [3]], y: [1.1, 2.2, 3.3]}
>>> ak.typeof(not_array)
{"x": var * var * int64, "y": var * float64}
>>> ak.Array({"x": [[1, 2], [], [3]], "y": [1.1, 2.2, 3.3]})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/jpivarski/irishep/awkward-1.0/awkward1/highlevel.py", line 34, in __init__
raise TypeError("could not convert dict into an awkward1.Array; try awkward1.Record")
TypeError: could not convert dict into an awkward1.Array; try awkward1.Record |
In one of my analysis frameworks I introduced a
Table
class, which is a thin wrapper tonumpy.recarray
and has also a constructor which takes a dictionary as input. It became quite popular among our users due to the ability to quickly create type-safe 2D recarrays. Here is an exampleThe inspiration came from Pandas which offers a similar constructor:
With awkward array, the corresponding constructor only does a "
for element in dict
"-like iteration, which in Python boils down to an iteration over the dictionary.keys()
. This might confuse people who already worked with pandas.DataFrames or similar classes. For the sake of completeness, here is what we get when passing a dictionary (no surprise for us but I post it here in for future reference):With pandas it'
Of course the
DataFrame
orTable
classes are mostly simple 2D table with the requirement of equal lengths for each field (that's why it e.g. auto-repeat-expands single value attributes) but I think this constructor would be a handy addition toakward.Arrays
, especially since it accepts awkward data shapes and layouts 😉What do you think? I am not sure if I find time to come up with a solid PR until 1.0.
The text was updated successfully, but these errors were encountered: