diff --git a/cpp/src/arrow/buffer.h b/cpp/src/arrow/buffer.h index 06160d7d4384..e79fc88b8b92 100644 --- a/cpp/src/arrow/buffer.h +++ b/cpp/src/arrow/buffer.h @@ -153,6 +153,12 @@ static inline std::shared_ptr SliceBuffer(const std::shared_ptr& return std::make_shared(buffer, offset, length); } +static inline std::shared_ptr SliceBuffer(const std::shared_ptr& 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 diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi index 9d5d3885be9a..d98bebc8eed7 100644 --- a/python/pyarrow/array.pxi +++ b/python/pyarrow/array.pxi @@ -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) diff --git a/python/pyarrow/includes/libarrow.pxd b/python/pyarrow/includes/libarrow.pxd index b2a86d7e2eb9..4c67256de74b 100644 --- a/python/pyarrow/includes/libarrow.pxd +++ b/python/pyarrow/includes/libarrow.pxd @@ -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) diff --git a/python/pyarrow/io.pxi b/python/pyarrow/io.pxi index da9ed518738b..7506d638ce2b 100644 --- a/python/pyarrow/io.pxi +++ b/python/pyarrow/io.pxi @@ -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): """ diff --git a/python/pyarrow/lib.pxd b/python/pyarrow/lib.pxd index a7e00b7ed5e4..653cecfe3d27 100644 --- a/python/pyarrow/lib.pxd +++ b/python/pyarrow/lib.pxd @@ -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): diff --git a/python/pyarrow/tests/test_io.py b/python/pyarrow/tests/test_io.py index 5de71d60c8fb..0b67d176884b 100644 --- a/python/pyarrow/tests/test_io.py +++ b/python/pyarrow/tests/test_io.py @@ -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"):