Skip to content

Allow diff & shift to take lists of periods? #19298

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
TomAugspurger opened this issue Jan 18, 2018 · 2 comments
Closed

Allow diff & shift to take lists of periods? #19298

TomAugspurger opened this issue Jan 18, 2018 · 2 comments
Labels
Datetime Datetime data dtype Enhancement Numeric Operations Arithmetic, Comparison, and Logical operations

Comments

@TomAugspurger
Copy link
Contributor

I've seen code like

In [36]: s = pd.Series(range(10))

In [37]: pd.concat([s.diff(i).rename(f"L{i}") for i in range(10)], axis=1)
Out[37]:
    L0   L1   L2   L3   L4   L5   L6   L7   L8   L9
0  0.0  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
1  0.0  1.0  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
2  0.0  1.0  2.0  NaN  NaN  NaN  NaN  NaN  NaN  NaN
3  0.0  1.0  2.0  3.0  NaN  NaN  NaN  NaN  NaN  NaN
4  0.0  1.0  2.0  3.0  4.0  NaN  NaN  NaN  NaN  NaN
5  0.0  1.0  2.0  3.0  4.0  5.0  NaN  NaN  NaN  NaN
6  0.0  1.0  2.0  3.0  4.0  5.0  6.0  NaN  NaN  NaN
7  0.0  1.0  2.0  3.0  4.0  5.0  6.0  7.0  NaN  NaN
8  0.0  1.0  2.0  3.0  4.0  5.0  6.0  7.0  8.0  NaN
9  0.0  1.0  2.0  3.0  4.0  5.0  6.0  7.0  8.0  9.0

Could we simplify that by allowing s.diff(range(10), prefix="L")?

How would we handle DataFrames? Prefix with the column name and a separator, like get_dummies?

@tliu30
Copy link

tliu30 commented Jul 10, 2018

Hi! Would love to make this a reality...

I took a first look at how this could work for diff for Series, and it seems pretty simple (will attach a possible solution in a pull request shortly).

For DataFrames...

  • when axis=0 i think we can get away with the same hack (also in pull request)
  • when axis=1...maybe we just don't allow them to use multiple periods if axis=1?

(for shift, could apply a similar if-else block for pandas.core.generic.NDFrame.shift)

@rhshadrach
Copy link
Member

Closing in favor of #44424

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Enhancement Numeric Operations Arithmetic, Comparison, and Logical operations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants