ENH: np.roll: extra argument to pad with zeros or constant
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 32.8k
- Forks
- 12.8k
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 197
Description
Proposed new feature or change:
In many situations, it is required to shift an array (like np.roll) and fill the values with zeros, instead of making the array "loop".
Is there a way to implement this:
x = np.array([1, 2, 3, 4, 5]
np.roll(x, 2, method="constant") # [ 0 0 1 2 3 ] instead of [ 4 5 1 2 3 ]
np.roll(x, -2, method="constant") # [ 3 4 5 0 0 ] instead of [ 3 4 5 1 2 ]
?
Example : python numpy roll with padding
Important notes:
-
solutions with
np.padexist but are slower, see https://stackoverflow.com/questions/2777907/python-numpy-roll-with-padding#comment3270288_3153267 -
other people have the needs for this: https://stackoverflow.com/questions/2777907/python-numpy-roll-with-padding#comment120688507_2778068: I think np.roll should do it natively with a keyword argument. (quite upvoted)
-
this solution works: https://stackoverflow.com/a/2778068 but it would be interesting for the Numpy API to have this useful option (in the same way
np.padhasconstantmode, etc.)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the np.roll API and review how its current wrapping behavior is implemented. Compare the requested zero and constant-fill examples, then determine the API and behavior changes needed; done means the new option works for positive and negative shifts without changing existing np.roll behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100