Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9bd7365

Browse filesBrowse files
committed
Merge pull request #5292 from jenshnielsen/datetimeexamplespy3load
FIX: Make examples that load npy files with datetime(objects) py3 compatible
2 parents dd78552 + d792936 commit 9bd7365
Copy full SHA for 9bd7365

File tree

Expand file treeCollapse file tree

4 files changed

+41
-7
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+41
-7
lines changed

‎doc/users/recipes.rst

Copy file name to clipboardExpand all lines: doc/users/recipes.rst
+17-4Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,15 @@ you will see that the x tick labels are all squashed together.
118118

119119
import matplotlib.cbook as cbook
120120
datafile = cbook.get_sample_data('goog.npy')
121-
r = np.load(datafile).view(np.recarray)
121+
try:
122+
# Python3 cannot load python2 .npy files with datetime(object) arrays
123+
# unless the encoding is set to bytes. Hovever this option was
124+
# not added until numpy 1.10 so this example will only work with
125+
# python 2 or with numpy 1.10 and later.
126+
r = np.load(datafile, encoding='bytes').view(np.recarray)
127+
except TypeError:
128+
# Old Numpy
129+
r = np.load(datafile).view(np.recarray)
122130
plt.figure()
123131
plt.plot(r.date, r.close)
124132
plt.title('Default date handling can cause overlapping labels')
@@ -179,8 +187,14 @@ right.
179187

180188
# load up some sample financial data
181189
datafile = cbook.get_sample_data('goog.npy')
182-
r = np.load(datafile).view(np.recarray)
183-
190+
try:
191+
# Python3 cannot load python2 .npy files with datetime(object) arrays
192+
# unless the encoding is set to bytes. Hovever this option was
193+
# not added until numpy 1.10 so this example will only work with
194+
# python 2 or with numpy 1.10 and later.
195+
r = np.load(datafile, encoding='bytes').view(np.recarray)
196+
except TypeError:
197+
r = np.load(datafile).view(np.recarray)
184198
# create two subplots with the shared x and y axes
185199
fig, (ax1, ax2) = plt.subplots(1,2, sharex=True, sharey=True)
186200

@@ -363,4 +377,3 @@ argument takes a dictionary with keys that are Patch properties.
363377
# place a text box in upper left in axes coords
364378
ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14,
365379
verticalalignment='top', bbox=props)
366-

‎examples/api/date_demo.py

Copy file name to clipboardExpand all lines: examples/api/date_demo.py
+8-1Lines 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

Copy file name to clipboardExpand all lines: examples/pylab_examples/centered_ticklabels.py
+8-1Lines 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

Copy file name to clipboardExpand all lines: examples/pylab_examples/scatter_demo2.py
+8-1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.