File tree 3 files changed +33
-4
lines changed
3 files changed +33
-4
lines changed Original file line number Diff line number Diff line change @@ -104,6 +104,7 @@ Enhancements
104
104
- Added ``Series.str.slice_replace()``, which previously raised NotImplementedError (:issue:`8888`)
105
105
- Added ``Timestamp.to_datetime64()`` to complement ``Timedelta.to_timedelta64()`` (:issue:`9255`)
106
106
- ``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`)
107
108
- ``Timedelta`` will now accept nanoseconds keyword in constructor (:issue:`9273`)
108
109
- SQL code now safely escapes table and column names (:issue:`8986`)
109
110
Original file line number Diff line number Diff line change @@ -1336,15 +1336,20 @@ def diff(self, periods=1):
1336
1336
result = com .diff (_values_from_object (self ), periods )
1337
1337
return self ._constructor (result , index = self .index ).__finalize__ (self )
1338
1338
1339
- def autocorr (self ):
1339
+ def autocorr (self , lag = 1 ):
1340
1340
"""
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.
1342
1347
1343
1348
Returns
1344
1349
-------
1345
1350
autocorr : float
1346
1351
"""
1347
- return self .corr (self .shift (1 ))
1352
+ return self .corr (self .shift (lag ))
1348
1353
1349
1354
def dot (self , other ):
1350
1355
"""
Original file line number Diff line number Diff line change @@ -6323,7 +6323,30 @@ def test_pct_change_shift_over_nas(self):
6323
6323
6324
6324
def test_autocorr (self ):
6325
6325
# 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 )
6327
6350
6328
6351
def test_first_last_valid (self ):
6329
6352
ts = self .ts .copy ()
You can’t perform that action at this time.
0 commit comments