Slicing time series data
In this recipe, we will learn how to slice time series data using pandas. This will help you extract information from various intervals in the time series data. We will learn how to use dates to handle subsets of our data.
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 to slice and dice the data:
# Input file containing data input_file = 'data_timeseries.txt'
We will use the third column again:
# Load data column_num = 2 data_timeseries = convert_data_to_timeseries(input_file, column_num)
Let's assume that we want to extract the data between given start and end years. Let's define these, as follows:
# Plot within a certain year range start = '2008' end = '2015'
Plot the data between the given year range:
plt.figure() data_timeseries[start:end].plot(...