Documentation Index
Fetch the complete documentation index at: https://numpyts.dev/llms.txt
Use this file to discover all available pages before exploring further.
Python vs TypeScript syntax
Because TypeScript does not support Python’sarr[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’sstart:stop:step:
| Slice String | Meaning | Python equivalent |
|---|---|---|
':' | All elements | [:] |
'0:5' | Elements 0 through 4 | [0:5] |
'2:' | From index 2 to the end | [2:] |
':3' | First 3 elements | [:3] |
'-3:' | Last 3 elements | [-3:] |
'::2' | Every other element | [::2] |
'::-1' | Reversed | [::-1] |
'1:7:2' | From 1 to 6, step 2 | [1:7:2] |
'3' | Single index (reduces dimension) | [3] |
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:
Convenience methods: row(), col(), rows(), cols()
For 2-D arrays, numpy-ts provides shorthand methods that are cleaner than manual slicing:
| Method | Equivalent | Returns |
|---|---|---|
row(i) | .slice(String(i), ':') | 1-D array (single row) |
col(j) | .slice(':', String(j)) | 1-D array (single column) |
rows(start, stop) | .slice(, ':') | 2-D sub-matrix |
cols(start, stop) | .slice(':', ) | 2-D sub-matrix |
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:
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 operationsT/transpose()reshape()(when the array is C-contiguous)squeeze()/expand_dims()broadcast_to()
flatten()/copy()take()/iindex()/bindex()/vindex()- Arithmetic operations (
add,multiply, etc.) sort(),argsort()
Summary: choosing the right tool
| Task | Method | Returns |
|---|---|---|
| Contiguous sub-array | .slice('0:5') | View |
| Single element (read) | .get([i, j]) | Scalar |
| Single element (write) | .set([i, j], val) | Void (in-place) |
| Single row/column | .row(i), .col(j) | View |
| Row/column range | .rows(a, b), .cols(a, b) | View |
| Arbitrary integer positions | .take(indices) or .iindex(indices) | Copy |
| Multi-axis vectorized indexing | np.vindex(a, idx0, idx1, ...) | Copy |
| In-place by flat index | .put(indices, values) | Void (in-place) |
| Boolean mask selection | .bindex(mask) | Copy |
| Conditional pick from two arrays | np.where(cond, x, y) | Copy |
Next steps
Broadcasting
How arrays with different shapes combine in operations.
Array Basics
NDArray properties, creation, and conversion.