Handling time-series data with Pandas
Let's get started by learning how to handle time-series data in Pandas. In this section, we will convert a sequence of numbers into time series data and visualize it. Pandas provides options to add timestamps, organize data, and then efficiently operate on it.
Create a new Python file and import the following packages:
import numpy as np import matplotlib.pyplot as plt import pandas as pd
Define a function to read the data from the input file. The parameter index
indicates the column number that contains the relevant data:
def read_data(input_file, index): # Read the data from the input file input_data = np.loadtxt(input_file, delimiter=',')
Define a lambda
function to convert strings to Pandas date format:
# Lambda function to convert strings to Pandas date format to_date = lambda x, y: str(int(x)) + '-' + str(int(y))
Use this lambda
function to get the start date from the first line in the input file:
...