paleolimbot commented on code in PR #378:
URL: https://github.com/apache/arrow-nanoarrow/pull/378#discussion_r1484934081
##########
python/src/nanoarrow/c_lib.py:
##########
@@ -125,10 +138,205 @@ def c_array(obj=None, requested_schema=None) -> CArray:
out = CArray.allocate(CSchema.allocate())
obj._export_to_c(out._addr(), out.schema._addr())
return out
- else:
+
+ # Try buffer protocol (e.g., numpy arrays)
+ try:
+ return c_array_from_pybuffer(obj)
+ except Exception as e:
raise TypeError(
f"Can't convert object of type {type(obj).__name__} to
nanoarrow.c_array"
+ ) from e
+
+
+def c_array_from_pybuffer(obj) -> CArray:
Review Comment:
That would definitely be more Pythonic! I think when there is an `Array`
class that lives in Python, `Array.from_buffers()` is absolutely the way to go.
This put me down a bit of a rabbit hole trying to find something that was
better/raised fewer eyebrows, and I settled on solidifying `c_array()` as the
one stop shop for "get me Arrow data". Like `pyarrow.array()` and
`numpy.array()`, they accept existing arrays, things with the right protocol
methods, and iterables. I had sort of done this already but solidifying and
documenting that made it clear that `c_array_from_pybuffer()` should be
internal (it's now `_c_array_from_pybuffer()`). I don't *think* there's
un-workaround-able overlap...one could do `c_array(memoryview(obj))` to force
buffer protocol, or `c_array(list(obj))` to force build-by-iterable.
I also did that for `c_buffer()`, which similarly made it clear (to me,
maybe) that `c_buffer_from_iterable()` should be internal, and that
`c_buffer()` should be exported.
`c_array_from_buffers()` doesn't quite fit the `c_array()` idea (I have
something array like, get it to me in arrow format), so I kept that on its own.
There's room for it to move somewhere else if it turns out to not be the right
fit!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]