Skip to main content

PyArrow extension types for storing fixed-shape and ragged tensors

Project description

ndarrow

PyArrow extension types for efficiently storing tensors with variable or fixed shapes.

Overview

ndarrow provides two complementary PyArrow-native storage formats for tensor data:

  • TensorArray — every element has exactly the same shape; the entire batch is backed by a single contiguous buffer.
  • RaggedTensorArray — each element is a tensor whose leading dimension may vary, with a fixed inner_shape shared by all elements.

Both types store the element shape and NumPy dtype in the Arrow extension metadata, so they round-trip correctly through IPC and Parquet without any extra configuration.

Installation

pip install ndarrow

Usage

import numpy as np
import pyarrow as pa
import pyarrow.parquet as pq
from ndarrow import TensorArray, RaggedTensorArray

# Fixed-shape: one 64-dim embedding per sentence
sentence_embeddings = TensorArray.from_numpy(np.random.randn(3, 64))
# also accepts a list of arrays:
# sentence_embeddings = TensorArray.from_numpy([
#     np.random.randn(64)
#     for _ in range(3)
# ])

# Ragged: each sentence has a variable number of tokens, each with a 64-dim embedding
token_embeddings = RaggedTensorArray.from_numpy([
    np.random.randn(6, 64),
    np.random.randn(9, 64),
    np.random.randn(3, 64),
])

table = pa.table({"sentence_embeddings": sentence_embeddings, "token_embeddings": token_embeddings})
print(table.schema)
# sentence_embeddings: extension<ndarrow.tensor<TensorType>>
# token_embeddings:    extension<ndarrow.ragged_tensor<RaggedTensorType>>

# Round-trip through Parquet — type metadata is preserved
pq.write_table(table, "data.parquet")
table2 = pq.read_table("data.parquet")

embeddings_np = table2.column("sentence_embeddings").chunk(0).to_numpy()  # shape (3, 64)
tokens_list   = table2.column("token_embeddings").chunk(0).to_numpy()     # list of 3 arrays of shape (?, 64)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ndarrow-0.1.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ndarrow-0.1.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file ndarrow-0.1.0.tar.gz.

File metadata

  • Download URL: ndarrow-0.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for ndarrow-0.1.0.tar.gz
Algorithm Hash digest
SHA256 be631dd80c06d1a0e1005e026d305128af993af726ffc6f77bfda6efc31e548b
MD5 f7b54c6866486a90147e15f6755648ff
BLAKE2b-256 8749ce42f142178bef2db9bfd992024b6f4afcc780514677f15f600c5a976a96

See more details on using hashes here.

File details

Details for the file ndarrow-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ndarrow-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for ndarrow-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 af8d3e208081e1c9c038691e522a86540ea75cfa476f900ea84298bb15dd7a12
MD5 46fdc1cefed2df05cbb58bfcf5694439
BLAKE2b-256 491f5c89c84304912e03347dce42b6481740e94e25a29558a4c7fcbef2ae756d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page