Sunday, July 5, 2020

Handling, Slicing and Extracting Statistic from Time Series Data -1 (Handling Time Series Data)

Pandas is a very useful tool if you have to work with time series data. With the help of Pandas, you can perform the following:
  • Create a range of dates by using the pd.date_range package
  • Index pandas with dates by using the pd.Series package
  • Perform re-sampling by using the ts.resample package
  • Change the frequency
Example

The following example shows you handling and slicing the time series data by using Pandas. Note that here we are using the Monthly Arctic Oscillation data, which can be downloaded from http://www.cpc.ncep.noaa.gov/products/precip/CWlink/daily_ao_index/monthly.ao.index.b50.current.ascii and can be converted to text format for our use.

Handling time series data

For handling time series data, you will have to perform the following steps:

The first step involves importing the following packages:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd


Next, define a function which will read the data from the input file, as shown in the code given below:

def read_data(input_file):
input_data = np.loadtxt(input_file, delimiter=None)


Now, convert this data to time series. For this, create the range of dates of our time series. In this example, we keep one month as frequency of data. Our file is having the data which starts from January 1950.

dates = pd.date_range('1950-01', periods=input_data.shape[0], freq='M')

In this step, we create the time series data with the help of Pandas Series, as shown below:

output = pd.Series(input_data[:, index], index=dates)
return output
if __name__=='__main__':

Enter the path of the input file as shown here:

input_file = "/Users/admin/AO.txt"

Now, convert the column to timeseries format, as shown here:

timeseries = read_data(input_file)

Finally, plot and visualize the data, using the commands shown:

plt.figure()
timeseries.plot()
plt.show()


You will observe the plots as shown in the following images:



Next post will focus on Slicing time series data.
Share:

0 comments:

Post a Comment