| 3 | |
| 4 | class TestAutoCorr: |
| 5 | def test_autocorr(self, datetime_series): |
| 6 | # Just run the function |
| 7 | corr1 = datetime_series.autocorr() |
| 8 | |
| 9 | # Now run it with the lag parameter |
| 10 | corr2 = datetime_series.autocorr(lag=1) |
| 11 | |
| 12 | # corr() with lag needs Series of at least length 2 |
| 13 | if len(datetime_series) <= 2: |
| 14 | assert np.isnan(corr1) |
| 15 | assert np.isnan(corr2) |
| 16 | else: |
| 17 | assert corr1 == corr2 |
| 18 | |
| 19 | # Choose a random lag between 1 and length of Series - 2 |
| 20 | # and compare the result with the Series corr() function |
| 21 | n = 1 + np.random.default_rng(2).integers(max(1, len(datetime_series) - 2)) |
| 22 | corr1 = datetime_series.corr(datetime_series.shift(n)) |
| 23 | corr2 = datetime_series.autocorr(lag=n) |
| 24 | |
| 25 | # corr() with lag needs Series of at least length 2 |
| 26 | if len(datetime_series) <= 2: |
| 27 | assert np.isnan(corr1) |
| 28 | assert np.isnan(corr2) |
| 29 | else: |
| 30 | assert corr1 == corr2 |