Skip to content

Commit 8149e03

Browse files
committed
fix slicing issue with memview
1 parent e35b769 commit 8149e03

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

pandas/_libs/arrays.pyx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,15 +481,16 @@ cdef class BitmaskArray:
481481
return bool(ArrowBitGet(self.bitmap.buffer.data, ckey))
482482
elif is_null_slice(key):
483483
return self.copy()
484-
elif isinstance(key, slice):
484+
elif isinstance(key, slice) and self.ndim == 1:
485485
# fastpath for slices that start at 0 and step 1 at a time
486486
# towards a positive number.
487487
# TODO: upstream generic ArrowBitsGet function in nanoarrow
488488
PySlice_Unpack(key, &start, &stop, &step)
489489
if start == 0 and stop > 0 and step == 1:
490+
if stop > self_.bitmap.size_bits:
491+
stop = self_.bitmap.size_bits
492+
490493
nbytes = (stop + 7) // 8
491-
if nbytes > self_.bitmap.size_bits:
492-
nbytes = self_.bitmap.size_bits
493494

494495
bma = BitmaskArray.__new__(BitmaskArray)
495496
ArrowBitmapInit(&bitmap)
@@ -501,7 +502,7 @@ cdef class BitmaskArray:
501502
bma.bitmap = bitmap
502503
bma.buffer_owner = True
503504
bma.ndim = self_.ndim
504-
bma.shape = self_.shape
505+
bma.shape[0] = stop
505506
bma.strides = self_.strides
506507
bma.parent = False
507508

pandas/tests/arrays/masked/test_bitmask.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,3 +460,10 @@ def test_memoryview(array, expected):
460460
bma = BitmaskArray(array)
461461
vw = memoryview(bma)
462462
assert vw.tolist() == expected
463+
464+
465+
def test_bitmask_array_shape_from_sliced_bitmask():
466+
orig_bma = BitmaskArray([True] * 100)
467+
bma = BitmaskArray(orig_bma[:10])
468+
469+
assert bma.shape == (10,)

0 commit comments

Comments
 (0)