Database Reference
In-Depth Information
Listing 12.4 Using Pandas for time series data
# Historical IBM stock price, 1962 to 2013
# IBMstock.csv
Date,Open,High,Low,Close,Volume,Adj Close
2013-02-22,199.23,201.09,198.84,201.09,3107900,201.09
2013-02-21,198.63,199.07,198.11,198.33,3922900,198.33
....
1962-01-03,572.00,577.00,572.00,577.00,288000,2.54
1962-01-02,578.50,578.50,572.00,572.00,387200,2.52
# Read CSV file and create a Pandas DataFrame
> import pandas as pd
> stock_data = pd.read_csv('IBMStock.csv',
index_col='Date',
parse_dates=True)
> print stock_data.head(3)
Open High Low Close Volume Adj Close
Date
2013-02-22 199.23 201.09 198.84 201.09 3107900 201.09
2013-02-21 198.63 199.07 198.11 198.33 3922900 198.33
2013-02-20 200.62 201.72 198.86 199.31 3715400 199.31
# Create an Eastern Time Zone localized DataFrame
> eastern_stock_data = stock_data.tz_localize('US/Eastern')
> print eastern_stock_data.head(3)
Open High Low Close Volume Adj Close
Date
2013-02-22 00:00:00-05:00 199.23 201.09 198.84 201.09 3107900 201.09
2013-02-21 00:00:00-05:00 198.63 199.07 198.11 198.33 3922900 198.33
2013-02-20 00:00:00-05:00 200.62 201.72 198.86 199.31 3715400 199.31
# Convert this data to a UTC time series
> utc_stock_data = eastern_stock_data.tz_convert('UTC')
> print utc_stock_data.head(3)
Open High Low Close Volume Adj Close
Date
2013-02-22 05:00:00+00:00 199.23 201.09 198.84 201.09 3107900 201.09
2013-02-21 05:00:00+00:00 198.63 199.07 198.11 198.33 3922900 198.33
2013-02-20 05:00:00+00:00 200.62 201.72 198.86 199.31 3715400 199.31
# A resampled DataFrame with the weekly max of each value
> print utc_stock_data.resample('W',how='max').sort(ascending=False).head(3)
Search WWH ::




Custom Search