Unyt - Units for NumPy

3.1.0 · active · verified Thu Apr 16

unyt is a Python package that provides NumPy arrays with units, enabling dimensional consistency checks and automatic unit conversions. It is developed as part of the yt-project and is actively maintained with a regular release cadence, typically seeing minor releases every few months and bugfix releases more frequently. The current stable version is 3.1.0.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create unyt_array objects, perform basic arithmetic while maintaining units, and convert quantities between different unit systems. It also shows how to access pre-defined physical constants.

from unyt import unyt_array, g, cm, s, G

# Create unyt_arrays with units
mass = unyt_array([10, 20, 30], 'g')
length = unyt_array(5, cm)
time = unyt_array(2, s)

print(f"Initial mass: {mass}")

# Perform arithmetic operations
density = mass / (length**3)
print(f"Calculated density: {density}")

# Convert to different units
density_kg_m3 = density.to('kg/m**3')
print(f"Density in kg/m^3: {density_kg_m3}")

# Access constants
print(f"Gravitational constant: {G}")

view raw JSON →