Skip to content

Commit ec020d6

Browse files
evanpwEvan Wright
authored and
Evan Wright
committed
ENH: Add an axis parameter to DataFrame.diff
1 parent 3703f74 commit ec020d6

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

doc/source/whatsnew/v0.16.1.txt

100644100755
+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Enhancements
1818
~~~~~~~~~~~~
1919

2020
- Added ``StringMethods.capitalize()`` and ``swapcase`` which behave as the same as standard ``str`` (:issue:`9766`)
21+
- ``DataFrame.diff`` now takes an ``axis`` parameter that determines the direction of differencing (:issue:`9727`)
2122
- Added ``StringMethods`` (.str accessor) to ``Index`` (:issue:`9068`)
2223

2324
The `.str` accessor is now available for both `Series` and `Index`.

pandas/core/frame.py

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

3587-
def diff(self, periods=1):
3587+
def diff(self, periods=1, axis=0):
35883588
"""
35893589
1st discrete difference of object
35903590
35913591
Parameters
35923592
----------
35933593
periods : int, default 1
35943594
Periods to shift for forming difference
3595+
axis : {0 or 'index', 1 or 'columns'}, default 0
35953596
35963597
Returns
35973598
-------
35983599
diffed : DataFrame
35993600
"""
3600-
new_data = self._data.diff(n=periods)
3601+
bm_axis = self._get_block_manager_axis(axis)
3602+
new_data = self._data.diff(n=periods, axis=bm_axis)
36013603
return self._constructor(new_data)
36023604

36033605
#----------------------------------------------------------------------

pandas/core/internals.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -869,9 +869,9 @@ def take_nd(self, indexer, axis, new_mgr_locs=None, fill_tuple=None):
869869
def get_values(self, dtype=None):
870870
return self.values
871871

872-
def diff(self, n):
872+
def diff(self, n, axis=1):
873873
""" return block for the diff of the values """
874-
new_values = com.diff(self.values, n, axis=1)
874+
new_values = com.diff(self.values, n, axis=axis)
875875
return [make_block(values=new_values,
876876
ndim=self.ndim, fastpath=True,
877877
placement=self.mgr_locs)]

pandas/tests/test_frame.py

+6
Original file line numberDiff line numberDiff line change
@@ -10047,6 +10047,12 @@ def test_diff_float_n(self):
1004710047
xp = self.tsframe.diff(1)
1004810048
assert_frame_equal(rs, xp)
1004910049

10050+
def test_diff_axis(self):
10051+
# GH 9727
10052+
df = DataFrame([[1., 2.], [3., 4.]])
10053+
assert_frame_equal(df.diff(axis=1), DataFrame([[np.nan, 1.], [np.nan, 1.]]))
10054+
assert_frame_equal(df.diff(axis=0), DataFrame([[np.nan, np.nan], [2., 2.]]))
10055+
1005010056
def test_pct_change(self):
1005110057
rs = self.tsframe.pct_change(fill_method=None)
1005210058
assert_frame_equal(rs, self.tsframe / self.tsframe.shift(1) - 1)

0 commit comments

Comments
 (0)