Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cpp/src/arrow/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ static inline std::shared_ptr<Buffer> SliceBuffer(const std::shared_ptr<Buffer>&
return std::make_shared<Buffer>(buffer, offset, length);
}

static inline std::shared_ptr<Buffer> SliceBuffer(const std::shared_ptr<Buffer>& buffer,
const int64_t offset) {
int64_t length = buffer->size() - offset;
return SliceBuffer(buffer, offset, length);
}

/// Construct a mutable buffer slice. If the parent buffer is not mutable, this
/// will abort in debug builds
ARROW_EXPORT
Expand Down
2 changes: 0 additions & 2 deletions python/pyarrow/array.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,6 @@ cdef class Array:
raise NotImplemented

def __getitem__(self, key):
cdef Py_ssize_t n = len(self)

if PySlice_Check(key):
return _normalize_slice(self, key)

Expand Down
5 changes: 5 additions & 0 deletions python/pyarrow/includes/libarrow.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil:
c_bool is_mutable() const
c_bool Equals(const CBuffer& other)

shared_ptr[CBuffer] SliceBuffer(const shared_ptr[CBuffer]& buffer,
int64_t offset, int64_t length)
shared_ptr[CBuffer] SliceBuffer(const shared_ptr[CBuffer]& buffer,
int64_t offset)

cdef cppclass CMutableBuffer" arrow::MutableBuffer"(CBuffer):
CMutableBuffer(const uint8_t* data, int64_t size)

Expand Down
37 changes: 35 additions & 2 deletions python/pyarrow/io.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -645,8 +645,41 @@ cdef class Buffer:
return pyarrow_wrap_buffer(parent_buf)

def __getitem__(self, key):
# TODO(wesm): buffer slicing
raise NotImplementedError
if PySlice_Check(key):
return _normalize_slice(self, key)

return self.getitem(_normalize_index(key, self.size))

cdef getitem(self, int64_t i):
return self.buffer.get().data()[i]

def slice(self, offset=0, length=None):
"""
Compute slice of this buffer

Parameters
----------
offset : int, default 0
Offset from start of buffer to slice
length : int, default None
Length of slice (default is until end of Buffer starting from
offset)

Returns
-------
sliced : Buffer
"""
cdef shared_ptr[CBuffer] result

if offset < 0:
raise IndexError('Offset must be non-negative')

if length is None:
result = SliceBuffer(self.buffer, offset)
else:
result = SliceBuffer(self.buffer, offset, max(length, 0))

return pyarrow_wrap_buffer(result)

def equals(self, Buffer other):
"""
Expand Down
1 change: 1 addition & 0 deletions python/pyarrow/lib.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ cdef class Buffer:
Py_ssize_t strides[1]

cdef void init(self, const shared_ptr[CBuffer]& buffer)
cdef getitem(self, int64_t i)


cdef class ResizableBuffer(Buffer):
Expand Down
51 changes: 51 additions & 0 deletions python/pyarrow/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,57 @@ def ne(a, b):
eq(buf2, buf5)


def test_buffer_getitem():
data = bytearray(b'some data!')
buf = pa.py_buffer(data)

n = len(data)
for ix in range(-n, n - 1):
assert buf[ix] == data[ix]

with pytest.raises(IndexError):
buf[n]

with pytest.raises(IndexError):
buf[-n - 1]


def test_buffer_slicing():
data = b'some data!'
buf = pa.py_buffer(data)

sliced = buf.slice(2)
expected = pa.py_buffer(b'me data!')
assert sliced.equals(expected)

sliced2 = buf.slice(2, 4)
expected2 = pa.py_buffer(b'me d')
assert sliced2.equals(expected2)

# 0 offset
assert buf.slice(0).equals(buf)

# Slice past end of buffer
assert len(buf.slice(len(buf))) == 0

with pytest.raises(IndexError):
buf.slice(-1)

# Test slice notation
assert buf[2:].equals(buf.slice(2))
assert buf[2:5].equals(buf.slice(2, 3))
assert buf[-5:].equals(buf.slice(len(buf) - 5))
with pytest.raises(IndexError):
buf[::-1]
with pytest.raises(IndexError):
buf[::2]

n = len(buf)
for start in range(-n * 2, n * 2):
for stop in range(-n * 2, n * 2):
assert buf[start:stop].to_pybytes() == buf.to_pybytes()[start:stop]


def test_buffer_hashing():
# Buffers are unhashable
with pytest.raises(TypeError, match="unhashable"):
Expand Down