Skip to content

Commit 50bf758

Browse files
committed
some small fixes to excel tools
svn path=/branches/v0_91_maint/; revision=5028
1 parent 7270563 commit 50bf758

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

examples/rec_groupby_demo.py

+12
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@
66
r.sort()
77

88
def daily_return(prices):
9+
'an array of daily returns from price array'
910
g = np.zeros_like(prices)
1011
g[1:] = (prices[1:]-prices[:-1])/prices[:-1]
1112
return g
1213

1314
def volume_code(volume):
15+
'code the continuous volume data categorically'
1416
ind = np.searchsorted([1e5,1e6, 5e6,10e6, 1e7], volume)
1517
return ind
1618

19+
# a list of (dtype_name, summary_function, output_dtype_name).
20+
# rec_summarize will call on each function on the indicated recarray
21+
# attribute, and the result assigned to output name in the return
22+
# record array.
1723
summaryfuncs = (
1824
('date', lambda x: [thisdate.year for thisdate in x], 'years'),
1925
('date', lambda x: [thisdate.month for thisdate in x], 'months'),
@@ -24,13 +30,18 @@ def volume_code(volume):
2430

2531
rsum = mlab.rec_summarize(r, summaryfuncs)
2632

33+
# stats is a list of (dtype_name, function, output_dtype_name).
34+
# rec_groupby will summarize the attribute identified by the
35+
# dtype_name over the groups in the groupby list, and assign the
36+
# result to the output_dtype_name
2737
stats = (
2838
('dreturn', len, 'rcnt'),
2939
('dreturn', np.mean, 'rmean'),
3040
('dreturn', np.median, 'rmedian'),
3141
('dreturn', np.std, 'rsigma'),
3242
)
3343

44+
# you can summarize over a single variable, like years or months
3445
print 'summary by years'
3546
ry = mlab.rec_groupby(rsum, ('years',), stats)
3647
print mlab. rec2txt(ry)
@@ -39,6 +50,7 @@ def volume_code(volume):
3950
rm = mlab.rec_groupby(rsum, ('months',), stats)
4051
print mlab.rec2txt(rm)
4152

53+
# or over multiple variables like years and months
4254
print 'summary by year and month'
4355
rym = mlab.rec_groupby(rsum, ('years','months'), stats)
4456
print mlab.rec2txt(rym)

lib/matplotlib/mlab.py

+7
Original file line numberDiff line numberDiff line change
@@ -1944,6 +1944,13 @@ def safe_isnan(x):
19441944
else: return b
19451945

19461946

1947+
def safe_isinf(x):
1948+
'isnan for arbitrary types'
1949+
try: b = npy.isinf(x)
1950+
except NotImplementedError: return False
1951+
else: return b
1952+
1953+
19471954
def rec_append_field(rec, name, arr, dtype=None):
19481955
'return a new record array with field name populated with data from array arr'
19491956
arr = npy.asarray(arr)

lib/matplotlib/toolkits/exceltools.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
import matplotlib.mlab as mlab
77
import matplotlib.toolkits.exceltools as exceltools
8-
8+
99
r = mlab.csv2rec('somefile.csv', checkrows=0)
1010
1111
formatd = dict(
@@ -107,6 +107,10 @@ def rec2excel(r, ws, formatd=None, rownum=0, colnum=0):
107107
else:
108108
if mlab.safe_isnan(val):
109109
ws.write(rownum, colnum+i, 'NaN')
110+
elif mlab.safe_isinf(val):
111+
if val<0: sign='-'
112+
else: sign='+'
113+
ws.write(rownum, colnum+i, '%sInf'%sign)
110114
else:
111115
ws.write(rownum, colnum+i, val, format.xlstyle)
112116
rownum += 1

0 commit comments

Comments
 (0)