scikit-hep / awkward-0.x

Manipulate arrays of complex data structures as easily as Numpy.
BSD 3-Clause "New" or "Revised" License
215 stars 39 forks source link

JaggedArray.fromiter() functions fails for python lists #252

Closed sitongan closed 4 years ago

sitongan commented 4 years ago

170

awkward.JaggedArray.fromiter([[20.2, 10.1, 30.3, 50.5], [50.5], [60]])

fails on jupyter notebook only with numpy 1.18.5 and python 3.7.7 on awkward 0.12.22 build pyh9f0ad1d_0 from conda-forge

Update

for fresh conda environment with numpy 1.18.5 and python 3.7.7 on awkward 0.12.22 build pyh9f0ad1d_0 from conda-forge:

`--------------------------------------------------------------------------- TypeError Traceback (most recent call last)

in ----> 1 awkward.JaggedArray.fromiter([[20.2, 10.1, 30.3, 50.5], [50.5], [60]]) /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/awkward/array/jagged.py in fromiter(cls, iterable) 150 return cls.JaggedArray.fget(None)([], [], []) 151 else: --> 152 return awkward.generate.fromiter(iterable, awkwardlib=cls.awkward.fget(None)) 153 154 @classmethod /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/awkward/generate.py in fromiter(iterable, awkwardlib, **options) 440 fillable = fillable.append(obj, typeof(obj)) 441 --> 442 return fillable.finalize(**options) 443 444 def fromiterchunks(iterable, chunksize, awkwardlib=None, **options): /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/awkward/generate.py in finalize(self, **options) 216 217 def finalize(self, **options): --> 218 return self.awkwardlib.JaggedArray.fromoffsets(self.offsets, self.content.finalize(**options)) 219 220 class TableFillable(Fillable): /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/awkward/array/jagged.py in fromoffsets(cls, offsets, content) 154 @classmethod 155 def fromoffsets(cls, offsets, content): --> 156 offsets = cls._util_toarray(offsets, cls.INDEXTYPE, cls.numpy.ndarray) 157 return cls(offsets[:-1], offsets[1:], content) 158 /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/awkward/array/base.py in _util_toarray(cls, value, defaultdtype, passthrough) 319 else: 320 try: --> 321 return cls.numpy.frombuffer(value, dtype=getattr(value, "dtype", defaultdtype)).reshape(getattr(value, "shape", -1)) 322 except AttributeError: 323 if len(value) == 0: TypeError: a bytes-like object is required, not 'list'`
jpivarski commented 4 years ago

Sorry, I've been unable to reproduce it:

>>> import numpy
>>> import awkward
>>> numpy.__version__
'1.18.5'
>>> awkward.__version__      # not my development version; a fresh copy from conda-forge
'0.12.22'
>>> awkward.JaggedArray.fromiter([[20.2, 10.1, 30.3, 50.5], [50.5], [60]])
<JaggedArray [[20.2 10.1 30.3 50.5] [50.5] [60.0]] at 0x7fa44f193c90>
>>> awkward.fromiter([[20.2, 10.1, 30.3, 50.5], [50.5], [60]])
<JaggedArray [[20.2 10.1 30.3 50.5] [50.5] [60.0]] at 0x7fa44f193d50>

screenshot

But hopefully that narrows down your search—you might not be using the versions you think you're using (particularly in Jupyter; it sometimes gets packages from an entirely different installation) or maybe there's something else going on.

If you're just starting a project, you might want to consider Awkward1.

>>> import awkward1 as ak
>>> ak.Array([[20.2, 10.1, 30.3, 50.5], [50.5], [60]])
<Array [[20.2, 10.1, 30.3, 50.5, ... [60]] type='3 * var * float64'>

There's a ak.from_awkward0 and ak.to_awkward0 and you can load both Awkward0 and Awkward1 in the same project, so you can make a gradual transition if necessary.

sitongan commented 4 years ago

Confirmed that it's a versioning issue instead(sorry!), thanks!