Skip to content

Commit f36b90d

Browse files
committed
FIX DataFrame diff with timedelta pandas-dev#4533
1 parent 1fa5dd4 commit f36b90d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pandas/core/common.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,8 @@ def diff(arr, n, axis=0):
813813
na_indexer[axis] = slice(None, n) if n >= 0 else slice(n, None)
814814
out_arr[tuple(na_indexer)] = na
815815

816-
if arr.ndim == 2 and arr.dtype.name in _diff_special:
816+
if (arr.ndim == 2 and arr.dtype.name in _diff_special
817+
and dtype != 'timedelta64[ns]'):
817818
f = _diff_special[arr.dtype.name]
818819
f(arr, out_arr, n, axis)
819820
else:

pandas/tests/test_frame.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9285,6 +9285,16 @@ def test_diff_float_n(self):
92859285
xp = self.tsframe.diff(1)
92869286
assert_frame_equal(rs, xp)
92879287

9288+
def test_diff_timedelta(self):
9289+
df = DataFrame(dict(time=[Timestamp('20130101 9:01'),
9290+
Timestamp('20130101 9:02')],
9291+
value=[1.0,2.0]))
9292+
res = df.diff()
9293+
exp = DataFrame([[pd.NaT, np.nan],
9294+
[np.timedelta64(int(6e10), 'ns'), 1]],
9295+
columns=['time', 'value'])
9296+
assert_frame_equal(res, exp)
9297+
92889298
def test_pct_change(self):
92899299
rs = self.tsframe.pct_change(fill_method=None)
92909300
assert_frame_equal(rs, self.tsframe / self.tsframe.shift(1) - 1)

0 commit comments

Comments
 (0)