File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2626# The record array stores python datetime.date as an object array in
2727# the date column
2828datafile = cbook .get_sample_data ('goog.npy' )
29- r = np .load (datafile ).view (np .recarray )
29+ try :
30+ # Python3 cannot load python2 .npy files with datetime(object) arrays
31+ # unless the encoding is set to bytes. Hovever this option was
32+ # not added until numpy 1.10 so this example will only work with
33+ # python 2 or with numpy 1.10 and later.
34+ r = np .load (datafile , encoding = 'bytes' ).view (np .recarray )
35+ except TypeError :
36+ r = np .load (datafile ).view (np .recarray )
3037
3138fig , ax = plt .subplots ()
3239ax .plot (r .date , r .adj_close )
Original file line number Diff line number Diff line change 2020
2121# load some financial data; apple's stock price
2222fh = cbook .get_sample_data ('aapl.npy.gz' )
23- r = np .load (fh )
23+ try :
24+ # Python3 cannot load python2 .npy files with datetime(object) arrays
25+ # unless the encoding is set to bytes. Hovever this option was
26+ # not added until numpy 1.10 so this example will only work with
27+ # python 2 or with numpy 1.10 and later.
28+ r = np .load (fh , encoding = 'bytes' )
29+ except TypeError :
30+ r = np .load (fh )
2431fh .close ()
2532r = r [- 250 :] # get the last 250 days
2633
Original file line number Diff line number Diff line change 1010# The record array stores python datetime.date as an object array in
1111# the date column
1212datafile = cbook .get_sample_data ('goog.npy' )
13- price_data = np .load (datafile ).view (np .recarray )
13+ try :
14+ # Python3 cannot load python2 .npy files with datetime(object) arrays
15+ # unless the encoding is set to bytes. Hovever this option was
16+ # not added until numpy 1.10 so this example will only work with
17+ # python 2 or with numpy 1.10 and later
18+ price_data = np .load (datafile , encoding = 'bytes' ).view (np .recarray )
19+ except TypeError :
20+ price_data = np .load (datafile ).view (np .recarray )
1421price_data = price_data [- 250 :] # get the most recent 250 trading days
1522
1623delta1 = np .diff (price_data .adj_close )/ price_data .adj_close [:- 1 ]
You can’t perform that action at this time.
0 commit comments