Operating on time series data
Now that we know how to slice data and extract various subsets, let's discuss how to operate on time series data. You can filter the data in many different ways. The pandas library allows you to operate on time series data in any way that you want.
How to do it…
Create a new Python file, and import the following packages:
import numpy as np import pandas as pd import matplotlib.pyplot as plt from convert_to_timeseries import convert_data_to_timeseries
We will use the same text file that we used in the previous recipe:
# Input file containing data input_file = 'data_timeseries.txt'
We will use both the third and fourth columns in this text file:
# Load data data1 = convert_data_to_timeseries(input_file, 2) data2 = convert_data_to_timeseries(input_file, 3)
Convert the data into a pandas data frame:
dataframe = pd.DataFrame({'first': data1, 'second': data2})
Plot the data in the given year range:
# Plot data dataframe['1952':'1955'].plot() plt.title('Data overlapped on top...