Skip to content

ENH: Use binary right shift as pipe operator #34925

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
JulianWgs opened this issue Jun 21, 2020 · 3 comments
Closed

ENH: Use binary right shift as pipe operator #34925

JulianWgs opened this issue Jun 21, 2020 · 3 comments

Comments

@JulianWgs
Copy link

Is your feature request related to a problem?

In R there is the pipe operator "%>%" which applies the function after the operator on the data before the operator. Pandas also follows this philosophy of method chaining. There is also a pipe function. Here is a good comparison of R vs pandas: https://stmorse.github.io/journal/tidyverse-style-pandas.html

Pandas is missing a stylish pipe operator.

Describe the solution you'd like

In [1]: import pandas as pd
   ...: import numpy as np
   ...:
   ...: def pipe(self, func):
   ...:     return func(self)
   ...:
   ...: pd.DataFrame.__rshift__ = pipe
   ...: pd.Series.__rshift__ = pipe

In [2]: df = pd.DataFrame({"A": [0, 1], "B": [2, 3]})
   ...: df
Out[2]:
   A  B
0  0  2
1  1  3

The old style. Execution from right to left or with pipe function.

In [3]: np.mean(np.sum(df))
Out[3]: 3.0

In [4]: df.pipe(np.sum).pipe(np.mean)
Out[4]: 3.0

With the pipe operator. Execution from left to right with pipe operator.

In [5]: df >> np.sum >> np.mean
Out[5]: 3.0

In [6]: df >> print
   A  B
0  0  2
1  1  3

I think the pipe operator makes it much more minimal.

API breaking implications

See above.

Describe alternatives you've considered

I picked the binary shift operator, because it is not currently used within pandas. I think it resembles a pipe the best.

Additional context

This does not work out of the box if the function takes additional argumements. One could use a lambda function, but that does not look clean. I think this is also the bigget downfall of this proposal.

@JulianWgs JulianWgs added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 21, 2020
@jreback
Copy link
Contributor

jreback commented Jun 22, 2020

This was already debated and was passed

-1 on this non python syntax.

I think there is a package that actually implements this on top of pandas if you want, but not for core pandas.

@JulianWgs
Copy link
Author

JulianWgs commented Jun 22, 2020

Hey, thanks for the fast answer. Could you link that discussion? I would then gladly close the issue :)

Edit: I think I've found the package: https://github.com/dodger487/dplython

Edit2: Since there already is an implementation I will close this issue.

@jreback
Copy link
Contributor

jreback commented Jun 22, 2020

there are many just search for pipe
#27400 #26809

@bashtage bashtage removed the Needs Triage Issue that has not been reviewed by a pandas team member label Aug 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants