Skip to content

fix: ->jvm on object-dtype numpy arrays with np-array bindings#288

Open
jsavyasachi wants to merge 1 commit into
clj-python:masterfrom
jsavyasachi:fix-object-ndarray-187
Open

fix: ->jvm on object-dtype numpy arrays with np-array bindings#288
jsavyasachi wants to merge 1 commit into
clj-python:masterfrom
jsavyasachi:fix-object-ndarray-187

Conversation

@jsavyasachi

@jsavyasachi jsavyasachi commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Fixes #187.

With the libpython-clj2.python.np-array zero-copy bindings loaded, ->jvm on an object-dtype array threw Unable to find datatype: object:

(require-python '[numpy :as np])
(require '[libpython-clj2.python.np-array])
(py/->jvm (np/array [] :dtype "object"))
;; => ExceptionInfo "Unable to find datatype: object"

Without the bindings it returns [] (the default sequence conversion).

Cause: the pyobject->jvm :ndarray method the bindings register always runs numpy->desc -> obj-dtype->dtype, which only maps numeric dtypes (int/uint/float). Object/str/datetime arrays hold python-object pointers, not a numeric native buffer, so they can't be zero-copied and have no mapping.

Fix: only take the tensor path when the dtype is numeric; otherwise fall back to the default element-wise copy conversion (the same path used when the bindings aren't loaded).

Added a regression test to numpy_test.clj (empty and mixed object arrays).

The np-array zero-copy bindings register a pyobject->jvm :ndarray method that
always maps the numpy dtype to a native tensor datatype. Object and other
non-numeric dtypes have no such mapping, so ->jvm threw "Unable to find
datatype: object" once the bindings were loaded. Route non-numeric dtypes to
the default element-wise copy conversion instead.

Closes clj-python#187
@jsavyasachi jsavyasachi force-pushed the fix-object-ndarray-187 branch from 17c00bf to f68dc36 Compare June 23, 2026 04:55
@jsavyasachi jsavyasachi changed the title fix: ->jvm on object-dtype numpy arrays with np-array bindings (#187) fix: ->jvm on object-dtype numpy arrays with np-array bindings Jun 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

->jvm on ndarray of type 'object' fails

1 participant