Skip to content

Commit

Permalink
nbytes attribute (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron authored Jul 31, 2024
1 parent 02892f4 commit 2cdec15
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions arro3-core/python/arro3/core/_core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Array:
target_type: Type to cast array to.
"""

@property
def nbytes(self) -> int: ...
def slice(self, offset: int = 0, length: int | None = None) -> Array:
"""Compute zero-copy slice of this array.
Expand Down Expand Up @@ -126,6 +128,8 @@ class ChunkedArray:
def equals(self, other: ArrowStreamExportable) -> bool: ...
def length(self) -> int: ...
@property
def nbytes(self) -> int: ...
@property
def null_count(self) -> int: ...
@property
def num_chunks(self) -> int: ...
Expand Down Expand Up @@ -696,6 +700,8 @@ class RecordBatch:
_description_
"""
@property
def nbytes(self) -> int: ...
@property
def num_columns(self) -> int:
"""Number of columns."""
@property
Expand Down Expand Up @@ -1122,6 +1128,8 @@ class Table:
_description_
"""
@property
def nbytes(self) -> int: ...
@property
def num_columns(self) -> int:
"""Number of columns in this table."""
@property
Expand Down
5 changes: 5 additions & 0 deletions pyo3-arrow/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ impl PyArray {
Ok(PyArray::new(new_array, new_field.into()).to_arro3(py)?)
}

#[getter]
fn nbytes(&self) -> usize {
self.array.get_array_memory_size()
}

#[pyo3(signature = (offset=0, length=None))]
pub fn slice(&self, py: Python, offset: usize, length: Option<usize>) -> PyResult<PyObject> {
let length = length.unwrap_or_else(|| self.array.len() - offset);
Expand Down
7 changes: 7 additions & 0 deletions pyo3-arrow/src/chunked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,13 @@ impl PyChunkedArray {
self.len()
}

#[getter]
fn nbytes(&self) -> usize {
self.chunks
.iter()
.fold(0, |acc, batch| acc + batch.get_array_memory_size())
}

#[getter]
pub fn null_count(&self) -> usize {
self.chunks
Expand Down
5 changes: 5 additions & 0 deletions pyo3-arrow/src/record_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ impl PyRecordBatch {
PyField::new(field.clone().into()).to_arro3(py)
}

#[getter]
fn nbytes(&self) -> usize {
self.0.get_array_memory_size()
}

/// Number of columns in this RecordBatch.
#[getter]
pub fn num_columns(&self) -> usize {
Expand Down
7 changes: 7 additions & 0 deletions pyo3-arrow/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,13 @@ impl PyTable {
Ok(PyField::new(field.clone().into()).to_arro3(py)?)
}

#[getter]
fn nbytes(&self) -> usize {
self.batches
.iter()
.fold(0, |acc, batch| acc + batch.get_array_memory_size())
}

#[getter]
pub fn num_columns(&self) -> usize {
self.schema.fields().len()
Expand Down

0 comments on commit 2cdec15

Please sign in to comment.