-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Docstring shift #21039
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
Docstring shift #21039
Changes from 2 commits
77f2a2b
c935896
a6ac960
c892655
3639e6c
61a4ae1
206b2da
c31d1a1
73a640f
5bee1e3
de0b7a1
739f8a1
a9550fa
67b6ee2
ab4e2f5
e7d6f0a
5bcae81
17f55d5
4dd276a
0b4f41b
7c15b08
950cc2e
9ec5ed7
888b97b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7807,6 +7807,66 @@ def mask(self, cond, other=np.nan, inplace=False, axis=None, level=None, | |
is not realigned. That is, use freq if you would like to extend the | ||
index when shifting and preserve the original data. | ||
|
||
Examples | ||
-------- | ||
Compute the difference between a column in a dataframe and | ||
its shifted version | ||
|
||
>>> data = pd.DataFrame({'mydate': [pd.to_datetime('2016-06-06'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is useful to have a named index as the label (mydate) is used later in the process (for the left join). Would you prefer if I use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. How about
|
||
... pd.to_datetime('2016-06-08'), | ||
... pd.to_datetime('2016-06-09'), | ||
... pd.to_datetime('2016-06-10'), | ||
... pd.to_datetime('2016-06-12'), | ||
... pd.to_datetime('2016-06-13')], | ||
... 'myvalue': [1, 2, 3, 4, 5, 6], | ||
... 'group': ['A', 'A', 'A', 'B', 'B', 'B']}) | ||
|
||
>>> data.set_index('mydate', inplace=True) | ||
>>> data | ||
myvalue group | ||
mydate | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this indented too far? I thought the leftmost output should be directly below the leftmost There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. noted |
||
2016-06-06 1 A | ||
2016-06-08 2 A | ||
2016-06-09 3 A | ||
2016-06-10 4 B | ||
2016-06-12 5 B | ||
2016-06-13 6 B | ||
|
||
For the groups compute the difference between current *myvalue* and | ||
*myvalue* shifted forward by 1 day | ||
|
||
>>> result = data.groupby('group').myvalue.apply( | ||
... lambda x: x - x.shift(1, pd.Timedelta('1 days'))) | ||
>>> result | ||
group mydate | ||
A 2016-06-06 NaN | ||
2016-06-07 NaN | ||
2016-06-08 NaN | ||
2016-06-09 1.0 | ||
2016-06-10 NaN | ||
B 2016-06-10 NaN | ||
2016-06-11 NaN | ||
2016-06-12 NaN | ||
2016-06-13 1.0 | ||
2016-06-14 NaN | ||
Name: myvalue, dtype: float64 | ||
|
||
Merge result as a column named *delta* to the original data | ||
|
||
>>> result.name = 'delta' | ||
>>> data.reset_index().merge( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have had a play and looks like the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #21220 for that. I think
|
||
... result.reset_index(), | ||
... how='left', | ||
... on=['mydate', 'group']).set_index('mydate') | ||
myvalue group delta | ||
mydate | ||
2016-06-06 1 A NaN | ||
2016-06-08 2 A NaN | ||
2016-06-09 3 A 1.0 | ||
2016-06-10 4 B NaN | ||
2016-06-12 5 B NaN | ||
2016-06-13 6 B 1.0 | ||
|
||
Returns | ||
------- | ||
shifted : %(klass)s | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
End with a
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
noted