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.
import numpy as np import pandas as pd import matplotlib.pyplot as plt from convert_to_timeseries import convert_data_to_timeseries
# Input file containing data input_file = 'data_timeseries.txt'
# Load data data1 = convert_data_to_timeseries(input_file, 2) data2 = convert_data_to_timeseries(input_file, 3)
dataframe = pd.DataFrame({'first': data1, 'second': data2})
# Plot data dataframe['1952':'1955'].plot() plt.title('Data overlapped on top of each other')
# Plot the difference plt.figure() difference = dataframe['1952':'1955']['first'] - dataframe['1952':'1955']['second'] difference.plot() plt.title('Difference (first - second)')
# When 'first' is greater than a certain threshold # and 'second' is smaller than a certain threshold dataframe[(dataframe['first'] > 60) & (dataframe['second'] < 20)].plot() plt.title('first > 60 and second < 20') plt.show()
operating_on_data.py
file that is already provided to you. If you run the code, the first figure will look like the following:18.222.83.185