What is a trend in time series? - GeeksforGeeks

12 Dec, 2021
Time series data is a sequence of data points that measure some variable over ordered period of time. It is the fastest-growing category of databases as it is widely used in a variety of industries to understand and forecast data patterns. So while preparing this time series data for modeling it’s important to check for time series components or patterns.  One of these components is Trend. 
Trend is a pattern in data that shows the movement of a series to relatively higher or lower values over a long period of time. In other words, a trend is observed when there is an increasing or decreasing slope in the time series. Trend usually happens for some time and then disappears, it does not repeat. For example, some new song comes, it goes trending for a while, and then disappears. There is fairly any chance that it would be trending again.
A trend could be :
Uptrend: Time Series Analysis shows a general pattern that is upward then it is Uptrend.
Downtrend: Time Series Analysis shows a pattern that is downward then it is Downtrend.
Horizontal or Stationary trend: If no pattern observed then it is called a Horizontal or stationary trend.
You can find trends in data either by simply visualizing or by the decomposing dataset.
By simply plotting the dataset you can see the general trend in data
Approach :  
Import module
Load dataset
Cast month column to date time object
Set month as index
Create plot
Note: In the examples given below the same code is used to show all three trends just the dataset used is different to reflect that particular trend.
Link for datasets : click here
Example: Uptrend 
Python3
# importing the librariesimport pandas as pdimport matplotlib # importing datasetdata = pd.read_csv(r'C:\Users\admin\Downloads\Electric_Production.csv') # casting Month column to datetime objectdata['DATE'] = pd.to_datetime(data['DATE']) # Setting Month as indexdata = data.set_index('DATE') # Creating the plotdata.plot()
Output : 
Example: Downtrend
Python3
import pandas as pdimport matplotlib # importing datasetdata = pd.read_csv(r'C:\Users\admin\Downloads\AlcoholSale.csv') # casting Date column to datetime objectdata['DATE'] = pd.to_datetime(data['DATE']) # Setting Date column as indexdata = data.set_index('DATE') # Creating the plotdata.plot()
Output : 
Example: Horizontal trend
Python3
# importing the librariesimport pandas as pdimport matplotlib # importing datasetdata = pd.read_csv(    r'C:\Users\admin\Downloads\monthly-beer-production-in-austr.csv') # casting Month column to datetime objectdata['Month'] = pd.to_datetime(data['Month']) # Setting Month as indexdata = data.set_index('Month') # Creating the plotdata['1984':'1994'].plot()
Output : 
Decomposition
To see the complexity behind linear visualization we can decompose the data. The function called seasonal_decompose within the statsmodels package can help us to decompose the data into its components/show patterns — trend, seasonality and residual components of time series. Here we are interested in trend component only so will acces it using seasonal_decompose().trend .
seasonal_decompose function uses moving averages method to estimate the trend.
Syntax : 
statsmodels.tsa.seasonal.seasonal_decompose(x, model=’additive’, period=None, extrapolate_trend=0)
Important parameters : 
x : array-like.  Time-Series.  If 2d, individual series are in columns. x must contain 2 complete cycles.
model : {“additive”, “multiplicative”}, optional (Depends on nature on seasonal component)
period(freq.) : int, optional . Must be use if x is not pandas object or index of  x does not have a frequency.
Returns : A object with seasonal, trend, and resid attributes.
Example : 
Python3
# importing functionfrom statsmodels.tsa.seasonal import seasonal_decompose # creating trend object by assuming multiplicative modeloutput = seasonal_decompose(data, model='multiplicative').trend # creating plotoutput.plot()
Output : 
varshagumber28
Picked
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Selecting rows in pandas DataFrame based on conditions
Create a directory in Python
Python | Pandas dataframe.groupby()