Skip to content

Commit 06f74e5

Browse files
committed
ENH: generify common.diff per #2174, close #2087
1 parent fd26355 commit 06f74e5

File tree

2 files changed

+8
-23
lines changed

2 files changed

+8
-23
lines changed

RELEASE.rst

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pandas 0.9.1
7979
- Fix groupby(...).first() issue with datetime64 (#2133)
8080
- Better floating point error robustness in some rolling_* functions (#2114)
8181
- Fix ewma NA handling in the middle of Series (#2128)
82+
- Fix numerical precision issues in diff with integer data (#2087)
8283
8384
pandas 0.9.0
8485
============

pandas/core/common.py

+7-23
Original file line numberDiff line numberDiff line change
@@ -377,29 +377,13 @@ def diff(arr, n, indexer, axis=0):
377377
out_arr = arr - arr.take(indexer, axis=axis)
378378
out_arr = _maybe_upcast(out_arr)
379379

380-
if axis == 0:
381-
if n > 0:
382-
out_arr[:n] = np.nan
383-
elif n < 0:
384-
out_arr[n:] = np.nan
385-
else:
386-
out_arr[:] = np.nan
387-
elif axis == 1:
388-
if n > 0:
389-
out_arr[:, :n] = np.nan
390-
elif n < 0:
391-
out_arr[:, n:] = np.nan
392-
else:
393-
out_arr[:, :] = np.nan
394-
elif axis == 2:
395-
if n > 0:
396-
out_arr[:, :, :n] = np.nan
397-
elif n < 0:
398-
out_arr[:, :, n:] = np.nan
399-
else:
400-
out_arr[:, :, :] = np.nan
401-
else:
402-
raise NotImplementedError()
380+
indexer = [slice(None)] * arr.ndim
381+
if n > 0:
382+
indexer[axis] = slice(None, n)
383+
elif n < 0:
384+
indexer[axis] = slice(None, n)
385+
out_arr[tuple(indexer)] = np.nan
386+
403387
return out_arr
404388

405389

0 commit comments

Comments
 (0)