Python | Pandas Series.autocorr()
Last Updated :
17 Feb, 2019
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.
Pandas Series.autocorr()
function compute the lag-N autocorrelation. This method computes the Pearson correlation between the Series and its shifted self.
Syntax: Series.autocorr(lag=1)
Parameter :
lag : Number of lags to apply before performing autocorrelation.
Returns : float
Example #1: Use Series.autocorr()
function to compute the lag-N auto-correlation of the underlying data for the given series object.
import pandas as pd
sr = pd.Series([ 11 , 21 , 8 , 18 , 65 , 18 , 32 , 10 , 5 , 32 , None ])
index_ = pd.date_range( '2010-10-09 08:45' , periods = 11 , freq = 'H' )
sr.index = index_
print (sr)
|
Output :
Now we will use Series.autocorr()
function to compute the lag-n auto-correlation of the underlying data for the given series object.
result = sr.autocorr()
print (result)
|
Output :
As we can see in the output, the Series.autocorr()
function has successfully returned the auto correlation of the underlying data of the given series object by lag 1.
Example #2 : Use Series.autocorr()
function to compute the lag-N auto-correlation of the underlying data for the given series object. Take the lag value equal to 3.
import pandas as pd
sr = pd.Series([ 34 , 5 , 13 , 32 , 4 , 15 ])
index_ = [ 'Coca Cola' , 'Sprite' , 'Coke' , 'Fanta' , 'Dew' , 'ThumbsUp' ]
sr.index = index_
print (sr)
|
Output :
Now we will use Series.autocorr()
function to compute the lag-n auto-correlation of the underlying data for the given series object.
result = sr.autocorr(lag = 3 )
print (result)
|
Output :
As we can see in the output, the Series.autocorr()
function has successfully returned the auto correlation of the underlying data of the given series object by lag 1.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...