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:
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.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:
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()- Arithmetic operations (
add,multiply, etc.) sort(),argsort()
Summary: choosing the right tool
Next steps
Broadcasting
How arrays with different shapes combine in operations.
Array Basics
NDArray properties, creation, and conversion.