apache / arrow-nanoarrow

Helpers for Arrow C Data & Arrow C Stream interfaces
https://arrow.apache.org/nanoarrow
Apache License 2.0
169 stars 35 forks source link

python: provide better error message for na.Array(..) from iterable without schema #423

Closed jorisvandenbossche closed 5 months ago

jorisvandenbossche commented 5 months ago

Running

import nanoarrow as na
na.Array([0, 1, 2, None])

gives

ValueError                                Traceback (most recent call last)
File [~/scipy/repos/arrow-nanoarrow/python/src/nanoarrow/c_lib.py:363](http://localhost:8888/lab/tree/repos/arrow-nanoarrow/python/src/nanoarrow/c_lib.py#line=362), in c_array_stream(obj, schema)
    362 try:
--> 363     array = c_array(obj, schema=schema)
    364     return CArrayStream.from_array_list([array], array.schema, validate=False)

File [~/scipy/repos/arrow-nanoarrow/python/src/nanoarrow/c_lib.py:179](http://localhost:8888/lab/tree/repos/arrow-nanoarrow/python/src/nanoarrow/c_lib.py#line=178), in c_array(obj, schema)
    178 if _obj_is_iterable(obj):
--> 179     return _c_array_from_iterable(obj, schema)
    181 raise TypeError(
    182     f"Can't convert object of type {type(obj).__name__} to nanoarrow.c_array"
    183 )

File [~/scipy/repos/arrow-nanoarrow/python/src/nanoarrow/c_lib.py:607](http://localhost:8888/lab/tree/repos/arrow-nanoarrow/python/src/nanoarrow/c_lib.py#line=606), in _c_array_from_iterable(obj, schema)
    606 if schema is None:
--> 607     raise ValueError("schema is required for CArray import from iterable")
    609 obj_len = -1

ValueError: schema is required for CArray import from iterable

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
Cell In[2], line 1
----> 1 na.Array([0, 1, 2, None])

File [~/scipy/repos/arrow-nanoarrow/python/src/nanoarrow/array.py:153](http://localhost:8888/lab/tree/repos/arrow-nanoarrow/python/src/nanoarrow/array.py#line=152), in Array.__init__(self, obj, schema, device)
    150     self._data = CMaterializedArrayStream.from_c_array(obj)
    151     return
--> 153 with c_array_stream(obj, schema=schema) as stream:
    154     self._data = CMaterializedArrayStream.from_c_array_stream(stream)

File [~/scipy/repos/arrow-nanoarrow/python/src/nanoarrow/c_lib.py:366](http://localhost:8888/lab/tree/repos/arrow-nanoarrow/python/src/nanoarrow/c_lib.py#line=365), in c_array_stream(obj, schema)
    364     return CArrayStream.from_array_list([array], array.schema, validate=False)
    365 except Exception as e:
--> 366     raise TypeError(
    367         f"Can't convert object of type {type(obj).__name__} "
    368         "to nanoarrow.c_array_stream or nanoarrow.c_array"
    369     ) from e

TypeError: Can't convert object of type list to nanoarrow.c_array_stream or nanoarrow.c_array

The message at the end (what you typically look at) is not super clear why this would not be possible to convert a list (it is possible), while the error in the middle actually has the correct message ("schema is required for CArray import from iterable")