Python - Load time series from CSV file using Pandas

By xngo on April 1, 2019

Here is how to load time series information from CSV file using Pandas. Assume that the data.csv file contains date, name, age and weight as follows:

2005-11-01 John 33 100.56
2005-11-02 Nikki 29 77.84

Here is the code to load the data.csv:

#!/usr/bin/python3
# Reference: https://stackoverflow.com/a/37453925
#            https://stackoverflow.com/a/17468012
#            http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html
 
import pandas as pd
 
my_headers = ['date', 'name', 'age', 'weight']
my_dtypes = {'date': 'str', 'name': 'str', 'age': 'int', 'weight': 'float'}
my_parse_dates = ['date']
loaded_data = pd.read_csv('data.csv', sep=' ', header=None, names=my_headers, 
                            dtype=my_dtypes, parse_dates=my_parse_dates)
 
print(loaded_data['date'])
print(loaded_data['name'])
print(loaded_data['age'])
print(loaded_data['weight'])

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.