Skip to content

Commit bc696a7

Browse files
author
Chang She
committed
BUG: Series diff(0) fails #3257
1 parent 78764f0 commit bc696a7

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

pandas/core/common.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ def diff(arr, n, axis=0):
634634
res_indexer = tuple(res_indexer)
635635

636636
lag_indexer = [slice(None)] * arr.ndim
637-
lag_indexer[axis] = slice(None, -n) if n >= 0 else slice(-n, None)
637+
lag_indexer[axis] = slice(None, -n) if n > 0 else slice(-n, None)
638638
lag_indexer = tuple(lag_indexer)
639639

640640
out_arr[res_indexer] = arr[res_indexer] - arr[lag_indexer]
@@ -763,7 +763,7 @@ def changeit():
763763
# our type is wrong here, need to upcast
764764
r, fill_value = _maybe_upcast(result, fill_value=other, dtype=dtype, copy=True)
765765
np.putmask(r, mask, other)
766-
766+
767767
# we need to actually change the dtype here
768768
if change is not None:
769769
change.dtype = r.dtype
@@ -778,7 +778,7 @@ def changeit():
778778
new_dtype, fill_value = _maybe_promote(result.dtype,other)
779779
if new_dtype != result.dtype:
780780

781-
# we have a scalar or len 0 ndarray
781+
# we have a scalar or len 0 ndarray
782782
# and its nan and we are changing some values
783783
if np.isscalar(other) or (isinstance(other,np.ndarray) and other.ndim < 1):
784784
if isnull(other):

pandas/tests/test_series.py

+5
Original file line numberDiff line numberDiff line change
@@ -3909,6 +3909,11 @@ def test_diff(self):
39093909
xp = self.ts - self.ts.shift(-1)
39103910
assert_series_equal(rs, xp)
39113911

3912+
# 0
3913+
rs = self.ts.diff(0)
3914+
xp = self.ts - self.ts
3915+
assert_series_equal(rs, xp)
3916+
39123917
def test_pct_change(self):
39133918
rs = self.ts.pct_change(fill_method=None)
39143919
assert_series_equal(rs, self.ts / self.ts.shift(1) - 1)

0 commit comments

Comments
 (0)