Skip to content

ENH: Use binary right shift as pipe operator #34925

Closed
@JulianWgs

Description

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.

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions