Skip to content

Commit d349adc

Browse files
omtinezjreback
authored andcommitted
Add lag parameter to autocorrelation, default to lag-1 autocorrelation
so existing code will work unchanged (GH9339)
1 parent 0b789eb commit d349adc

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

doc/source/whatsnew/v0.16.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ Enhancements
104104
- Added ``Series.str.slice_replace()``, which previously raised NotImplementedError (:issue:`8888`)
105105
- Added ``Timestamp.to_datetime64()`` to complement ``Timedelta.to_timedelta64()`` (:issue:`9255`)
106106
- ``tseries.frequencies.to_offset()`` now accepts ``Timedelta`` as input (:issue:`9064`)
107+
- Lag parameter was added to the autocorrelation method of Series, defaults to lag-1 autocorrelation (:issue:`9192`)
107108
- ``Timedelta`` will now accept nanoseconds keyword in constructor (:issue:`9273`)
108109
- SQL code now safely escapes table and column names (:issue:`8986`)
109110

pandas/core/series.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1336,15 +1336,20 @@ def diff(self, periods=1):
13361336
result = com.diff(_values_from_object(self), periods)
13371337
return self._constructor(result, index=self.index).__finalize__(self)
13381338

1339-
def autocorr(self):
1339+
def autocorr(self, lag=1):
13401340
"""
1341-
Lag-1 autocorrelation
1341+
Lag-N autocorrelation
1342+
1343+
Parameters
1344+
----------
1345+
lag : int, default 1
1346+
Number of lags to apply before performing autocorrelation.
13421347
13431348
Returns
13441349
-------
13451350
autocorr : float
13461351
"""
1347-
return self.corr(self.shift(1))
1352+
return self.corr(self.shift(lag))
13481353

13491354
def dot(self, other):
13501355
"""

pandas/tests/test_series.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -6323,7 +6323,30 @@ def test_pct_change_shift_over_nas(self):
63236323

63246324
def test_autocorr(self):
63256325
# Just run the function
6326-
self.ts.autocorr()
6326+
corr1 = self.ts.autocorr()
6327+
6328+
# Now run it with the lag parameter
6329+
corr2 = self.ts.autocorr(lag=1)
6330+
6331+
# corr() with lag needs Series of at least length 2
6332+
if len(self.ts) <= 2:
6333+
self.assertTrue(np.isnan(corr1))
6334+
self.assertTrue(np.isnan(corr2))
6335+
else:
6336+
self.assertEqual(corr1, corr2)
6337+
6338+
# Choose a random lag between 1 and length of Series - 2
6339+
# and compare the result with the Series corr() function
6340+
n = 1 + np.random.randint(max(1, len(self.ts) - 2))
6341+
corr1 = self.ts.corr(self.ts.shift(n))
6342+
corr2 = self.ts.autocorr(lag=n)
6343+
6344+
# corr() with lag needs Series of at least length 2
6345+
if len(self.ts) <= 2:
6346+
self.assertTrue(np.isnan(corr1))
6347+
self.assertTrue(np.isnan(corr2))
6348+
else:
6349+
self.assertEqual(corr1, corr2)
63276350

63286351
def test_first_last_valid(self):
63296352
ts = self.ts.copy()

0 commit comments

Comments
 (0)