w3resource

Pandas Series: autocorr() function

Compute the lag-N autocorrelation in Pandas

The autocorr() function is used to compute the lag-N autocorrelation.

This method computes the Pearson correlation between the Series and its shifted self.

Syntax:

Series.autocorr(self, lag=1)
Pandas Series: autocorr() function

Parameters:

Name Description Type/Default Value Required / Optional
lag Number of lags to apply before performing autocorrelation. int
Default Value: 1
Required

Returns: float
The Pearson correlation between self and self.shift(lag)

Notes: If the Pearson correlation is not well defined return ‘NaN’.

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([0.35, 0.6, 0.4, -0.06])
s.autocorr()  # doctest: +ELLIPSIS

Output:

0.03350474249762301

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([0.35, 0.6, 0.4, -0.06])
s.autocorr(lag=2)  # doctest: +ELLIPSIS

Output:

-1.0

Example - If the Pearson correlation is not well defined, then ‘NaN’ is returned:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, 0, 0, 0])
s.autocorr()

Output:

nan

Previous: Test whether all element is true over requested Pandas axis
Next: Boolean Series in Pandas



Follow us on Facebook and Twitter for latest update.