This is an automated email from the ASF dual-hosted git repository.

paleolimbot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-nanoarrow.git


The following commit(s) were added to refs/heads/main by this push:
     new 6ff2dcbf fix(python): avoid carrying over the null count from the 
parent array to a slice (#1036)
6ff2dcbf is described below

commit 6ff2dcbf4503bf17b768012228d0422cc4e5b57f
Author: Edgar Ramírez Mondragón 
<[email protected]>
AuthorDate: Fri Nov 27 14:10:21 2026 -0600

    fix(python): avoid carrying over the null count from the parent array to a 
slice (#1036)
    
    The parent's null count was carrying over to the slice. That should only
    happen if the parent's null count is 0, or if the slice covers the whole
    array (`:`).
    
    ---------
    
    Signed-off-by: Edgar Ramírez-Mondragón <[email protected]>
---
 python/src/nanoarrow/_array.pyx |  4 ++++
 python/tests/test_c_array.py    | 18 ++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/python/src/nanoarrow/_array.pyx b/python/src/nanoarrow/_array.pyx
index 0cf41731..ed425f1f 100644
--- a/python/src/nanoarrow/_array.pyx
+++ b/python/src/nanoarrow/_array.pyx
@@ -401,6 +401,10 @@ cdef class CArray:
 
         c_array_out.offset = c_array_out.offset + start
         c_array_out.length = stop - start
+
+        if c_array_out.null_count != 0 and c_array_out.length != 
self._ptr.length:
+            c_array_out.null_count = -1
+
         cdef CArray out = CArray(base, <uintptr_t>c_array_out, self._schema)
         out._set_device(self._device_type, self._device_id, self._sync_event)
 
diff --git a/python/tests/test_c_array.py b/python/tests/test_c_array.py
index 9959bf03..4ff33551 100644
--- a/python/tests/test_c_array.py
+++ b/python/tests/test_c_array.py
@@ -133,6 +133,24 @@ def test_c_array_slice():
     assert array2.length == 2
 
 
+def test_c_array_slice_null_count():
+    array = na.c_array([1, None, 3, None], na.int64())
+    assert array.null_count == 2
+
+    # A full slice has the same null count as its parent
+    assert array[:].null_count == 2
+
+    # A null count of zero applies to any slice
+    no_nulls = na.c_array([1, 2, 3, 4], na.int64())
+    assert no_nulls[1:3].null_count == 0
+
+    # Otherwise, the null count of a slice is unknown until computed
+    array2 = array[2:3]
+    assert array2.null_count == -1
+    assert array2.view().null_count == 0
+    assert array[1:3].view().null_count == 1
+
+
 def test_c_array_slice_errors():
     array = na.c_array([1, 2, 3], na.int32())
 

Reply via email to