ENH: Proposal to add shift functionality to array (follow up)
Hello everyone, I hope this is the correct place to post this as this is my first (potential) contribution to numpy. I would like to implement a function numpy.shift which would work similarly to numpy.roll, with one key difference. After shifting an array by a value `shift`, the function fills the missing values with a `fill_value` For example shifting the following array by 1 along axis 1with fill_value of 0
arr = numpy.arange(10).reshape((2,5)) arr array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) numpy.shift(arr, 1, axis=1, fill_value=0)
array([[0, 0, 1, 2, 3], [0, 5, 6, 7, 8]]) Cheers, Filippo
On Tue, Sep 27, 2022 at 10:58 AM Filippo Tagliacarne < filippotagliacarne@gmail.com> wrote:
Hello everyone,
I hope this is the correct place to post this as this is my first (potential) contribution to numpy.
Hi Filippo, yes this is the correct place. Thanks for your proposal, and for being patient when no one replied the first time around.
I would like to implement a function numpy.shift which would work similarly to numpy.roll, with one key difference. After shifting an array by a value `shift`, the function fills the missing values with a `fill_value` For example shifting the following array by 1 along axis 1with fill_value of 0
arr = numpy.arange(10).reshape((2,5)) arr array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) numpy.shift(arr, 1, axis=1, fill_value=0)
array([[0, 0, 1, 2, 3], [0, 5, 6, 7, 8]])
This shift function is straightforward to implement on top of roll:
x = np.roll(arr, 1) x[:, 0] = 0 # fill_value x array([[0, 0, 1, 2, 3], [0, 5, 6, 7, 8]])
We prefer not to add new convenience functions like this to NumPy if they can be implemented in a couple of lines of code. There are a huge amount of such functions possible; it's better to implement those in the downstream library or end user code where it is needed. Cheers, Ralf
participants (2)
-
Filippo Tagliacarne
-
Ralf Gommers