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.
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 column_num = 2 data_timeseries = convert_data_to_timeseries(input_file, column_num)
# Plot within a certain year range start = '2008' end = '2015'
plt.figure() data_timeseries[start:end].plot() plt.title('Data from ' + start + ' to ' + end)
# Plot within a certain range of dates start = '2007-2' end = '2007-11'
plt.figure() data_timeseries[start:end].plot() plt.title('Data from ' + start + ' to ' + end) plt.show()
slicing_data.py
file that is provided to you. If you run the code, you will see the following image:18.118.120.204