raulcd commented on code in PR #46618: URL: https://github.com/apache/arrow/pull/46618#discussion_r2111992828
########## python/pyarrow/src/arrow/python/helpers.cc: ########## @@ -81,14 +83,23 @@ PyObject* PyHalf_FromHalf(npy_half value) { return result; } -Status PyFloat_AsHalf(PyObject* obj, npy_half* out) { - if (PyArray_IsScalar(obj, Half)) { +PyObject* PyFloat_FromHalf(uint16_t value) { + // Convert the uint16_t Float16 value to a PyFloat object + arrow::util::Float16 half_val = arrow::util::Float16::FromBits(value); + PyObject* result = PyFloat_FromDouble(half_val.ToDouble()); + return result; +} + +Status PyFloat_AsHalf(PyObject* obj, uint16_t* out) { + if (PyFloat_Check(obj)) { + // Use Arrow's Float16 implementation instead of NumPy + float float_val = static_cast<float>(PyFloat_AsDouble(obj)); + arrow::util::Float16 half_val = arrow::util::Float16::FromFloat(float_val); + *out = half_val.bits(); + } else if (has_numpy() && PyArray_IsScalar(obj, Half)) { *out = PyArrayScalar_VAL(obj, Half); - return Status::OK(); - } else { - // XXX: cannot use npy_double_to_half() without linking with Numpy - return Status::TypeError("Expected np.float16 instance"); } Review Comment: I am returning an error now if we can't convert the PyFloat to Float16 -- 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: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org