Skip to main content

Python vs TypeScript syntax

Because TypeScript does not support Python’s arr[0:5, :] subscript syntax, numpy-ts uses string-based slicing via the .slice() method. Every slice dimension is a string argument.

String slice syntax

The slice syntax mirrors Python’s start:stop:step:

Basic slicing

All slicing returns a view — no data is copied. Modifications to the slice affect the original array.

Multi-dimensional slicing

Pass one string argument per dimension:
When you pass an integer as a string (e.g. '1'), that dimension is removed from the result, just like NumPy’s integer indexing. A range like '1:2' keeps the dimension.

Ellipses

The string '...' expands to the number of ':' slices needed to index all dimensions. In most cases, this means the length of the expanded selection tuple equals ndim. There may only be a single ellipsis present.

newaxis

The string 'newaxis' expands the dimensions of the result by one unit-length dimension. The added dimension is the position of the newaxis token in the indices.

Negative indices

Negative indices count from the end, just like Python:

Element access: get() and set()

For reading or writing individual elements, use get() and set() with an array of indices:
The get() and set() methods require one index per dimension. Passing the wrong number of indices throws an error.

Convenience methods: row(), col(), rows(), cols()

For 2-D arrays, numpy-ts provides shorthand methods that are cleaner than manual slicing:
These methods require at least 2 dimensions. Calling row() on a 1-D array throws an error.

Fancy indexing: take and put

take(indices, axis?)

Select elements at arbitrary positions along an axis. Returns a new array (not a view):

put(indices, values)

Modify elements at specified flat indices in-place:

Boolean indexing: bindex

Select elements where a boolean mask is true. Returns a new 1-D array containing only the matching elements:

Integer array indexing: iindex

Select elements using an array of integer indices along a given axis. Similar to NumPy’s fancy integer array indexing:

Conditional selection: where

The where function selects elements from one of two arrays based on a condition:
When called with only a condition, where returns the indices of non-zero elements:

Views vs copies

Understanding when an operation returns a view (shared data) versus a copy (new data) is important for both correctness and performance: Views (shared memory):
  • slice() — all slicing operations
  • T / transpose()
  • reshape() (when the array is C-contiguous)
  • squeeze() / expand_dims()
  • broadcast_to()
Copies (new data):
  • flatten() / copy()
  • take() / iindex() / bindex() / vindex()
  • Arithmetic operations (add, multiply, etc.)
  • sort(), argsort()
Check arr.base to determine if an array is a view. If base is not null, the array shares data with its base.

Summary: choosing the right tool

Next steps

Broadcasting

How arrays with different shapes combine in operations.

Array Basics

NDArray properties, creation, and conversion.