Skip to content

Commit 85a9e08

Browse files
committed
special handling for object ararys
1 parent bae0ba2 commit 85a9e08

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/_arraykit.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3573,7 +3573,7 @@ array_to_tuple_array(PyObject *Py_UNUSED(m), PyObject *a)
35733573
i++;
35743574
}
35753575
}
3576-
else { // ndim == 1
3576+
else if (PyArray_TYPE(input_array) != NPY_OBJECT) { // ndim == 1, not object
35773577
while (p < p_end) {
35783578
tuple = PyTuple_New(1);
35793579
if (tuple == NULL) {
@@ -3590,6 +3590,22 @@ array_to_tuple_array(PyObject *Py_UNUSED(m), PyObject *a)
35903590
i++;
35913591
}
35923592
}
3593+
else { // ndim == 1, object
3594+
while (p < p_end) {
3595+
tuple = PyTuple_New(1);
3596+
if (tuple == NULL) {
3597+
goto error;
3598+
}
3599+
// scalar returned in is native PyObject from object arrays
3600+
item = *(PyObject**)PyArray_GETPTR1(input_array, i);
3601+
Py_INCREF(item);
3602+
// TODO: identify tuple
3603+
PyTuple_SET_ITEM(tuple, 0, item); // steals reference to item
3604+
*p++ = tuple; // assign with new ref, no incr needed
3605+
i++;
3606+
}
3607+
}
3608+
35933609
PyArray_CLEARFLAGS((PyArrayObject *)output, NPY_ARRAY_WRITEABLE);
35943610
return output;
35953611
error:

test/test_util.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,9 @@ def test_array2d_to_array1d_1d_c(self) -> None:
303303

304304
def test_array2d_to_array1d_1d_d(self) -> None:
305305
a1 = np.array([('a', 10), ('b', 30), ('c', 5)], dtype=object)
306-
a2 = array_to_tuple_array(a1)
307-
self.assertEqual(a2.tolist(), [('a', 10), ('b', 30), ('c', 5)])
306+
a2 = array_to_tuple_array(a1) # from 2d
307+
a3 = array_to_tuple_array(a2) # from 1d
308+
self.assertEqual(a3.tolist(), [('a', 10), ('b', 30), ('c', 5)])
308309

309310
def test_array2d_to_array1d_1d_e(self) -> None:
310311
a1 = np.array([True, False, True], dtype=object)

0 commit comments

Comments
 (0)