Skip to content

BUG: Fix .diff() on datelike and timedelta operations (GH3100_) #3554

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
merged 1 commit into from
May 9, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pandas 0.11.1
- Properly convert np.datetime64 objects in a Series (GH3416_)
- Raise a TypeError on invalid datetime/timedelta operations
e.g. add datetimes, multiple timedelta x datetime
- Fix ``.diff`` on datelike and timedelta operations (GH3100_)

.. _GH3164: https://github.com/pydata/pandas/issues/3164
.. _GH2786: https://github.com/pydata/pandas/issues/2786
Expand All @@ -108,6 +109,7 @@ pandas 0.11.1
.. _GH3379: https://github.com/pydata/pandas/issues/3379
.. _GH3480: https://github.com/pydata/pandas/issues/3480
.. _GH2852: https://github.com/pydata/pandas/issues/2852
.. _GH3100: https://github.com/pydata/pandas/issues/3100
.. _GH3454: https://github.com/pydata/pandas/issues/3454
.. _GH3457: https://github.com/pydata/pandas/issues/3457
.. _GH3491: https://github.com/pydata/pandas/issues/3491
Expand Down
32 changes: 29 additions & 3 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,9 +617,18 @@ def func(arr, indexer, out, fill_value=np.nan):


def diff(arr, n, axis=0):
""" difference of n between self,
analagoust to s-s.shift(n) """

n = int(n)
dtype = arr.dtype
if issubclass(dtype.type, np.integer):
na = np.nan

if is_timedelta64_dtype(arr) or is_datetime64_dtype(arr):
dtype = 'timedelta64[ns]'
arr = arr.view('i8')
na = tslib.iNaT
elif issubclass(dtype.type, np.integer):
dtype = np.float64
elif issubclass(dtype.type, np.bool_):
dtype = np.object_
Expand All @@ -628,7 +637,7 @@ def diff(arr, n, axis=0):

na_indexer = [slice(None)] * arr.ndim
na_indexer[axis] = slice(None, n) if n >= 0 else slice(n, None)
out_arr[tuple(na_indexer)] = np.nan
out_arr[tuple(na_indexer)] = na

if arr.ndim == 2 and arr.dtype.name in _diff_special:
f = _diff_special[arr.dtype.name]
Expand All @@ -642,7 +651,24 @@ def diff(arr, n, axis=0):
lag_indexer[axis] = slice(None, -n) if n > 0 else slice(-n, None)
lag_indexer = tuple(lag_indexer)

out_arr[res_indexer] = arr[res_indexer] - arr[lag_indexer]
# need to make sure that we account for na for datelike/timedelta
# we don't actually want to subtract these i8 numbers
if dtype == 'timedelta64[ns]':
res = arr[res_indexer]
lag = arr[lag_indexer]

mask = (arr[res_indexer] == na) | (arr[lag_indexer] == na)
if mask.any():
res = res.copy()
res[mask] = 0
lag = lag.copy()
lag[mask] = 0

result = res-lag
result[mask] = na
out_arr[res_indexer] = result
else:
out_arr[res_indexer] = arr[res_indexer] - arr[lag_indexer]

return out_arr

Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4039,6 +4039,17 @@ def test_diff(self):
xp = self.ts - self.ts
assert_series_equal(rs, xp)

# datetime diff (GH3100)
s = Series(date_range('20130102',periods=5))
rs = s-s.shift(1)
xp = s.diff()
assert_series_equal(rs, xp)

# timedelta diff
nrs = rs-rs.shift(1)
nxp = xp.diff()
assert_series_equal(nrs, nxp)

def test_pct_change(self):
rs = self.ts.pct_change(fill_method=None)
assert_series_equal(rs, self.ts / self.ts.shift(1) - 1)
Expand Down