-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Docstring pandas.data frame.diff #20227
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
Merged
TomAugspurger
merged 11 commits into
pandas-dev:master
from
adatasetaday:docstring_pandas.DataFrame.diff
Mar 11, 2018
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7c7e9dc
DOC: update the pandas.DataFrame.diff docstring
adatasetaday 2e84d6f
DOC: update the pandas.DataFrame.diff docstring
adatasetaday 5b61330
DOC: update the pandas.DataFrame.diff docstring
adatasetaday 785dae1
DOC: update the pandas.DataFrame.diff docstring
adatasetaday 8019d85
DOC: update the pandas.DataFrame.diff docstring
adatasetaday 15bcb7d
DOC: update the pandas.DataFrame.diff docstring
adatasetaday 736310e
DOC: update the pandas.DataFrame.diff docstring
adatasetaday 2d3242d
DOC: update the pandas.DataFrame.diff docstring
adatasetaday 51206f5
DOC: update the pandas.DataFrame.diff docstring
adatasetaday 2802e1b
DOC: update the pandas.DataFrame.diff docstring
adatasetaday 3a07a0f
Cleanup
TomAugspurger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4722,20 +4722,87 @@ def melt(self, id_vars=None, value_vars=None, var_name=None, | |
|
||
def diff(self, periods=1, axis=0): | ||
""" | ||
1st discrete difference of object | ||
First discrete difference of element. | ||
|
||
Calculates the difference of a DataFrame element compared with another | ||
element in the DataFrame (default is element in same column of the | ||
previous row). | ||
|
||
Parameters | ||
---------- | ||
periods : int, default 1 | ||
Periods to shift for forming difference | ||
Periods to shift for calculating difference, accepts negative | ||
values. | ||
axis : {0 or 'index', 1 or 'columns'}, default 0 | ||
Take difference over rows (0) or columns (1). | ||
|
||
.. versionadded:: 0.16.1 | ||
.. versionadded:: 0.16.1. | ||
|
||
Returns | ||
------- | ||
diffed : DataFrame | ||
|
||
See Also | ||
-------- | ||
DataFrame.pct_change: Percent change over given number of periods. | ||
Series.diff: First discrete difference of object | ||
|
||
Examples | ||
-------- | ||
Difference with previous row | ||
|
||
>>> df = pd.DataFrame({'a': [1, 2, 3, 4, 5, 6], | ||
... 'b': [1, 1, 2, 3, 5, 8], | ||
... 'c': [1, 4, 9, 16, 25, 36]}) | ||
>>> df | ||
a b c | ||
0 1 1 1 | ||
1 2 1 4 | ||
2 3 2 9 | ||
3 4 3 16 | ||
4 5 5 25 | ||
5 6 8 36 | ||
>>> df.diff() | ||
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. add a blank line between cases |
||
a b c | ||
0 NaN NaN NaN | ||
1 1.0 0.0 3.0 | ||
2 1.0 1.0 5.0 | ||
3 1.0 1.0 7.0 | ||
4 1.0 2.0 9.0 | ||
5 1.0 3.0 11.0 | ||
|
||
Difference with previous column | ||
|
||
>>> df.diff(axis=1) | ||
a b c | ||
0 NaN 0.0 0.0 | ||
1 NaN -1.0 3.0 | ||
2 NaN -1.0 7.0 | ||
3 NaN -1.0 13.0 | ||
4 NaN 0.0 20.0 | ||
5 NaN 2.0 28.0 | ||
|
||
Difference with 3rd previous row | ||
|
||
>>> df.diff(periods=3) | ||
a b c | ||
0 NaN NaN NaN | ||
1 NaN NaN NaN | ||
2 NaN NaN NaN | ||
3 3.0 2.0 15.0 | ||
4 3.0 4.0 21.0 | ||
5 3.0 6.0 27.0 | ||
|
||
Difference with following row | ||
|
||
>>> df.diff(periods=-1) | ||
a b c | ||
0 -1.0 0.0 -3.0 | ||
1 -1.0 -1.0 -5.0 | ||
2 -1.0 -1.0 -7.0 | ||
3 -1.0 -2.0 -9.0 | ||
4 -1.0 -3.0 -11.0 | ||
5 NaN NaN NaN | ||
""" | ||
bm_axis = self._get_block_manager_axis(axis) | ||
new_data = self._data.diff(n=periods, axis=bm_axis) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
add DataFrame.shift