Friday, July 5, 2024
HomeLanguagesPythonPython | Pandas Series.autocorr()

Python | Pandas Series.autocorr()

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.




# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series([11, 21, 8, 18, 65, 18, 32, 10, 5, 32, None])
  
# Create the Index
index_ = pd.date_range('2010-10-09 08:45', periods = 11, freq ='H')
  
# set the index
sr.index = index_
  
# Print the series
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.




# return the auto correlation
result = sr.autocorr()
  
# Print the result
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.




# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series([34, 5, 13, 32, 4, 15])
  
# Create the Index
index_ = ['Coca Cola', 'Sprite', 'Coke', 'Fanta', 'Dew', 'ThumbsUp']
  
# set the index
sr.index = index_
  
# Print the series
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.




# return the auto correlation
# by lag-3
result = sr.autocorr(lag = 3)
  
# Print the result
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.

Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments