Skip to content

Commit a6ae1af

Browse files
committed
Merge pull request #9727 from evanpw/diff_axis
ENH: Add an axis parameter to DataFrame.diff
2 parents c9d1ef9 + ec020d6 commit a6ae1af

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

doc/source/whatsnew/v0.16.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Enhancements
2323
~~~~~~~~~~~~
2424

2525
- Added ``StringMethods.capitalize()`` and ``swapcase`` which behave as the same as standard ``str`` (:issue:`9766`)
26+
- ``DataFrame.diff`` now takes an ``axis`` parameter that determines the direction of differencing (:issue:`9727`)
2627
- Added ``StringMethods`` (.str accessor) to ``Index`` (:issue:`9068`)
2728
- Allow clip, clip_lower, and clip_upper to accept array-like arguments as thresholds (:issue:`6966`). These methods now have an ``axis`` parameter which determines how the Series or DataFrame will be aligned with the threshold(s).
2829

pandas/core/frame.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -3586,20 +3586,22 @@ def unstack(self, level=-1):
35863586
#----------------------------------------------------------------------
35873587
# Time series-related
35883588

3589-
def diff(self, periods=1):
3589+
def diff(self, periods=1, axis=0):
35903590
"""
35913591
1st discrete difference of object
35923592
35933593
Parameters
35943594
----------
35953595
periods : int, default 1
35963596
Periods to shift for forming difference
3597+
axis : {0 or 'index', 1 or 'columns'}, default 0
35973598
35983599
Returns
35993600
-------
36003601
diffed : DataFrame
36013602
"""
3602-
new_data = self._data.diff(n=periods)
3603+
bm_axis = self._get_block_manager_axis(axis)
3604+
new_data = self._data.diff(n=periods, axis=bm_axis)
36033605
return self._constructor(new_data)
36043606

36053607
#----------------------------------------------------------------------

pandas/core/internals.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -874,9 +874,9 @@ def take_nd(self, indexer, axis, new_mgr_locs=None, fill_tuple=None):
874874
def get_values(self, dtype=None):
875875
return self.values
876876

877-
def diff(self, n):
877+
def diff(self, n, axis=1):
878878
""" return block for the diff of the values """
879-
new_values = com.diff(self.values, n, axis=1)
879+
new_values = com.diff(self.values, n, axis=axis)
880880
return [make_block(values=new_values,
881881
ndim=self.ndim, fastpath=True,
882882
placement=self.mgr_locs)]

pandas/tests/test_frame.py

+6
Original file line numberDiff line numberDiff line change
@@ -10100,6 +10100,12 @@ def test_diff_float_n(self):
1010010100
xp = self.tsframe.diff(1)
1010110101
assert_frame_equal(rs, xp)
1010210102

10103+
def test_diff_axis(self):
10104+
# GH 9727
10105+
df = DataFrame([[1., 2.], [3., 4.]])
10106+
assert_frame_equal(df.diff(axis=1), DataFrame([[np.nan, 1.], [np.nan, 1.]]))
10107+
assert_frame_equal(df.diff(axis=0), DataFrame([[np.nan, np.nan], [2., 2.]]))
10108+
1010310109
def test_pct_change(self):
1010410110
rs = self.tsframe.pct_change(fill_method=None)
1010510111
assert_frame_equal(rs, self.tsframe / self.tsframe.shift(1) - 1)

0 commit comments

Comments
 (0)