Skip to content

Commit eac62cf

Browse files
authored
DOC: Clarify output of diff (returned type and possible overflow) (#32699)
Co-authored-by: mproszewska <[email protected]>
1 parent 36c3a60 commit eac62cf

File tree

2 files changed

+71
-67
lines changed

2 files changed

+71
-67
lines changed

pandas/core/frame.py

+20-35
Original file line numberDiff line numberDiff line change
@@ -7148,40 +7148,14 @@ def melt(
71487148
# ----------------------------------------------------------------------
71497149
# Time series-related
71507150

7151-
def diff(self, periods: int = 1, axis: Axis = 0) -> "DataFrame":
7152-
"""
7153-
First discrete difference of element.
7154-
7155-
Calculates the difference of a DataFrame element compared with another
7156-
element in the DataFrame (default is the element in the same column
7157-
of the previous row).
7158-
7159-
Parameters
7160-
----------
7161-
periods : int, default 1
7162-
Periods to shift for calculating difference, accepts negative
7163-
values.
7164-
axis : {0 or 'index', 1 or 'columns'}, default 0
7165-
Take difference over rows (0) or columns (1).
7166-
7167-
Returns
7168-
-------
7169-
DataFrame
7170-
7171-
See Also
7172-
--------
7173-
Series.diff: First discrete difference for a Series.
7174-
DataFrame.pct_change: Percent change over given number of periods.
7175-
DataFrame.shift: Shift index by desired number of periods with an
7176-
optional time freq.
7177-
7178-
Notes
7179-
-----
7180-
For boolean dtypes, this uses :meth:`operator.xor` rather than
7181-
:meth:`operator.sub`.
7182-
7183-
Examples
7184-
--------
7151+
@doc(
7152+
Series.diff,
7153+
klass="Dataframe",
7154+
extra_params="axis : {0 or 'index', 1 or 'columns'}, default 0\n "
7155+
"Take difference over rows (0) or columns (1).\n",
7156+
other_klass="Series",
7157+
examples=dedent(
7158+
"""
71857159
Difference with previous row
71867160
71877161
>>> df = pd.DataFrame({'a': [1, 2, 3, 4, 5, 6],
@@ -7237,7 +7211,18 @@ def diff(self, periods: int = 1, axis: Axis = 0) -> "DataFrame":
72377211
3 -1.0 -2.0 -9.0
72387212
4 -1.0 -3.0 -11.0
72397213
5 NaN NaN NaN
7240-
"""
7214+
7215+
Overflow in input dtype
7216+
7217+
>>> df = pd.DataFrame({'a': [1, 0]}, dtype=np.uint8)
7218+
>>> df.diff()
7219+
a
7220+
0 NaN
7221+
1 255.0"""
7222+
),
7223+
)
7224+
def diff(self, periods: int = 1, axis: Axis = 0) -> "DataFrame":
7225+
72417226
bm_axis = self._get_block_manager_axis(axis)
72427227
self._consolidate_inplace()
72437228

pandas/core/series.py

+51-32
Original file line numberDiff line numberDiff line change
@@ -2314,38 +2314,12 @@ def cov(self, other, min_periods=None) -> float:
23142314
return np.nan
23152315
return nanops.nancov(this.values, other.values, min_periods=min_periods)
23162316

2317-
def diff(self, periods: int = 1) -> "Series":
2318-
"""
2319-
First discrete difference of element.
2320-
2321-
Calculates the difference of a Series element compared with another
2322-
element in the Series (default is element in previous row).
2323-
2324-
Parameters
2325-
----------
2326-
periods : int, default 1
2327-
Periods to shift for calculating difference, accepts negative
2328-
values.
2329-
2330-
Returns
2331-
-------
2332-
Series
2333-
First differences of the Series.
2334-
2335-
See Also
2336-
--------
2337-
Series.pct_change: Percent change over given number of periods.
2338-
Series.shift: Shift index by desired number of periods with an
2339-
optional time freq.
2340-
DataFrame.diff: First discrete difference of object.
2341-
2342-
Notes
2343-
-----
2344-
For boolean dtypes, this uses :meth:`operator.xor` rather than
2345-
:meth:`operator.sub`.
2346-
2347-
Examples
2348-
--------
2317+
@doc(
2318+
klass="Series",
2319+
extra_params="",
2320+
other_klass="DataFrame",
2321+
examples=dedent(
2322+
"""
23492323
Difference with previous row
23502324
23512325
>>> s = pd.Series([1, 1, 2, 3, 5, 8])
@@ -2379,6 +2353,51 @@ def diff(self, periods: int = 1) -> "Series":
23792353
4 -3.0
23802354
5 NaN
23812355
dtype: float64
2356+
2357+
Overflow in input dtype
2358+
2359+
>>> s = pd.Series([1, 0], dtype=np.uint8)
2360+
>>> s.diff()
2361+
0 NaN
2362+
1 255.0
2363+
dtype: float64"""
2364+
),
2365+
)
2366+
def diff(self, periods: int = 1) -> "Series":
2367+
"""
2368+
First discrete difference of element.
2369+
2370+
Calculates the difference of a {klass} element compared with another
2371+
element in the {klass} (default is element in previous row).
2372+
2373+
Parameters
2374+
----------
2375+
periods : int, default 1
2376+
Periods to shift for calculating difference, accepts negative
2377+
values.
2378+
{extra_params}
2379+
Returns
2380+
-------
2381+
{klass}
2382+
First differences of the Series.
2383+
2384+
See Also
2385+
--------
2386+
{klass}.pct_change: Percent change over given number of periods.
2387+
{klass}.shift: Shift index by desired number of periods with an
2388+
optional time freq.
2389+
{other_klass}.diff: First discrete difference of object.
2390+
2391+
Notes
2392+
-----
2393+
For boolean dtypes, this uses :meth:`operator.xor` rather than
2394+
:meth:`operator.sub`.
2395+
The result is calculated according to current dtype in {klass},
2396+
however dtype of the result is always float64.
2397+
2398+
Examples
2399+
--------
2400+
{examples}
23822401
"""
23832402
result = algorithms.diff(self.array, periods)
23842403
return self._constructor(result, index=self.index).__finalize__(

0 commit comments

Comments
 (0)