feature request: Consider adding a strides method to np.dtype
See original GitHub issueIt would do something like this:
def strides_from_dtype(dtype):
shape = list(dtype.shape)
# Make the strides for an array with an itemsize of 1 in C-order.
tmp_strides = shape[::-1]
tmp_strides[1:] = list(np.cumprod(tmp_strides[:-1]))
tmp_strides[0] = 1
# Adjust it for the real itemsize.
tmp_strides = dtype.base.itemsize * np.array(tmp_strides)
# Convert it to a tuple, reversing it back for proper C-order.
return tuple(tmp_strides[::-1])
This works, but it seems inefficient:
strides = np.zeros((), dtype=dtype).strides
Issue Analytics
- State:
- Created 5 years ago
- Comments:29 (29 by maintainers)
Top Results From Across the Web
Support for datetime[*] numpy dtype - scikit-hep/awkward
To work around this, we'd have to detect it with array.dtype().kind() == 'M' and then get the data pointer, shape, strides, itemsize, and...
Read more >numpy.ndarray.strides — NumPy v1.24 Manual
The strides of an array tell us how many bytes we have to skip in memory to move to the next position along...
Read more >Advanced NumPy: Master stride tricks with 25 illustrated ...
The answer — the strides and shape to be used as parameters in numpy.lib.stride_tricks.as_strided to achieve the final NumPy view. The explanation; The...
Read more >Supported NumPy features - Numba
NumPy arrays provide an efficient storage method for homogeneous sets of data. NumPy dtypes provide type information useful when compiling, and the regular, ......
Read more >Typed Memoryviews — Cython 3.0.0a11 documentation
Memory views use Python slicing syntax in a similar way as NumPy. ... To use a memory view on a numpy array with...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@njsmith: I see where you’re coming from, but I think we should explicitly support this:
some_real_array[empty_slice]
, to allow them to be used similarly to C++ iterators / pointersRegarding my post above - here’s an implementation of some syntactic sugar to solve your problem https://gist.github.com/eric-wieser/d8790dede1c9311c5670018ee1cee83a
With this, the offset and strides for
a['time']['subtime'][2]
can be gotten with justThanks for the inspiration, @njsmith: here’s a neat solution I came up with