How To Resample Time Series Data In Pandas


Notice: Undefined index: limited_lang in /home/beaczwhx/dggulaitutorial.com/wp-content/plugins/code-syntax-highlighter/inc/src/rendrer.php on line 297

Hello everyone, welcome to my new Pandas Resample Time Series tutorial. In this tutorial you will learn to resample time series data in pandas using python. This tutorial will cover to resample time series data in various form which are following –

  • Resampling time series data weekly
  • Resampling time series data monthly
  • Resampling time series data hourly
  • Resampling time series data minute etc.

Before proceeding to main topic of this tutorial, we will learn about meaning of resampling time series data.

Introduction To Resample Time Series Data In Pandas

Resampling of time series data is a process of summarizing or aggregating time series data by the new period of time. For example, to summarize daily data to monthly data or weekly data etc.

Why we need to resample time series data?

For most use cases, the data provided isn’t clean, even more when the granularity is decreasing. Data points are often acquired by manual input, when a variation is detected or when an event occurred. Sometimes people may have decided to change the acquisition periods and that leads to different time steps in the series. These are problems that you generally have to deal with to get a cleaned time series ready to be processed in stream.

There are two important reasons why we need to resample time series data.

  1. Problem Framing
  2. Feature Engineering

Types of resampling time series

There are two types of resampling time series data which are following.

  1. Upsampling – It is a process of increasing the frequency of time series data such as from minutes to seconds.
  2. Downsampling – It is a process of decreasing the frequency of time series data such as from days to months.

So guys, till now we have seen just some basics idea about resampling time series. Now let’s start to learn how to resample time series in pandas with python.

So let’s gets started – What is resample time series in pandas and how to do that?

Pandas Resample Time Series – How To Resample Time Series Data In Pandas

Pandas is very popular library for data science. Pandas is very useful in time series analysis. It provides easiest ways to do time series analysis. Resampling is one of the main process in time series analysis. Pandas provide an important method resample() to resample time series data. So in this section you will learn the one of the important concept of time series analysis i.e. – How do you resample time series data?

Let’s gets started main topic of our Pandas Resample Time Series tutorial.

Steps Involved In Resampling Time Series In Pandas

These are following process which takes place during resampling time series in pandas.

  1. Load timeseries data into pandas dataframe.
  2. Convert data column into a pandas datatypes.
  3. Choose the resampling frequency and apply the pandas.DataFrame.resample method.
  4. Perform some time series operations like rolling, moving, average and shifting.

Aggregation Methods In Time Series Resampling

We can apply following aggregation methods according to our requirement to resample time series data.

  1. mean()
  2. min()
  3. max()
  4. sum()

Now we will learn to resample time series data in pandas.

Loading Time Series Data

import pandas as pd

df = pd.read_csv("sales_data_sample.csv", encoding= 'unicode_escape', parse_dates=['ORDERDATE'], 
    index_col=['ORDERDATE'])

df.head(5)
  • Firstly we have imported pandas library.
  • Then used read_csv() method to use csv file. Then used parse_dates parameter to change the type of ORDERDATE column into datetimes datatype.
  • Then we have converted ORDERDATE column to index column of the dataframe.

Output

Pandas Resample Time Series

Resampling Time Series In Different Frequencies

In this section we will learn resampling pandas time series in different frequencies. We will also cover various pandas resample example codes.

Resample Time Series Monthly

We have to write following code to resample time series data monthly. In this pandas resample time series monthly example, we have resampled the sales data in monthly frequency.

import pandas as pd

df = pd.read_csv("sales_data_sample.csv", encoding= 'unicode_escape', parse_dates=['ORDERDATE'], 
index_col=['ORDERDATE'])           #loading data 

df.SALES.resample('M').mean()     #resampling in monthly frequency
  • Here we have resampled daily sales of this dataset in monthly frequency. Here we have applied the mean() method to get mean of monthly sales.

Now let’s see its output.

Output

Pandas Resample Time Series

Now we will plot a chart of this data. So run following code to do so.

%matplotlib inline                                  #display output inline
df.SALES.resample('M').mean().plot()     #calling plot() method of matplotlib library

%matplotlib is a magic command which performs the necessary behind-the-scenes setup for IPython to work correctly hand-in-hand with matplotlib. It does not execute any Python import commands, that is, no names are added to the namespace.

Output

Pandas Resample Time Series

Resample Time Series Weekly

Write the following code for pandas resample time series weekly.

import pandas as pd

df = pd.read_csv("sales_data_sample.csv", encoding= 'unicode_escape', parse_dates=['ORDERDATE'], 
    index_col=['ORDERDATE'])

df.SALES.resample('W').sum()  #resampling weekly

Output

So sum of weekly sales data of this sample sales data file is following.

Pandas Resample Time Series

Resample Time Series Daily

We have to write following code to resample time series data daily. In this pandas resample time series daily example, we have resampled the sales data in daily frequency.

import pandas as pd
df = pd.read_csv("sales_data_sample.csv", encoding= 'unicode_escape', parse_dates=['ORDERDATE'], 
    index_col=['ORDERDATE'])

df.SALES.resample('D').mean()

Output

Resample Time Series Yearly

We will pass the argument Y to resample() method to resample time series data in yearly frequency. Let’s see an example of pandas resample time series yearly.

import pandas as pd
df = pd.read_csv("sales_data_sample.csv", encoding= 'unicode_escape', parse_dates=['ORDERDATE'], 
    index_col=['ORDERDATE'])

df.SALES.resample('Y').max()
  • Here we have used max() aggregation method to get maximum sales of this dataset in yearly frequency.

Output

So our output is here.

Resample Time Series Quarterly

We will pass the argument Q to resample() method to resample time series data in quarterly frequency. Let’s understand it with pandas resample time series quarterly example.

import pandas as pd
df = pd.read_csv("sales_data_sample.csv", encoding= 'unicode_escape', parse_dates=['ORDERDATE'], 
    index_col=['ORDERDATE'])

df.SALES.resample('Q').mean()

Output

Resample Time Series Hourly

We have to pass the argument H to resample() method to resample time series data in hourly frequency. So here is an example of pandas resample time series hourly.

import pandas as pd

df = pd.read_csv("sales_data_sample.csv", encoding= 'unicode_escape', parse_dates=['ORDERDATE'], 
    index_col=['ORDERDATE'])

df.SALES.resample('H').mean()

Output

Resample Time Series Minutes

We have to pass the argument min to resample() method to resample time series data in minutes frequency. So here is an example code of pandas resample time series minutes.

import pandas as pd

df = pd.read_csv("sales_data_sample.csv", encoding= 'unicode_escape', parse_dates=['ORDERDATE'], 
    index_col=['ORDERDATE'])

df.SALES.resample('min').mean()

Output

Resample Time Series Second

We have to pass the argument s to resample() method to resample time series data in seconds frequency. So here is an example code of pandas resample time series seconds.

So guys now i am wrapping up Pandas resample time series tutorial here. I hope you have found this article valuable and you have learned resampling of time series data in pandas very well. But still if you have any doubt about this tutorial then feel free to ask in comment section. I will be happy to help you. Stay tuned with Dggul AI Tutorial for getting more valuable pandas tutorials.

 

People Are Also Reading…

Leave a Comment