7. Time series plotting
The drawing function of the Pandas time series is better than the Matplotlib native in date formatting.
#-*-coding: utf-8-*-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime as dt
from pandas import Series, DataFrame
from datetime import datetime
from dateutil.parser import parse
import time
from pandas.tseries.offsets import Hour, Minute, Day, MonthEnd
import pytz
#The following two parameters, one is to parse the date form, and the other is to use the first column as the row name
close_px_all = pd.read_csv (‘E: \\ stock_px.csv‘, parse_dates = True, index_col = 0)
print close_px_all.head (), ‘\ n’
close_px = close_px_all [[‘AAPL‘, ‘MSFT’, ‘XOM’]]
close_px = close_px.resample (‘B‘, fill_method = ‘ffill’)
print close_px.head ()
#Note the following index method
close_px [‘AAPL‘]. plot ()
close_px.ix [‘2009‘]. plot ()
close_px [‘AAPL‘]. ix [‘01 -2011 ‘:‘ 03-2011 ’]. plot ()
#Quarterly frequency data will be formatted with quarter markers, this kind of thing will be very laborious if manual ... (It makes sense ...)
appl_q = close_px [‘AAPL‘]. resample (‘Q-DEC’, fill_method = ‘ffill’)
appl_q.ix [‘2009‘:]. plot ()
#The author said that the interactive way, right-click and hold the date will dynamically expand or contract, actually do it yourself, no effect ...
plt.show ()
>>>
AA AAPL GE IBM JNJ MSFT PEP SPX XOM
1990-02-01 4.98 7.86 2.87 16.79 4.27 0.51 6.04 328.79 6.12
1990-02-02 5.04 8.00 2.87 16.89 4.37 0.51 6.09 330.92 6.24
1990-02-05 5.07 8.18 2.87 17.32 4.34 0.51 6.05 331.85 6.25
1990-02-06 5.01 8.12 2.88 17.56 4.32 0.51 6.15 329.66 6.23
1990-02-07 5.04 7.77 2.91 17.93 4.38 0.51 6.17 333.75 6.33
AAPL MSFT XOM
1990-02-01 7.86 0.51 6.12
1990-02-02 8.00 0.51 6.24
1990-02-05 8.18 0.51 6.25
1990-02-06 8.12 0.51 6.23
1990-02-07 7.77 0.51 6.33
[Finished in 37.5s]
Here are some of the diagrams made:
8. Moving window functions
The various statistical functions computed on the Moving window (which can have exponential attenuation weights) are also a class of array transformations common to time series. The authors refer to them as moving window functions (moving window function), which also includes functions that have a variable window length (such as exponential weighted moving average). As with other statistical functions, the Move window function automatically excludes missing values.
"Data analysis using Python" reading notes--tenth Chapter time series (iii)