Hello Numpy community,
I'm proposing the introduction of a `pipe` method for NumPy arrays to enhance
their usability and expressiveness.
Similar to other data processing libraries like pandas, a `pipe` method would
allow users to chain operations together in a more readable and intuitive
manner.
Consider the following examples where method chaining with `pipe` can improve
code readability compared to traditional NumPy code:
# ********************************************************************
# Class PipeableArray just for illustration
import numpy as np
class PipeableArray:
def __init__(self, array: np.ndarray):
self.array = array
def pipe(self, func, *args, **kwargs):
"""Apply function and return the result wrapped in PipeableArray."""
try:
result = func(self.array, *args, **kwargs)
return PipeableArray(result)
except Exception as exc:
print('Ups, something went wrong...')
def __repr__(self):
return repr(self.array)
# ********************************************************************
# Original code using traditional NumPy chaining
arr = np.array([1, 2, 3, 4, 5])
arr = np.square(arr)
arr = np.log(arr)
arr = np.cumsum(arr)
# Original code using traditional NumPy nested functions
arr = np.arange(1., 5.)
result = np.cumsum(np.log(np.square(arr)))
# ********************************************************************
# Proposed Numpy method chaining using a new pipe method
arr = PipeableArray(np.arange(1., 5.))
result = (arr
.pipe(np.square)
.pipe(np.log)
.pipe(np.cumsum)
)
# ********************************************************************
Benefits:
- Readability: Method chaining with pipe offers a more readable and intuitive
way to express complex data transformations, making the intended data
processing pipeline easier to understand.
- Customization: The pipe method allows users to chain custom functions or
already implemented NumPy operations seamlessly.
- Modularity: Users can define reusable functions and chain them together using
pipe, leading to cleaner and more maintainable code.
- Consistency: Introducing a pipe method in NumPy aligns with similar
functionality available in other libraries like pandas, polars, etc.
- Optimization: While NumPy may not currently optimize chained expressions, the
introduction of pipe lays the groundwork for potential future optimizations
with lazy evaluation.
I believe this enhancement could benefit the NumPy community by providing a
more flexible and expressive way to work with arrays.
I'd love to see such a feature in Numpy and like to hear your thoughts on this
proposal.
Best regards,
Oyibo
_______________________________________________
NumPy-Discussion mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: [email protected]