Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ca40ff5

Browse files
committed
Make examples that load npy files with datetime(objects) py3 compatible
Requires np 1.10 to work under py3
1 parent a83dead commit ca40ff5

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

examples/api/date_demo.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,14 @@
2626
# The record array stores python datetime.date as an object array in
2727
# the date column
2828
datafile = 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

3138
fig, ax = plt.subplots()
3239
ax.plot(r.date, r.adj_close)

examples/pylab_examples/centered_ticklabels.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@
2020

2121
# load some financial data; apple's stock price
2222
fh = 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)
2431
fh.close()
2532
r = r[-250:] # get the last 250 days
2633

examples/pylab_examples/scatter_demo2.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@
1010
# The record array stores python datetime.date as an object array in
1111
# the date column
1212
datafile = 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)
1421
price_data = price_data[-250:] # get the most recent 250 trading days
1522

1623
delta1 = np.diff(price_data.adj_close)/price_data.adj_close[:-1]

0 commit comments

Comments
 (0)