Skip to content

Pipe Operator? #3050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
max-sixty opened this issue Jun 26, 2019 · 1 comment
Closed

Pipe Operator? #3050

max-sixty opened this issue Jun 26, 2019 · 1 comment

Comments

@max-sixty
Copy link
Collaborator

I realize this is a topic primed for bike-shedding, so I've held off suggesting, but here goes...

Is there any interest in a pipe operator to make xarray syntax easier?
Our internal code has much code like:

delinq_c = (
    base_ds['delinq_comp_array']
    # lots of `pipe` lines!
    .pipe(lambda x: x - base_ds['delinq_comp_array'].mean('fsym_id'))
    .pipe(lambda x: x / delinq_diff)
    .pipe(lambda x: x * 2)
)

...with lots of new lines starting with .pipe. The fluent code is great for sequences of logic, but the requirement to have .pipe on each line adds verbosity.

The addition of a pipe operator would allow for:

delinq_c = (
    base_ds['delinq_comp_array']
    >> lambda x: x - base_ds['delinq_comp_array'].mean('fsym_id')
    >> lambda x: x / delinq_diff
    >> lambda x: x * 2
)

This requires (ab)using an existing python operator, such as >> (bitshift operator) or | (or operator). Airflow uses >>, Beam (and bash) use |. While xarray doesn't allow either to be used, other libraries might incorrectly assume their presence implies they work consistently with those definitions.

Python has explicitly not added this, nor reduced the character count of lambda x:, so this would be somewhat a dissent from their standards, and introduce potential confusion to completely new xarray users.

I remember some discussions at pandas on similar topics. I can't find them all, but here's an issue re adding the X term pandas-dev/pandas#13133

@shoyer
Copy link
Member

shoyer commented Jun 27, 2019

I also use .pipe very heavily in my own code, so I appreciate the motivation here. That said, I've been pretty happy with writing .pipe().

Both >> and | are already operators in xarray/numpy, which complicates using them for another purpose. >> for bitshift may be unusual, but | is definitely widely used for elementwise or.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants