Skip to main content

NumPy structured array utilities — joining, flattening, field views, enum mapping, and position arrays

Project description

NumPy Utils

NumPy structured array utilities — joining, flattening, field views, enum mapping, and position arrays.

Installation

pip install vcti-nputils>=1.0.0

In pyproject.toml dependencies

dependencies = [
    "vcti-nputils>=1.0.0",
]

Quick Start

import numpy as np
from vcti.nputils import (
    join_struct_arrays,
    flatten_record_dtype,
    fields_view,
    drop_fields,
    rename_fields,
    name_array,
    position_array,
    as_ndarray,
)

# Join structured arrays horizontally
dt1 = np.dtype([('id', 'i4'), ('value', 'f8')])
dt2 = np.dtype([('name', 'U10')])
arr1 = np.array([(1, 1.5), (2, 2.5)], dtype=dt1)
arr2 = np.array([('Alice',), ('Bob',)], dtype=dt2)
joined = join_struct_arrays([arr1, arr2])
# dtype: [('id', 'i4'), ('value', 'f8'), ('name', 'U10')]

# Create a zero-copy view with selected fields
view = fields_view(joined, ['id', 'name'])

# Drop fields from a structured array (zero-copy)
clean = drop_fields(joined, ['value'])
# dtype: [('id', 'i4'), ('name', 'U10')]

# Rename fields in a dtype
new_dt = rename_fields(dt1, {'id': 'node_id', 'value': 'temperature'})
# dtype: [('node_id', 'i4'), ('temperature', 'f8')]

# Flatten array fields into individual columns
dt = np.dtype([('id', 'i4'), ('coords', 'f8', (3,))])
flat_dt, cols = flatten_record_dtype(dt)
# cols: ['id', 'coord_0', 'coord_1', 'coord_2']

# Map numeric enum values to names
enum_dict = {1: 'ACTIVE', 2: 'INACTIVE', 3: 'PENDING'}
names = name_array(np.array([1, 2, 1, 3]), enum_dict)

# Convert counts to cumulative offsets
offsets = position_array(np.array([3, 2, 4, 1]))
# array([0, 3, 5, 9, 10])

# Safely coerce inputs to ndarray
arr = as_ndarray([1, 2, 3], dtype=np.float64)
empty = as_ndarray(None)  # array([], dtype=float64)

Functions

Function Purpose
join_struct_arrays(arrays) Join structured arrays horizontally by combining fields
fields_view(sa, fields) Zero-copy view with selected fields only
drop_fields(sa, exclude) Zero-copy view with specified fields excluded
rename_fields(dt, mapping) Return a new dtype with fields renamed
flatten_record_dtype(dt) Expand array fields into individual scalar fields
name_array(nparray, enum_dict) Map numeric values to string names
position_array(counts, dtype) Convert count array to cumulative offset array
as_ndarray(value, dtype) Coerce None, list, or ndarray to ndarray

Dependencies

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

vcti_nputils-1.0.0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

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

vcti_nputils-1.0.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file vcti_nputils-1.0.0.tar.gz.

File metadata

  • Download URL: vcti_nputils-1.0.0.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for vcti_nputils-1.0.0.tar.gz
Algorithm Hash digest
SHA256 809a25bb40af401a4a268319942e32a0899efb3390c3c088bde70404c6fab210
MD5 a0e3e962c252157571c90ae073cd6583
BLAKE2b-256 1e74ca3b161376ddbe78ac26d5710cc6deb1efec904d79ceff5ccb3e8d8abe90

See more details on using hashes here.

Provenance

The following attestation bundles were made for vcti_nputils-1.0.0.tar.gz:

Publisher: publish.yml on vcollab/vcti-python-nputils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file vcti_nputils-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: vcti_nputils-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for vcti_nputils-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 303bac38257459aa58d60e49f66fc194e5300cc34fcf6aa196e3b97df5cc5833
MD5 67c73c0965118e7a376960d8bb7d8f5e
BLAKE2b-256 cac9ce930f85e1a42c4ecd21fc1d285f3b971e62b395f073730c0a3008bcac13

See more details on using hashes here.

Provenance

The following attestation bundles were made for vcti_nputils-1.0.0-py3-none-any.whl:

Publisher: publish.yml on vcollab/vcti-python-nputils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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