The Wayback Machine - https://web.archive.org/web/20240605084712/https://www.geeksforgeeks.org/what-is-a-trend-in-time-series/
Open In App

What is a trend in time series?

Last Updated : 20 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Visualization

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.

Example: Uptrend 

Python3




# importing the libraries
import pandas as pd
import matplotlib
  
# importing dataset
data = pd.read_csv(r'C:\Users\admin\Downloads\Electric_Production.csv')
  
# casting Month column to datetime object
data['DATE'] = pd.to_datetime(data['DATE'])
  
# Setting Month as index
data = data.set_index('DATE')
  
# Creating the plot
data.plot()


Output : 

Example: Downtrend

Python3




import pandas as pd
import matplotlib
  
# importing dataset
data = pd.read_csv(r'C:\Users\admin\Downloads\AlcoholSale.csv')
  
# casting Date column to datetime object
data['DATE'] = pd.to_datetime(data['DATE'])
  
# Setting Date column as index
data = data.set_index('DATE')
  
# Creating the plot
data.plot()


Output : 

Example: Horizontal trend

Python3




# importing the libraries
import pandas as pd
import matplotlib
  
# importing dataset
data = pd.read_csv(
    r'C:\Users\admin\Downloads\monthly-beer-production-in-austr.csv')
  
# casting Month column to datetime object
data['Month'] = pd.to_datetime(data['Month'])
  
# Setting Month as index
data = data.set_index('Month')
  
# Creating the plot
data['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 access 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 function
from statsmodels.tsa.seasonal import seasonal_decompose
  
# creating trend object by assuming multiplicative model
output = seasonal_decompose(data, model='multiplicative').trend
  
# creating plot
output.plot()


Output : 



Previous Article
Next Article

Similar Reads

Pandas Series dt.time | Extract Time from Time Stamp in Series
The Series.dt.time attribute returns a NumPy array containing time values of the timestamps in a Pandas series. Example C/C++ Code import pandas as pd sr = pd.Series(['2012-10-21 09:30', '2019-7-18 12:30', '2008-02-2 10:30', '2010-4-22 09:25', '2019-11-8 02:22']) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5'] sr.index = idx sr = pd.to_datetime
2 min read
Plotting a trend graph in Python
Prerequisites: Matplotlib A trend Graph is a graph that is used to show the trends data over a period of time. It describes a functional representation of two variables (x , y). In which the x is the time-dependent variable whereas y is the collected data. The graph can be in shown any form that can be via line chart, Histograms, scatter plot, bar
3 min read
Convert a series of date strings to a time series in Pandas Dataframe
During the analysis of a dataset, oftentimes it happens that the dates are not represented in proper type and are rather present as simple strings which makes it difficult to process them and perform standard date-time operations on them. pandas.to_datetime() Function helps in converting a date string to a python date object. So, it can be utilized
3 min read
Pandas Series dt.freq | Retrieve Frequency of Pandas Time Series
Pandas dt.freq attribute returns the time series frequency applied on the given series object if any, else it returns None. Examples C/C++ Code import pandas as pd sr = pd.Series(['2012-12-31', '2019-1-1 12:30', '2008-02-2 10:30', '2010-1-1 09:25', '2019-12-31 00:00']) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5'] sr.index = idx sr = pd.to_da
2 min read
Pandas Series dt.normalize() | Normalize Time in Pandas Series
The dt.normalize() method converts times to midnight. The time component of the date-time is converted to midnight i.e. 00:00:00. This is useful in cases when the time does not matter. Length is unaltered. The time zones are unaffected. Example: C/C++ Code import pandas as pd sr = pd.Series(pd.date_range('2012-12-31 09:45', periods = 5, freq = 'M',
2 min read
How To Highlight a Time Range in Time Series Plot in Python with Matplotlib?
A time series plot is a plot which contains data which is being measured over a period of time, for example, a gross domestic product of a country, the population of the world and many other data. Sometimes we want to highlight a specific period of the timeline so that it is easier for the observer to read specific data. We can highlight a time ran
4 min read
Time difference between expected time and given time
Given the initial clock time h1:m1 and the present clock time h2:m2, denoting hour and minutes in 24-hours clock format. The present clock time h2:m2 may or may not be correct. Also given a variable K which denotes the number of hours passed. The task is to calculate the delay in seconds i.e. time difference between expected time and given time. Ex
5 min read
Python | Pandas series.cumprod() to find Cumulative product of a Series
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Series.cumprod() is used to find Cumulative product of a series. In cumulative product, the length of returned series is same as
3 min read
Python | Pandas Series.str.replace() to replace text in a series
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages that makes importing and analyzing data much easier. Pandas Series.str.replace() method works like Python .replace() method only, but it works on Series too. Before calling .replace() on a Panda
5 min read
Python | Pandas Series.astype() to convert Data type of series
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas astype() is the one of the most important methods. It is used to change data type of a series. When data frame is made from a csv
2 min read
Article Tags :
Practice Tags :